Advertisement
Guest User

SAVA APASA AICI

a guest
Jan 15th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int viz[100001];
  6. int N, D[100001], M;
  7. queue <int> Q;
  8. vector <int> L[100001];
  9.  
  10. ifstream fin("ascensiune.in");
  11. ofstream fout("ascensiune.out");
  12.  
  13. void BFS(int K)
  14. {
  15. int i, Elem;
  16. viz[K] = 1;
  17. D[K] = 1;
  18. while(!Q.empty())
  19. {
  20. K = Q.front();
  21. Q.pop();
  22. for(auto i : L[K])
  23. {
  24. if(!viz[i])
  25. {
  26. Q.push(i);
  27. viz[i] = 1;
  28. D[i] = 1 + D[K];
  29. }
  30. }
  31. }
  32. }
  33.  
  34. int main()
  35. {
  36. int i, Elem;
  37. fin >> N;
  38. for(i = 1 ; i <= N - 1 ; ++i)
  39. {
  40. int x, y;
  41. fin >> x >> y;
  42. L[x].push_back(y);
  43. L[y].push_back(x);
  44. }
  45. Q.push(1);
  46. BFS(1);
  47. int Max = 0;
  48. for(i = 1 ; i <= N ; ++i)
  49. {
  50. if(D[i] > Max)
  51. {
  52. Max = D[i];
  53. Elem = i;
  54. }
  55. }
  56. for(i = 1 ; i <= N ; ++i)
  57. viz[i] = 0;
  58.  
  59. Q.push(Elem);
  60. BFS(Elem);
  61. for(i = 1 ; i <= N ; ++i)
  62. if(D[i] > Max) Max = D[i];
  63. fout << Max;
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement