Description
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 -
题意:给一些多米诺骨牌,两面有数字。要求用这些多米诺骨牌摆成一列,使得相对面的数字相同
分析:就是一道欧拉路径的题,但是题目要求把欧拉路径打印出来,所以要用到一个递归打印的算法。这道题要求记录边的方向,所以用邻接表来存图(用邻接矩阵也可以不过反正要存边)。判断是否联通用并查集,判断是否有欧拉路径用奇数度点的个数来判断(如果奇数度的点有两个或者没有,那么就有欧拉路径)找一个奇数度的点作为起点(如果没有就任意)打印路径即可。
这题我很白痴的WA了好多发,后来才发现自己内存开小了(因为要存两倍的边)实在太傻比了。。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n,fa[10],deg[10],cnt,head[10],done[205];
struct edge{
int u,v,flag,next;
}e[205];
void addedge(int u,int v){
e[cnt].u = u;
e[cnt].v = v;
e[cnt].flag = 1;
e[cnt].next = head[u];
head[u] = cnt++;
e[cnt].u = v;
e[cnt].v = u;
e[cnt].flag = -1;
e[cnt].next = head[v];
head[v] = cnt++;
}
int findset(int x){
return fa[x]!=x?fa[x] = findset(fa[x]):x;
}
void euler(int st){
for(int i=head[st];i!=-1;i = e[i].next){
int v = e[i].v,ok = e[i].flag;
if(!done[i]){
done[i] = done[i+ok] = 1;
euler(v);
printf("%d %c\n",i/2+1,ok+1?'-':'+');
}
}
}
int main()
{
while(scanf("%d",&n)==1){
int u,v;
memset(deg,0,sizeof(deg));
memset(head,-1,sizeof(head));
memset(done,0,sizeof(done));
memset(e,0,sizeof(e));
cnt = 0;
for(int i=0;i<10;i++)fa[i] = i;
for(int i=1;i<=n;i++){
scanf("%d%d",&u,&v);
addedge(u,v);
deg[u]++,deg[v]++;
u = findset(u),v = findset(v);
fa[u] = v;
}
int pd1 = 0,pd2 = 0,st=-1;
for(int i=0;i<=6;i++)if(deg[i]){st = i;break;}
for(int i=0;i<=6;i++)if(fa[i]==i&°[i])pd1++;
for(int i=0;i<=6;i++)if(deg[i]%2==1)pd2++,st = i;
if(pd1 == 1&&(pd2 == 0||pd2 ==2))
euler(st);
else printf("No solution\n");
}
return 0;
}