sgu 101 Domino

来源:互联网 发布:域名是否被墙查询 编辑:程序博客网 时间:2024/05/01 03:25

题目描述:

                                    101. Domino

                                                              time limit per test: 0.5 sec. 
                                                         memory limit per test: 4096 KB 

Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The blocks usually are called bones, dominoes, or pieces and sometimes men, stones, or even cards.
The face of each piece is divided, by a line or ridge, into two squares, each of which is marked as would be a pair of dice...

The principle in nearly all modern dominoes games is to match one end of a piece to another that is identically or reciprocally numbered.

ENCYCLOPÆDIA BRITANNICA


Given a set of domino pieces where each side is marked with two digits from 0 to 6. Your task is to arrange pieces in a line such way, that they touch through equal marked sides. It is possible to rotate pieces changing left and right side.


Input

The first line of the input contains a single integer N (1 ≤ N ≤ 100) representing the total number of pieces in the domino set. The following N lines describe pieces. Each piece is represented on a separate line in a form of two digits from 0 to 6 separated by a space.


Output

Write “No solution” if it is impossible to arrange them described way. If it is possible, write any of way. Pieces must be written in left-to-right order. Every of N lines must contains number of current domino piece and sign “+” or “-“ (first means that you not rotate that piece, and second if you rotate it).


Sample Input
5
1 2
2 4
2 4
6 4
2 1


Sample Output
2 -
5 +
1 +
3 +
4 -


思路:

经典的欧陆路径题:

约等于模板题:

代码:

#include<iostream>#include<cstring>#include<cstdio>#include<set>#include<algorithm>#include<vector>#include<cstdlib>#define inf 0xfffffff#define CLR(a,b) memset((a),(b),sizeof((a)))using namespace std;int const nMax = 110;typedef int LL;typedef pair<LL,char> pij;int du[7];struct edge{    bool vis;    int u,v;};edge e[nMax];vector<int> a[7];vector<pij> st;void dfs(int u){    int v;    for(int i=0;i<a[u].size();i++)if(!e[v=a[u][i]].vis){        e[v].vis=true;        if(u==e[v].u){            dfs(e[v].v);            st.push_back(pij(v+1,'+'));        }else {            dfs(e[v].u);            st.push_back(pij(v+1,'-'));        }    }    return ;}int n;int main(){    CLR(du,0);    scanf("%d",&n);    for(int i=0;i<n;i++){        scanf("%d%d",&e[i].u,&e[i].v);        e[i].vis=false;        du[e[i].u]++;du[e[i].v]++;        a[e[i].u].push_back(i);        a[e[i].v].push_back(i);    }    int k=0;    for(int i=0;i<=6;i++)k+=(du[i]&1);    if((k!=0&&k!=2))puts("No solution");    else {        st.clear();        if(k){            for(int i=0;i<=6;i++)if(du[i]%2){                dfs(i);                break;            }        }else  {            for(int i=0;i<=6;i++){                if(du[i]){                    dfs(i);                    break;                }            }        }        if(st.size()<n)puts("No solution");        else {            for(int i=st.size()-1;i>=0;i--)printf("%d %c\n",st[i].first,st[i].second);        }    }    return 0;}