Pagini recente » Cod sursa (job #2754622) | Cod sursa (job #2423794) | Cod sursa (job #1500771) | Cod sursa (job #2975763) | Cod sursa (job #627806)
Cod sursa(job #627806)
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#define NMAX 100001
using namespace std;
int cost[NMAX], viz[NMAX];
int N, T;
bool cmp(int a, int b)
{
if(cost[a] > cost[b])
{
return 1;
}
return 0;
}
void mark(vector< vector<int> > &v, int nod)
{
int max, c, s;
vector<int>::iterator it;
if(v[nod].empty())
{
cost[nod] = 0;
return;
}
for(it=v[nod].begin();it!=v[nod].end();it++)
{
if(!viz[*it])
{
viz[*it] = 1;
mark(v, *it);
}
}
sort(v[nod].begin(), v[nod].end(), cmp);
max = 0;
c = 1;
for(it=v[nod].begin();it!=v[nod].end();it++)
{
if(max < c + cost[*it])
{
max = c + cost[*it];
}
c++;
}
cost[nod] = max;
}
void solve()
{
vector< vector<int> > v(NMAX);
int i, x, y;
scanf("%d", &N);
for(i=0;i<N-1;++i)
{
scanf("%d %d", &x, &y);
v[x].push_back(y);
}
memset(cost, 0, sizeof(cost));
memset(viz, 0, sizeof(viz));
mark(v, 1);
}
int main()
{
freopen("zvon.in", "r", stdin);
freopen("zvon.out", "w", stdout);
scanf("%d", &T);
while(T--)
{
solve();
printf("%d\n", cost[1]);
}
return 0;
}