Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector<int> graf[100010];
  6. int veze[100010];
  7. int qu[100010];
  8. bool mark[100010];
  9. int q = 0;
  10.  
  11. void topolosko(int x)
  12. {
  13. //mark[x] = true;
  14. for(int i = 0; i < q; i++)
  15. {
  16.  
  17. for(int p = 0; p < graf[qu[i]].size(); p++)
  18. {
  19. int t = graf[qu[i]][p];
  20. veze[t]--;
  21. if(veze[t] == 0) qu[q++] = t;
  22. }
  23. }
  24. }
  25.  
  26. int main()
  27. {
  28. ios_base::sync_with_stdio(false);
  29. cin.tie(NULL);
  30.  
  31. int n, m;
  32. cin >> n >> m;
  33. for(int i = 0; i< m; i++)
  34. {
  35. int a, b;
  36. cin >> a >> b;
  37. graf[a].push_back(b);
  38. veze[b]++;
  39. }
  40. for(int i = 1; i <= n; i++)
  41. {
  42. if(veze[i] == 0) qu[q++] = i;
  43. }
  44. topolosko(qu[0]);
  45. for(int i=0; i < q; i++) cout << qu[i] << " ";
  46.  
  47.  
  48. return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement