## SGU 101 解题报告

http://acm.sgu.ru/problem.php?contest=0&problem=101

## 101. Domino

time limit per test: 0.50 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).

```5
1 2
2 4
2 4
6 4
2 1
```

```2 -
5 +
1 +
3 +
4 -
```

### CODE

```#include
#include
struct node
{
int x,y;
}ans[202],dom[202];
int k,D[7][7],deg[7];
void dfs(int s)
{
int i;
for(i=0;i<7;++i)
if(D[s][i])
{
--D[s][i];
--D[i][s];
dfs(i);
++k;
ans[k].x=s;
ans[k].y=i;
}
}
int main()
{
int i,j,n,x,y;
scanf("%d",&n);
memset(D,0,sizeof(D));
memset(deg,0,sizeof(deg));
for(i=1;i<=n;++i)
{
scanf("%d%d",&x,&y);
++D[x][y];
++D[y][x];
++deg[x];
++deg[y];
dom[i].x=x;
dom[i].y=y;
}
for(i=0;i<7;++i)
{
if(deg[i])
{
j=i;
break;
}
}
k=0;
for(i=0;i<7;++i)
{
if(deg[i]%2)
{
++k;
j=i;
}
}
if(!(k==0||k==2))
{
printf("No solutionn");
return 0;
}
k=0;
dfs(j);
if(k!=n)
{
printf("No solutionn");
return 0;
}
for(i=k;i>0;--i)
for(j=1;j<=n;++j)
if(ans[i].x==dom[j].x && ans[i].y==dom[j].y)
{
printf("%d +n",j);
dom[j].x=-1;
break;
}
else if(ans[i].x==dom[j].y && ans[i].y==dom[j].x)
{
printf("%d -n",j);
dom[j].x=-1;
break;
}
return 0;
}
```