Cod sursa(job #916149)

Utilizator balakraz94abcd efgh balakraz94 Data 15 martie 2013 21:28:01
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include<cstdio>
#include<vector>
#include<algorithm>
#define pb push_back
#define nxt (*it)
#define FOR(i,a,b)\
   for(int i=a; i<=b; ++i)
#define ALL(g)\
   for(typeof(g.begin()) it=g.begin(); it!=g.end(); ++it)
#define infile "zvon.in"
#define outfile "zvon.out"
#define nMax 100005
using namespace std;

vector < int > v[nMax];

int DP[nMax];

int N, M, T;

inline void init(){
	FOR(i,1,N){
		DP[i] = 0;
		v[i].clear();
	}
}

void read(){
	init();

	scanf("%d", &N);

	int x, y;
	FOR(i,1,N-1){
		scanf("%d %d", &x, &y);

		v[x].pb(y);
	}
}

bool cmp(const int &x, const int &y){
	return DP[x] > DP[y];
}

void DF(int x){
	ALL(v[x])
		DF(nxt);

	sort(v[x].begin(), v[x].end(), cmp);

	int step = 0;
	ALL(v[x])
		DP[x] = max(DP[x], DP[nxt] + (++ step));
}

int main(){
	freopen(infile, "r", stdin);
    freopen(outfile, "w", stdout);

	for(scanf("%d", &T); T; T--){
		read();

		DF(1);

		printf("%d\n", DP[1]);
	}

	fclose(stdin);
	fclose(stdout);

    return 0;
}