This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
Solution Plan:
Maybe just find an eulerian cycle then
go through the cycle, if you find a vertex that was
visited before, form a cycle with the vertices going back
to the last appearence of the vertex
*/
#include <bits/stdc++.h>
using namespace std;
int N, M;
vector<vector<pair<int, int>>> G;
vector<bool> used;
vector<int> path;
void dfs(int v) {
while (!G[v].empty()) {
auto [u, id] = G[v].back();
G[v].pop_back();
if (used[id]) continue;
used[id] = true;
dfs(u);
}
path.push_back(v);
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> N >> M;
G.resize(N);
used.assign(M, false);
for (int i = 0; i < M; i++) {
int v, u;
cin >> v >> u;
v--, u--;
G[v].push_back({u, i});
G[u].push_back({v, i});
}
dfs(0);
stack<int> S;
vector<vector<int>> ans;
vector<bool> seen(N);
for (auto v : path) {
if (seen[v]) {
vector<int> tour;
while (seen[v]) {
int u = S.top();
S.pop();
seen[u] = false;
tour.push_back(u);
}
ans.push_back(tour);
} else {
seen[v] = true;
}
S.push(v);
}
for (auto tour : ans) {
for (auto v : tour) cout << v+1 << ' ';
cout << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |