Pagini recente » Cod sursa (job #1538676) | Cod sursa (job #2430977) | Cod sursa (job #327030) | Cod sursa (job #6212) | Cod sursa (job #630078)
Cod sursa(job #630078)
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stack>
#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, curent;
vector<int>::iterator it;
stack<int> S;
int vec[NMAX];
memset(vec, 0, sizeof(vec));
S.push(nod);
viz[nod] = 1;
while(!S.empty())
{
curent = S.top();
if(vec[curent])
{
S.pop();
sort(v[curent].begin(), v[curent].end(), cmp);
max = 0;
c = 1;
for(it=v[curent].begin();it!=v[curent].end();it++)
{
if(max < c + cost[*it])
{
max = c + cost[*it];
}
c++;
}
cost[curent] = max;
}
for(it=v[curent].begin();it!=v[curent].end();it++)
{
if(!viz[*it])
{
viz[*it] = 1;
S.push(*it);
}
}
vec[curent] = 1;
}
}
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;
}