This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> adj(n);
vector<array<int, 2>> edges;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u].push_back(i);
adj[v].push_back(i);
edges.push_back({u, v});
}
vector<vector<int>> tours;
vector<bool> visited(m);
vector<int> sBefore(n);
vector<int> v;
function<void(int)> Euler = [&](int head) {
while ((int) adj[head].size() > 0) {
auto u = adj[head].back();
adj[head].pop_back();
v.push_back(head);
sBefore[head] += 1;
if (!visited[u]) {
visited[u] = true;
int nxt = head ^ edges[u][0] ^ edges[u][1];
if (sBefore[nxt]) {
vector<int> cur;
int p = -1;
do {
cur.push_back(1 + v.back());
p = v.back();
sBefore[p] -= 1;
v.pop_back();
} while (p != nxt);
tours.push_back(cur);
}
Euler(nxt);
}
if ((int) v.size() > 0 && v.back() == head) {
v.pop_back();
sBefore[head] -= 1;
}
}
return ;
};
Euler(0);
for (int i = 0; i < (int) tours.size(); i++) {
for (int j = 0; j < (int) tours[i].size(); j++) {
cout << tours[i][j] << " \n"[j == (int) tours[i].size() - 1];
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |