Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <queue>
  6.  
  7. using namespace std;
  8.  
  9. ifstream fin("iepuri1.in");
  10. ofstream fout("iepuri1.out");
  11.  
  12. const int MOD = 30011;
  13. const int DIM = 105;
  14.  
  15. vector <int> G[DIM];
  16.  
  17. int n,k,Root,x,y,Nr[DIM],Dist[DIM];
  18.  
  19. void Read()
  20. {
  21. fin>>n>>k;
  22. for(int i=1;i<=n-1;i++)
  23. {
  24. fin>>x>>y;
  25. G[x].push_back(y);
  26. Nr[y]++;
  27. }
  28. }
  29.  
  30. void GetRoot()
  31. {
  32. for(int i=1;i<=n;i++)
  33. {
  34. if(!Nr[i])
  35. {
  36. Root=i;
  37. break;
  38. }
  39. }
  40. }
  41.  
  42. queue <int> Q;
  43.  
  44. void BFS(int node)
  45. {
  46. Q.push(node);
  47. Dist[node]=1;
  48. while(!Q.empty)
  49. {
  50. int nod=Q.front();
  51. vector <int> ::iterator it;
  52. for(it=G[nod].begin();it!=G[nod].end();it++)
  53. {
  54. if(!Dist[*it])
  55. {
  56. Dist[*it]=Dist[node]+1;
  57. Q.push(*it);
  58. }
  59. }
  60. Q.pop();
  61. }
  62. }
  63.  
  64. int main()
  65. {
  66. Read();
  67. GetRoot();
  68. BFS(Root);
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement