forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101skatou.cpp
More file actions
111 lines (103 loc) · 2.24 KB
/
101skatou.cpp
File metadata and controls
111 lines (103 loc) · 2.24 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//ac
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <list>
#include <sstream>
#include <iterator>
using namespace std;
#define MAXN 10
void find_path_u(int n,int mat[][MAXN],int now,int& step,int* path){
int i;
for (i=n-1;i>=0;i--)
while (mat[now][i]){
mat[now][i]--,mat[i][now]--;
find_path_u(n,mat,i,step,path);
}
path[step++]=now;
}
void find_path_d(int n,int mat[][MAXN],int now,int& step,int* path){
int i;
for (i=n-1;i>=0;i--)
while (mat[now][i]){
mat[now][i]--;
find_path_d(n,mat,i,step,path);
}
path[step++]=now;
}
int euclid_path(int n,int mat[][MAXN],int start,int* path){
int ret=0;
find_path_u(n,mat,start,ret,path);
// find_path_d(n,mat,start,ret,path);
return ret;
}
int n;
int mat[10][10];
map<pair<int, int>, deque<int> > M;
int path[1000];
int degree[10];
int visit[10];
void dfs(int now) {
visit[now] = 1;
for (int i = 0; i < 7; ++i) if (!visit[i] && mat[now][i])
dfs(i);
}
int main() {
while (scanf("%d", &n) != EOF) {
M.clear();
memset(mat, 0, sizeof(mat));
memset(degree, 0, sizeof(degree));
for (int i = 0; i < 7; ++i)
visit[i] = 1;
for (int i = 1; i <= n; ++i) {
int a, b;
scanf("%d%d", &a, &b);
mat[a][b]++; mat[b][a]++;
degree[a]++; degree[b]++;
visit[a] = visit[b] = 0;
pair<int, int> u(a, b);
M[u].push_back(i);
}
int start = 0;
for (int i = 0; i < 7; ++i) if (!visit[i]) {
start = i;
dfs(i); break;
}
int flag = 0;
for (int i = 0; i < 7; ++i) if (!visit[i]) {
flag = 1; break;
}
if (flag) {
printf("No solution\n");
continue;
}
int oddCnt = 0;
for (int i = 0; i < 7; ++i) if (degree[i] & 1)
oddCnt++, start = i;
if (!(oddCnt == 0 || oddCnt == 2)) {
printf("No solution\n");
continue;
}
int ret = euclid_path(7, mat, start, path);
for (int i = 1; i < ret; ++i) {
pair<int, int> u(path[i - 1], path[i]), v(path[i], path[i - 1]);
if (M[u].size() > 0) {
int num = M[u].front(); M[u].pop_front();
printf("%d +\n", num);
} else if (M[v].size() > 0) {
int num = M[v].front(); M[v].pop_front();
printf("%d -\n", num);
}
}
}
return 0;
}