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<set<int>> adj;
stack<int> res;
vector<int> vis;
int dfs(int x){
vis[x] = 1;
while(!adj[x].empty()){
int viz = *adj[x].begin();
adj[x].erase(viz);
adj[viz].erase(x);
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);
for(int i=0; i<m; i++){
int a,b; cin >> a >> b;
adj[a].insert(b);
adj[b].insert(a);
}
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... |