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;
typedef pair<int,int> ii;
vector<vector<ii>> adj;
stack<int> res;
vector<int> vis, edge;
int dfs(int x){
vis[x] = 1;
while(!adj[x].empty()){
auto [viz, idx] = adj[x].back();
adj[x].pop_back();
if(edge[idx]) continue;
edge[idx] = 1;
if(vis[viz]){
res.push(x);
vis[x] = 0;
return viz;
}
int k = dfs(viz);
if(k == x){
res.push(x);
while(!res.empty()){
if(res.top() != x) cout << ' ';
cout << res.top();
res.pop();
}
cout << endl;
}
else{
res.push(x);
vis[x] = 0;
return k;
}
}
vis[x] = 0;
return 0;
}
int main(){
int n,m; cin >> n >> m;
adj.resize(n+2);
vis.assign(n+2, 0);
edge.assign(m+2,0);
for(int i=0; i<m; i++){
int a,b; cin >> a >> b;
adj[a].push_back({b,i});
adj[b].push_back({a,i});
}
for(int i=1; i<=n; i++){
if(!adj[i].empty()) dfs(i);
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |