Cod sursa(job #447505)

Utilizator andrei.sfrentSfrent Andrei andrei.sfrent Data 28 aprilie 2010 21:58:24
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <stdio.h>
#include <vector>
#include <algorithm>

using namespace std;

#define N 100000
#define FTYPE int

int n;
int timp[N + 1];
vector<int> a[N + 1];

void clear()
{
	for(int i = 1; i <= n; ++i) a[i].clear();
}

bool cmp(FTYPE a, FTYPE b)
{
	return a > b;
}

FTYPE f(int nod)
{
	if(a[nod].empty()) return 0;
	else
	{
		vector<FTYPE> sub;
		vector<FTYPE> :: iterator it;
		for(it = a[nod].begin(); it != a[nod].end(); ++it)
			sub.push_back(f(*it));

		sort(sub.begin(), sub.end(), cmp);		
		
		FTYPE max = -1, tmp;
		for(int i = 0; i < sub.size(); ++i) 
		{
			tmp = sub[i] + i + 1;
			if(tmp > max) max = tmp;
		}
		return max;
	}
}

void rezolva_test()
{
	scanf("%d", &n);

	if(n == 1)
	{
		printf("0\n");
		return;
	}

	int i, x, y;
	for(i = 1; i <= n - 1; ++i)
	{
		scanf("%d%d", &x, &y);
		a[x].push_back(y);
	}
	
	printf("%d\n", f(1));

	clear();
}

int main()
{
	freopen("zvon.in", "r", stdin);
	freopen("zvon.out", "w", stdout);

	int t;
	scanf("%d", &t);
	while(t--) rezolva_test();

	return 0;
}