Cod sursa(job #99005)

Utilizator eferLiviu Ciortea efer Data 10 noiembrie 2007 20:05:57
Problema Zvon Scor 100
Compilator cpp Status done
Runda Happy Coding 2007 Marime 1.67 kb
// Liviu Ciortea, Bucuresti, Romania (TopCoder handle: _efer_)

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <utility>
#include <string>
using namespace std;

#define REP(i, N) for (int i = 0; i < (N); ++i)
#define REPV(i, a, b) for (int i = (a); i <= (b); ++i)
#define REPD(i, N) for (int i = (N)-1; i >= 0; --i)
#define REPVD(i, b, a) for (int i = (b); i >= (a); --i)
#define REPIT(it, v) for (it = (v).begin(); it != (v).end(); ++it)
#define SZ(a) ((int)(a).size())
#define MP make_pair
#define PB push_back
#define X first
#define Y second
#define ALL(a) (a).begin(), (a).end()
#define CLR(a) memset((a), 0, sizeof(a))
#define MSET(a, v) memset((a), v, sizeof(a))
#define CPY(dest, source) memcpy(dest, source, sizeof(dest))

typedef long long LL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef pair<int, int> PII;

const int MAXN = 102400;
VI adj[MAXN];

int go(int n, int paps) {
	int res = 0;
	VI times;
	VI::iterator it;
	REPIT(it, adj[n]) {
		if (*it == paps) continue;
		times.PB(go(*it, n));
	}
	sort(ALL(times));
	int S = SZ(times);
	REPV(i, 1, S) res = max(res, i + times[S-i]);
	return res;
}

int main() {
	freopen("zvon.in", "rt", stdin);
	freopen("zvon.out", "wt", stdout);

	int T;
	scanf("%d", &T);

	REP(test, T) {
		int N, a, b;
		scanf("%d", &N);
		REP(i, N) adj[i].clear();
		REP(i, N-1) {
			scanf("%d %d", &a, &b);
			a--, b--;
			adj[a].PB(b);
		}

		int res = go(0, -1);
		printf("%d\n", res);
	}

	return 0;
}