Pagini recente » Cod sursa (job #2181843) | Cod sursa (job #2396040) | Cod sursa (job #2277246) | Cod sursa (job #2396043) | Cod sursa (job #2392425)
#include <bits/stdc++.h>
using namespace std;
ifstream in("darb.in");
ofstream out("darb.out");
#define NMAX 100005
int n,dist[NMAX],maxim;
vector<int>g[NMAX];
queue <int>q;
void bfs(int poz){
q.push(poz);
while(!q.empty()){
int node=q.front();
q.pop();
for(auto y:g[node]){
if(!dist[y]){
dist[y]=dist[node]+1;
q.push(y);
maxim=max(maxim,dist[y]);
}
}
}
}
int main()
{
in>>n;
for(int i=1;i<n;i++){
int x,y;
in>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
bfs(1);
int node;
for(int i=1;i<=n;i++){
if(dist[i]==maxim)
node=i;
}
memset(dist,0,sizeof(dist));
while(!q.empty())
q.pop();
bfs(node);
out<<maxim+1;
return 0;
}