이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int MAXN = 5e5+10;
vector<pair<int,int>> adj[MAXN]; //for knowing whether we visited this edge before, we save the edge index
vector<bool> visedge, vis;
vector<int> cyc;
int cur;
void dfs(int u){
vis[u] = true; cyc.push_back(u);
while(!adj[u].empty()){
while(!adj[u].empty() && visedge[adj[u].back().s]){
adj[u].pop_back();
}
if(adj[u].empty()){ //give up -> this one won't be added in the cycle!
cyc.pop_back();
return;
}
int v = adj[u].back().f;
visedge[adj[u].back().s] = true;
if(vis[v]){
cur = v;
cout << v << " ";
while(cyc.back() != v){
cout << cyc.back() << " ";
vis[cyc.back()] = false; cyc.pop_back();
}
cout << "\n";
}
else{
dfs(v);
}
if(u != cur){
//AHHHHHH (we cannot continue here)
return;
}
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N,M; cin >> N >> M;
visedge = vector<bool>(M,false), vis = vector<bool>(N+1,false);
for(int i = 0;i<M;++i){
int u,v; cin >> u >> v;
adj[u].push_back({v,i}); adj[v].push_back({u,i});
}
for(int i = 1;i<=N;++i){ //is this necessary? Or can we just do it in one? It is necessary! Otherwise we might get stuck
dfs(i);
}
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... |