-
Notifications
You must be signed in to change notification settings - Fork 4
/
101.cpp
75 lines (73 loc) · 1.57 KB
/
101.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
*Name: Domino
*LANG: C++
*Source: sgu101
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <fstream>
using namespace std;
struct node{
int v,next;
}e[250];
int v[10],t[10],pre[250];
bool visit[250];
int path[250],pathNum;
int n,x,y,edgeNum;
void insert(int a,int b){
e[++edgeNum].v=b;e[edgeNum].next=v[a];v[a]=edgeNum;t[a]++;t[b]++;
e[++edgeNum].v=a;e[edgeNum].next=v[b];v[b]=edgeNum;t[a]++;t[b]++;
}
void dfs(int pos){
if (t[pos]==0) {path[++pathNum]=pos;return;}
int j=v[pos];
while (j!=0){
if (visit[(j+1)/2]){
visit[(j+1)/2]=false;
t[pos]--;t[e[j].v]--;
dfs(e[j].v);
}
j=e[j].next;
}
path[++pathNum]=pos;
}
int main(){
//input
scanf("%d",&n);
for (int i=1;i<=n;++i){
scanf("%d %d",&x,&y);
insert(x,y);
}
//solve
int tmp=0;int pos=-1,pos1=-1;
for (int i=0;i<=6;++i) t[i]/=2;
for (int i=6;i>=0;--i)
if (t[i] % 2==1) {tmp++;if (t[i]!=0) if (pos==-1) pos=i; else pos1=-1;}
if (pos==-1)
for (int i=0;i<=6;++i)
if (t[i]!=0) {pos=i;break;}
if (tmp!=2 && tmp!=0){
printf("%s\n","No solution");
return 0;
}
else{
memset(visit,true,sizeof(visit));
dfs(pos);
}
//print
memset(visit,true,sizeof(visit));
if (pathNum!=(n+1)) {printf("%s\n","No solution");return 0;}
for (int i=1;i<pathNum;++i){
int j=v[path[i]];
while (j!=0 && (e[j].v!=path[i+1] || visit[(j+1)/2]==false))
j=e[j].next;
visit[(j+1)/2]=false;
if ((j+1) % 2==0)
printf("%d %c\n",(j+1)/2,'+');
else
printf("%d %c\n",(j+1)/2,'-');
}
return 0;
}