#include <bits/stdc++.h>
using namespace std;
int n, m;
bool flag;
vector<vector<int>> adj;
vector<vector<int>> ans;
vector<int> st;
stack<int> path;
set<pair<int, int>> used;
void dfs(int u, int p) {
if (flag) return;
st[u] = 1;
path.push(u);
for (int v: adj[u]) {
if (flag) return;
if (v == p) continue;
if (used.find({u, v}) != used.end()) continue;
if (st[v] == 1) { // loop
vector<int> aux;
aux.push_back(v);
int prev = v;
while (!path.empty() and path.top() != v) {
int w = path.top();
// cout << w+1 << '\n';
used.insert({prev, w});
used.insert({w, prev});
aux.push_back(w);
path.pop();
prev = w;
}
used.insert({prev, v});
used.insert({v, prev});
ans.push_back(aux);
flag = true;
return;
} else if (st[v] == 0) {
dfs(v, u);
}
}
if (flag) return;
st[u] = 2;
path.pop();
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
adj = vector<vector<int>>(n);
int u, v;
for (int i = 0; i < m; i++) {
cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
// 0 still not processed
// 1 in dfs
// 2 processed
for (int i = 0; i < 5*n; i++) {
if ((int)used.size() == 2*m) {
break;
}
st = vector<int>(n, 0);
path = stack<int>();
flag = false;
dfs(i%n, -1);
// until all edges used
}
for (int i = 0; i < (int)ans.size(); i++) {
for (int j = 0; j < (int)ans[i].size(); j++) {
cout << ans[i][j]+1 << ' ';
}
cout << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
56 ms |
784 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
24 ms |
724 KB |
Output is correct |
7 |
Incorrect |
151 ms |
2080 KB |
Some edges were not used |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
48 ms |
700 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
28 ms |
796 KB |
Output is correct |
7 |
Incorrect |
146 ms |
2164 KB |
Some edges were not used |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
49 ms |
780 KB |
Output is correct |
5 |
Correct |
3 ms |
340 KB |
Output is correct |
6 |
Correct |
23 ms |
768 KB |
Output is correct |
7 |
Incorrect |
151 ms |
2040 KB |
Some edges were not used |
8 |
Halted |
0 ms |
0 KB |
- |