Advertisement
porosh45

UVa 10305

Oct 6th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. vector <int > vec[105]; stack<int > s;
  4. int vis[105];
  5. void dfs(int a)
  6. {
  7. if(vis[a]==1)
  8. return;
  9.  
  10. // if(vis[a]==1)
  11. // {
  12. // cout<<"Circle found"<<endl;
  13. // return;
  14. // }
  15.  
  16. vis[a]=1;
  17. for(int i = 0 ; i < vec[a].size();i++)
  18. {
  19. dfs(vec[a][i]);
  20. }
  21. //vis[a] = 2;
  22. s.push(a);
  23. }
  24. int main()
  25. {
  26. int n,e;
  27. while(cin>>n>>e && n && e)
  28. {
  29.  
  30. for(int i = 1;i<=e;i++)
  31. {
  32. int a,b;
  33. cin>>a>>b;
  34. vec[a].push_back(b);
  35. }
  36.  
  37. for(int i = 1;i<=n;i++)
  38. dfs(i);
  39.  
  40. while(!s.empty())
  41. {
  42. int u = s.top();
  43. cout<<u<<" ";
  44. s.pop();
  45. }
  46.  
  47. cout<<endl;
  48. }
  49. return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement