Advertisement
kolek0002

Untitled

Jan 22nd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int const ext=1000004;
  4. int n, a, b, visits, out[ext];
  5. vector<int> graph[ext], cycle;
  6. bool visited[ext], debug=1;
  7.  
  8.  
  9. void find_cycle(int v, int u){
  10. int w=u;
  11. cycle.push_back(w);
  12. while(w!=v && w!=0){
  13. w=out[w];
  14. cycle.push_back(w);
  15. }
  16. if(cycle[cycle.size()-1]==0)
  17. cycle.pop_back();
  18. return;
  19. }
  20.  
  21.  
  22. void dfs(int v){
  23. visited[v]=true;
  24. visits++;
  25. for(int i=0; i<graph[v].size(); i++){
  26. if(visited[graph[v][i]] && out[graph[v][i]]!=v)
  27. find_cycle(v, graph[v][i]);
  28. if(!visited[graph[v][i]]){
  29. dfs(graph[v][i]);
  30. out[v]=graph[v][i];
  31. }
  32. }
  33. return;
  34. }
  35.  
  36. int main(){
  37. scanf("%d", &n);
  38. for(int i=1; i<n+1; i++){
  39. scanf("%d%d", &a, &b);
  40. graph[a].push_back(b);
  41. graph[b].push_back(a);
  42. }
  43. a=1;
  44. while(visits<n){
  45. while(visited[a])
  46. a++;
  47. dfs(a);
  48. while(cycle.size()>0){
  49. printf("%d ", cycle[cycle.size()-1]);
  50. cycle.pop_back();
  51. }
  52. printf("\n");
  53. }
  54. return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement