Pagini recente » Cod sursa (job #390238) | Cod sursa (job #2441890) | Cod sursa (job #795941) | Cod sursa (job #3249518) | Cod sursa (job #1547783)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
const int nmax = 100005;
ifstream fin ("zvon.in");
ofstream fout ("zvon.out");
int cost[nmax];
inline bool cmp(int x, int y)
{
return cost[x] > cost[y];
}
void dfs(int dad, vector <int> g[nmax])
{
int i, son, maxx, c;
if(g[dad].size()==0)
{
cost[dad]=0;
return;
}
for(i=0; i<g[dad].size(); i++)
{
son=g[dad][i];
dfs(son, g);
}
sort(g[dad].begin(), g[dad].end(), cmp);
maxx=0, c=1;
for(i=0; i<g[dad].size(); i++)
{
son=g[dad][i];
maxx=max(maxx, c+cost[son]);
c++;
}
cost[dad]=maxx;
}
void solve_test()
{
int n, i, x, y;
vector <int> g[nmax];
fin >> n;
if(n==1)
{
fout << 0 << "\n";
return;
}
for(i=1; i<n; i++)
{
fin >> x >> y;
g[x].push_back(y);
}
for(i=1; i<=n; i++)
cost[i]=0;
dfs(1, g);
fout << cost[1] << "\n";
}
int main()
{
ios_base::sync_with_stdio(false);
int t;
fin >> t;
while(t--) solve_test();
fin.close();
fout.close();
return 0;
}