Guest User

Untitled

a guest
Nov 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream f("BFS.in");
  6. ofstream g("BFS.out");
  7.  
  8. const int DMAX = 100005;
  9. unsigned int n, m;
  10. int distanta[DMAX];
  11.  
  12. vector <int> muchii[DMAX];
  13. queue <int> coada;
  14.  
  15. void BFS()
  16. {
  17. int nod, vecin;
  18. while (!coada.empty())
  19. {
  20. nod=coada.front();
  21. coada.pop();
  22. for (int i=0; i<muchii[nod].size(); i++)
  23. {
  24. vecin=muchii[nod][i];
  25. g<<vecin<<' ';
  26. if (distanta[vecin]==-1)
  27. {
  28. coada.push(vecin);
  29. distanta[vecin]=distanta[nod] + 1;
  30. }
  31. }
  32. }
  33. }
  34.  
  35. void read()
  36. {
  37. int x;
  38. f>>n>>m>>x;
  39. for (int i=1; i<=m; i++)
  40. {
  41. int x, y;
  42. f>>x>>y;
  43. muchii[x].push_back(y);
  44. }
  45.  
  46. for (int i = 1; i <= n; i++)
  47. distanta[i]=-1;
  48. distanta[x]=0;
  49. coada.push(x);
  50. BFS();
  51. }
  52.  
  53. int main()
  54. {
  55. read();
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment