Pagini recente » Cod sursa (job #2943346) | Cod sursa (job #107005) | Cod sursa (job #2144025) | Cod sursa (job #584340) | Cod sursa (job #2103108)
#include <bits/stdc++.h>
using namespace std;
ifstream in("darb.in");
ofstream out("darb.out");
const int nx=100002;
vector < int > v[nx];
bitset < nx > p;
int root;
int n,i,j;
int dist[nx];
void bfs (int start)
{
queue < int > q;
q.push(start);
dist[start]=1;
while(!q.empty())
{
int i=q.front();
q.pop();
for(vector < int > :: iterator it=v[i].begin(); it!=v[i].end(); it++)
if(dist[*it]==0)
{
dist[*it]=dist[i]+1;
q.push(*it);
}
}
}
int main()
{
in>>n;
for(int x=1; x<n; x++)
{
in>>i>>j;
p.set(j);
v[i].push_back(j);
v[j].push_back(i);
}
for(int i=1; i<=n; i++)
if(p.test(i)==0)
{
root=i;
break;
}
bfs(root);
root=0;
dist[root]=INT_MIN;
for(int i=1; i<=n; i++)
if(dist[i]>dist[root]) root=i;
memset(dist,0,sizeof(dist));
bfs(root);
root=0;
dist[root]=INT_MIN;
for(int i=1; i<=n; i++)
if(dist[i]>dist[root]) root=i;
out<<dist[root];
return 0;
}