Pagini recente » Cod sursa (job #78348) | Cod sursa (job #1575690) | Cod sursa (job #828670) | Cod sursa (job #2094808) | Cod sursa (job #587793)
Cod sursa(job #587793)
#include<algorithm>
#include<vector>
#include<stdio.h>
#define pb push_back
using namespace std;
vector<int> g[101010];
int best[101010],T;
int x,y,N;
bool cmp(int a,int b)
{
return best[a]>best[b];
}
void df(int x)
{
//printf("%d",x);
int ok=1;
for(int i=0;i<g[x].size();++i)
{
ok=0;
df(g[x][i]);
}
if(ok)
{
best[x]=0;
}
else
{
sort(g[x].begin(),g[x].end(),cmp);
int timp=1;
for(int i=0;i<g[x].size();++i)
{
if(best[g[x][i]]+timp>best[x])
best[x]=best[g[x][i]]+timp;
++timp;
}
}
}
int main()
{
freopen("zvon.in","r",stdin);
freopen("zvon.out","w",stdout);
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
for(int i=1;i<N;++i)
{
scanf("%d%d",&x,&y);
g[x].pb(y);
}
df(1);
printf("%d\n",best[1]);
for(int i=1;i<=N;++i)
{
best[i]=0;
g[i].clear();
}
}
return 0;
}