Pagini recente » Rating Margarit Ioan (LostDelmu) | Cod sursa (job #1609032) | Cod sursa (job #2357318) | Cod sursa (job #918667) | Cod sursa (job #1585201)
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;
const int Mn = 1e5 + 6;
int n,vert,sol,pos = 0;;
char buff[Mn];
vector< int > g[Mn];
void read(int &num)
{
num = 0;
char sign = '+';
while (!isdigit(buff[pos]))
{
sign = buff[pos];
if (++pos == Mn)
fread(buff,1,Mn,stdin),pos = 0;
}
while (isdigit(buff[pos]))
{
num = num * 10 + buff[pos] - '0';
if (++pos == Mn)
fread(buff,1,Mn,stdin),pos = 0;
}
if (sign == '-')
num *= -1;
}
void dfs(int node,int parent,int depth)
{
if (depth > sol)
vert = node,sol = depth;
for (int i = 0;i < g[node].size();i++)
if (g[node][i] != parent)
dfs(g[node][i],node,depth + 1);
}
int main()
{
freopen("darb.in","r",stdin);
freopen("darb.out","w",stdout);
read(n);
for (int i = 1;i <= n;i++)
{
int x,y;
read(x);
read(y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1,0,1);
dfs(vert,0,1);
printf("%d\n",sol);
return 0;
}