Advertisement
Falak_Ahmed_Shakib

bfs wait 1

May 12th, 2020
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 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. void BFS(ll s)
  39. {
  40. vis[s]=true;
  41.  
  42. cost[s]=0;
  43.  
  44. queue<ll>q;
  45.  
  46. q.push(s);
  47.  
  48.  
  49. while(!q.empty())
  50. {
  51. ll u=q.front();
  52. q.pop();
  53.  
  54. for(auto x:v[u])
  55. {
  56. if(!vis[x])
  57. {
  58. cost[x]=cost[u]+1;
  59.  
  60. vis[x]=true;
  61. q.push(x);
  62.  
  63. }
  64.  
  65.  
  66. }
  67.  
  68. }
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87. void input()
  88. {
  89.  
  90. cin>>n>>m;
  91.  
  92. ll a,b;
  93. for(ll i=0;i<m;i++)
  94. {
  95. cin>>a>>b;
  96.  
  97. v[a].eb(b);
  98.  
  99. v[b].eb(a);
  100.  
  101. }
  102. }
  103.  
  104.  
  105. int main()
  106. {
  107. fastread();
  108.  
  109. input();
  110.  
  111.  
  112. ll s;
  113.  
  114.  
  115. // cin>>s;
  116.  
  117.  
  118.  
  119. BFS(1);
  120.  
  121.  
  122.  
  123. for(ll i=0;i<=n;i++)
  124. {
  125. cout<<i<<" "<<cost[i]<<endl;
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. }
  139. /*
  140.  
  141. 6 6
  142. 1 2
  143. 1 3
  144. 2 4
  145. 2 6
  146. 3 5
  147. 3 4
  148.  
  149. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement