Pagini recente » Cod sursa (job #2922654) | Cod sursa (job #1263752) | Cod sursa (job #1387875) | Cod sursa (job #1333844) | Cod sursa (job #1585212)
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;
const int Mn = 1e5 + 6;
int n,vert,sol,cnt,pos;
char buff[Mn];
bool used[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)
{
used[node] = 1;
cnt++;
if (cnt > sol)
vert = node,sol = cnt;
for (int i = 0;i < g[node].size();i++)
if (!used[g[node][i]])
dfs(g[node][i]);
cnt--;
}
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);
memset(used,0,sizeof(used));
dfs(vert);
printf("%d\n",sol);
return 0;
}