Pagini recente » Cod sursa (job #135733) | Cod sursa (job #88673) | Cod sursa (job #2467136) | Cod sursa (job #2205865) | Cod sursa (job #2442193)
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int t, n, i, j, sol[100010];
vector<int> graph[100010];
struct comp{
bool operator()(int x, int y){
return x>=y;
}
} c;
void dfs(int node);
int main()
{
freopen("zvon.in", "r", stdin);
freopen("zvon.out", "w", stdout);
scanf("%d", &t);
while(t--){
scanf("%d", &n);
for(i=1; i<=n; ++i) {graph[i].clear(); sol[i]=0;}
for(i=1; i<n; ++i){
int x, y;
scanf("%d%d", &x, &y);
graph[x].push_back(y);
}
dfs(1);
printf("%d\n", sol[1]);
}
return 0;
}
void dfs(int node){
int i;
for(i=0; i<graph[node].size(); ++i)
{dfs(graph[node][i]); graph[node][i]=sol[graph[node][i]];}
sort(graph[node].begin(), graph[node].end(), c);
int val, mx=0;
if(node==1) val=0; else val=1;
for(i=0; i<graph[node].size(); ++i) {graph[node][i]+=i; mx=max(mx, graph[node][i]);}
val+=mx;
sol[node]=val;
return;
}