-
Notifications
You must be signed in to change notification settings - Fork 3
/
F.cpp
117 lines (106 loc) · 3.32 KB
/
F.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
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m;
std::cin >> n >> m;
std::vector<std::vector<std::pair<int, int>>> adj(n);
for (int i = 0; i < m; i++) {
int u, v;
std::cin >> u >> v;
u--, v--;
adj[u].emplace_back(v, i);
adj[v].emplace_back(u, i);
}
std::vector<std::vector<std::pair<int, int>>> G(n);
auto check = [&](int r) -> bool {
std::vector<int> odd;
for (int i = 0; i < n; i++) {
if (G[i].size() % 2) {
odd.emplace_back(i);
}
}
if (odd.size() > 2) {
return false;
}
if (odd.size() == 2 && odd[0] != r && odd[1] != r) {
return false;
}
std::vector<bool> vis(n);
vis[r] = true;
std::queue<int> q;
q.emplace(r);
while (!q.empty()) {
auto u = q.front();
q.pop();
for (auto &[v, id] : G[u]) {
if (vis[v]) {
continue;
}
q.emplace(v);
vis[v] = true;
}
}
for (int i = 0; i < n; i++) {
if (!vis[i] && G[i].size() > 0) {
return false;
}
}
return true;
};
for (int i = 0; i < n; i++) {
std::set<int> S;
for (auto &[v, id] : adj[i]) {
if (adj[v].size() % 2) {
S.insert(v);
}
}
for (int j = 0; j <= adj[i].size(); j++) {
int x = -1;
if (j < adj[i].size()) {
x = adj[i][j].first;
}
G.assign(n, std::vector<std::pair<int, int>>());
for (int k = 0; k < n; k++) {
for (auto &[v, id] : adj[k]) {
if ((x == v && k == i) || (i == v && k == x) || !((S.count(k) && v == i) || (S.count(v) && k == i))) {
G[k].emplace_back(v, id);
}
}
}
if (check(i)) {
std::vector<bool> used(m);
std::vector<int> nxt(n), ans;
std::function<void(int)> dfs = [&](int u) -> void {
while (nxt[u] < G[u].size()) {
auto &[v, id] = G[u][nxt[u]];
nxt[u]++;
if (used[id]) {
continue;
}
used[id] = true;
dfs(v);
}
ans.emplace_back(u);
};
dfs(i);
ans.emplace_back(-2);
for (auto v : S) {
if (v == x) {
continue;
}
ans.emplace_back(v);
ans.emplace_back(i);
}
std::cout << ans.size() << "\n";
for (int i = 0; i < ans.size(); i++) {
std::cout << ans[i] + 1 << " \n"[i == ans.size() - 1];
}
return 0;
}
}
}
std::cout << 0 << "\n";
return 0;
}