Cod sursa(job #108113)

Utilizator wefgefAndrei Grigorean wefgef Data 21 noiembrie 2007 17:06:25
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

#define pb push_back
#define sz(c) int((c).size())
#define rall(c) (c).rbegin(), (c).rend()
#define tr(it, c) for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)

const int Nmax = 100005;

int N;
vector<int> G[Nmax];
int Ret[Nmax];

inline int max(int a, int b) { return (a > b ? a : b); }

void Df(int k) {
	if (!sz(G[k])) {
		Ret[k] = 0;
		return;
	}
	vector<int> v;
	tr(it, G[k]) {
		Df(*it);
		v.pb(Ret[*it]);
	}
	sort(rall(v));
	Ret[k] = 0;
	for (int i = 0; i < sz(v); ++i)
		Ret[k] = max(Ret[k], v[i] + i + 1);
}

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

	int t;
	for (scanf("%d", &t); t; --t) {
		scanf("%d", &N);
		for (int i = 1; i <= N; ++i)
			G[i].clear();

		for (int i = 1; i < N; ++i) {
			int a, b;
			scanf("%d %d", &a, &b);
			G[a].pb(b);
		}
		Df(1);
		printf("%d\n", Ret[1]);
	}
}