Pagini recente » Cod sursa (job #1652850) | Cod sursa (job #2587644) | Cod sursa (job #3143603) | Cod sursa (job #686699) | Cod sursa (job #3209149)
#include <bits/stdc++.h>
using namespace std;
ifstream f("darb.in");
ofstream g("darb.out");
const int NMAX=100005;
int n,x,y,last,ans;
vector<int>dist,graph[NMAX];
queue<int>q;
void bfs(int node)
{
dist[node]=1;
q.push(node);
while(!q.empty())
{
node=q.front();
q.pop();
for(int next:graph[node])
if(!dist[next])
{
dist[next]=dist[node]+1;
q.push(next);
if(dist[next]>ans)
{
ans=dist[next];
last=next;
}
}
}
}
int main()
{
f>>n;
dist.resize(n+1);
for(int i=1;i<n;i++)
{
f>>x>>y;
graph[x].push_back(y);
graph[y].push_back(x);
}
bfs(1);
fill(dist.begin(),dist.end(),0);
bfs(last);
g<<ans;
return 0;
}