Advertisement
HmHimu

TOPO

Jul 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1.  
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int v;
  6. int adj[100][100];
  7. char color[100];
  8. stack<int> s;
  9.  
  10. void visit_dfs(int u)
  11. {
  12. color[u]='g';
  13.  
  14. for(int i=1;i<=v;i++)
  15. {
  16. if(adj[u][i]==1 && color[i]=='w')
  17. {
  18. visit_dfs(i);
  19. }
  20. }
  21.  
  22. color[u]='b';
  23. s.push(u);
  24.  
  25. }
  26. void dfs()
  27. {
  28. for(int i=1;i<=v;i++)
  29. {
  30. color[i]='w';
  31. }
  32.  
  33. for(int i=1;i<=v;i++)
  34. {
  35. if(color[i]=='w')
  36. {
  37. visit_dfs(i);
  38. }
  39. }
  40. }
  41. int main()
  42. {
  43. int a,b;
  44.  
  45. cout<<" Enter number of vartex : ";
  46. cin>>v;
  47.  
  48. while(cin>>a>>b && a!=0 && b!=0)
  49. {
  50. adj[a][b]=1;
  51. }
  52.  
  53. dfs();
  54.  
  55. while(!s.empty())
  56. {
  57. cout<<s.top()<<" ";
  58. s.pop();
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement