Advertisement
Falak_Ahmed_Shakib

top_BFS();

Oct 23rd, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. // In the name of Allah.
  2. // We're nothing and you're everything.
  3. // Ya Ali!
  4. /*
  5. , \ / ,
  6. / \ )\__/( / \
  7. / \ (_\ /_) / \
  8. ____/_____\__\@ @/___/_____\____
  9. | |\../| |
  10. | \VV/ |
  11. | ------___------- |
  12. |__________Chuta Dragon___________|
  13. | /\ / \\ \ /\ |
  14. | / V )) V \ |
  15. |/ ` // ' \|
  16. ` V '
  17. */
  18.  
  19. #include<bits/stdc++.h>
  20. using namespace std;
  21. typedef long long ll;
  22. typedef pair<ll,ll> pll;
  23. #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL))
  24. #define fi first
  25. #define se second
  26. #define pb push_back
  27. ll const MOD=1000000007;
  28. const int M= 2e5 + 10;
  29. ///-------------------------------------------------------------------------------------------------///
  30. /// KARMA IS LIKE 69,,, YOU GET WHAT YOU GIVE ///
  31. ///-------------------------------------------------------------------------------------------------///
  32. #define eb emplace_back
  33.  
  34. vector<ll>v[M+10];
  35. ll n,m;
  36. bool vis[M+10];
  37. ll cost[M+1];
  38. queue<ll>q;
  39. vector<ll>ans;
  40. void top_BFS()
  41. {
  42.  
  43.  
  44.  
  45.  
  46. while(!q.empty())
  47. {
  48. ll u=q.front();
  49. q.pop();
  50. //cout<<u<<endl;
  51. ans.push_back(u);
  52. for(auto x:v[u])
  53. {
  54. cost[x]--;
  55. if(!cost[x])
  56. {
  57.  
  58. q.push(x);
  59.  
  60. }
  61.  
  62.  
  63. }
  64.  
  65. }
  66.  
  67.  
  68. }
  69.  
  70.  
  71. void input()
  72. {
  73.  
  74. cin>>n>>m;
  75.  
  76. ll a,b;
  77. for(ll i=0;i<m;i++)
  78. {
  79. cin>>a>>b;
  80.  
  81. v[a].eb(b);
  82.  
  83. cost[b]++;
  84.  
  85. }
  86. }
  87.  
  88.  
  89. int main()
  90. {
  91. fastread();
  92.  
  93. input();
  94.  
  95.  
  96. for(ll i=1;i<=n;i++)
  97. {
  98. if(!cost[i])q.push(i);
  99.  
  100. }
  101.  
  102.  
  103.  
  104. top_BFS();
  105.  
  106.  
  107.  
  108. if(ans.size()==n)
  109. {
  110. for(auto x:ans)cout<<x<<endl;
  111.  
  112.  
  113. }else cout<<"IMPOSSIBLE"<<endl;
  114.  
  115.  
  116.  
  117.  
  118. }
  119. /*
  120.  
  121. 6 6
  122. 1 2
  123. 1 3
  124. 2 4
  125. 2 6
  126. 3 5
  127. 3 4
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement