Pagini recente » Cod sursa (job #710263) | Cod sursa (job #2082450) | Cod sursa (job #262946) | Cod sursa (job #988092) | Cod sursa (job #1103991)
#include <fstream>
#include <vector>
#include <queue>
#define NMAX 100005
using namespace std;
FILE* f=freopen("darb.in","r",stdin);
FILE* o=freopen("darb.out","w",stdout);
vector<int> graph[NMAX];
bool vis[NMAX];
int depth[NMAX];
int n;
int BFS(int x)
{
queue<int> q;
q.push(x);
vis[x]=1;
int r;
while(!q.empty())
{
x=q.front();
q.pop();
r=x;
for(int i=0;i<graph[x].size();++i)
{
int ind=graph[x][i];
if(!vis[ind])
{
vis[ind]=1;
q.push(ind);
}
}
}
return r;
}
int dmax;
void DFS(int x)
{
dmax=(dmax>depth[x])?dmax:depth[x];
for(int i=0;i<graph[x].size();++i) {
if(!depth[graph[x][i]])
{
depth[graph[x][i]]=depth[x]+1;
DFS(graph[x][i]);
}
}
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n-1;++i)
{
int x,y;
scanf("%d%d",&x,&y);
graph[x].push_back(y);
graph[y].push_back(x);
}
int x=BFS(1);
depth[x]=1;
DFS(x);
printf("%d",dmax);
return 0;
}