Pagini recente » Cod sursa (job #386239) | Cod sursa (job #2956229) | Cod sursa (job #196679) | Cod sursa (job #1548728) | Cod sursa (job #129299)
Cod sursa(job #129299)
/*
infoarena, Happy Coding 2007
Zvon
*/
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const char iname[] = "zvon.in";
const char oname[] = "zvon.out";
#define MAXN 100005
vector <int> G[MAXN], V;
int zvon[MAXN];
bool mycmp(int a, int b) {
return a > b;
}
void DF(int n)
{
vector <int>::iterator it;
for (it = G[n].begin(); it != G[n].end(); ++ it)
DF(*it);
for (it = G[n].begin(); it != G[n].end(); ++ it)
V.push_back(zvon[*it]);
sort(V.begin(), V.end(), mycmp);
for (size_t i = 0; i < V.size(); ++ i)
if (zvon[n] < V[i] + (i + 1))
zvon[n] = V[i] + (i + 1);
vector <int> ().swap(V);
}
int main(void)
{
freopen(iname, "r", stdin);
freopen(oname, "w", stdout);
int T;
int n;
int x, y;
for (scanf("%d", &T); T > 0; -- T)
{
scanf("%d", &n);
for (int i = 1; i < n; ++ i)
{
scanf("%d %d", &x, &y);
G[x].push_back(y);
}
DF(1);
printf("%d\n", zvon[1]);
/* Curata */
memset(zvon, 0, sizeof(zvon));
for (int i = 1; i <= n; ++ i)
vector <int> ().swap(G[i]);
}
return 0;
}