Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int viz[100001];
- int N, D[100001], M;
- queue <int> Q;
- vector <int> L[100001];
- ifstream fin("ascensiune.in");
- ofstream fout("ascensiune.out");
- void BFS(int K)
- {
- int i, Elem;
- viz[K] = 1;
- D[K] = 1;
- while(!Q.empty())
- {
- K = Q.front();
- Q.pop();
- for(auto i : L[K])
- {
- if(!viz[i])
- {
- Q.push(i);
- viz[i] = 1;
- D[i] = 1 + D[K];
- }
- }
- }
- }
- int main()
- {
- int i, Elem;
- fin >> N;
- for(i = 1 ; i <= N - 1 ; ++i)
- {
- int x, y;
- fin >> x >> y;
- L[x].push_back(y);
- L[y].push_back(x);
- }
- Q.push(1);
- BFS(1);
- int Max = 0;
- for(i = 1 ; i <= N ; ++i)
- {
- if(D[i] > Max)
- {
- Max = D[i];
- Elem = i;
- }
- }
- for(i = 1 ; i <= N ; ++i)
- viz[i] = 0;
- Q.push(Elem);
- BFS(Elem);
- for(i = 1 ; i <= N ; ++i)
- if(D[i] > Max) Max = D[i];
- fout << Max;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement