SGU101

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 <algorithm>
#include <vector> using namespace std; #define maxn 105 int n,sp,len = ;
int fa[],deg[],first[],v1[ * maxn],next[ * maxn],ans1[maxn];
bool done[],vis[ * maxn];
bool flag = ;
char ans2[maxn]; int _find(int x) {
return x == fa[x] ? x : fa[x] = _find(fa[x]);
} void _union(int x,int y) {
int u = _find(x),v = _find(y); if(u != v) fa[u] = v;
}
void judge() {
int pos = ;
while(pos <= && !done[pos]) pos++; sp = pos; int now = fa[pos];
int odd = ;
for(int i = ; i <= ; i++) {
if(!done[i]) continue;
if(deg[i] % ) {
odd++;
sp = i;
}
int u = _find(i);
if(u != now) flag = ;
} if(odd > ) flag = ; } void output(int u) {
//printf("u = %d\n",u);
for(int e = first[u]; e != -; e = next[e]) { if(!vis[e]) {
vis[e] = ;
int t = (e % ) ? - : ;
vis[e + t] = ;
output(v1[e]); char c = t == - ? '-' : '+';
//printf("u = %d v = %d\n",u,v1[e]);
ans1[len] =e / + ;
ans2[len++] = c; }
} } void addedge(int a,int b,int id) {
int e = first[a];
next[id] = e;
first[a] = id;
v1[id] = b; }
void solve() { judge(); if(flag) {
output(sp);
for(int i = len - ; i >= ; i--)
printf("%d %c\n",ans1[i],ans2[i]);
}
else printf("No solution\n"); }
int main()
{ // freopen("sw.in","r",stdin);
scanf("%d",&n); //printf("n = %d\n",n); for(int i = ; i <= ; i++) {
fa[i] = i;
first[i] = -;
} for(int i = ; i < * n; i += ) {
int a,b;
scanf("%d%d",&a,&b);
done[a] = ;
done[b] = ;
_union(a,b);
deg[a]++;
deg[b]++;
addedge(a,b,i);
addedge(b,a,i + );
} solve(); //cout << "Hello world!" << endl;
return ;
}
上一篇:Android监听ScrollView滑动到顶端和底部


下一篇:[C/C++11]_[初级]_[std::bind介绍和使用]