-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhierholzer.cpp
132 lines (100 loc) · 2.49 KB
/
hierholzer.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// BOJ 1199 오일러 회로
#include <bits/stdc++.h>
#define sz size()
#define bk back()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
void dfs(int cur, vector<int> &edges, vector<vector<int>> &graph, vector<bool> &used) {
while (graph[cur].sz) {
int idx = graph[cur].bk;
graph[cur].pop_back();
if (used[idx])
continue;
used[idx] = true;
int nxt = edges[idx] ^ cur;
dfs(nxt, edges, graph, used);
}
cout << cur << ' ';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<int> edges;
vector<vector<int>> graph(n + 1);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
int x;
cin >> x;
if (i > j)
continue;
while (x--) {
graph[i].push_back(edges.sz);
graph[j].push_back(edges.sz);
edges.push_back(i ^ j);
}
}
}
for (int i = 1; i <= n; i++) {
if (graph[i].sz & 1) {
cout << -1;
return 0;
}
}
vector<bool> used(edges.sz);
dfs(1, edges, graph, used);
}
//--------------------------------------------------------------------------------
#include <bits/stdc++.h>
#define sz size()
#define bk back()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
void dfs(int cur, vector<vector<int>> &graph) {
for (int nxt = 1; nxt < graph.sz; nxt++) {
int k = 0;
while (graph[cur][nxt] > 2) {
graph[cur][nxt] -= 2;
graph[nxt][cur] -= 2;
k++;
}
if (graph[cur][nxt]) {
graph[cur][nxt]--;
graph[nxt][cur]--;
dfs(nxt, graph);
cout << nxt << ' ';
while (k--)
cout << cur << ' ' << nxt << ' ';
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<vector<int>> graph(n + 1, vector<int>(n + 1));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> graph[i][j];
for (int i = 1; i <= n; i++) {
int degree = 0;
for (int j = 1; j <= n; j++)
degree += graph[i][j];
if (degree & 1) {
cout << -1;
return 0;
}
}
dfs(1, graph);
cout << 1;
}