Pagini recente » Cod sursa (job #1328113) | Cod sursa (job #1987117) | Cod sursa (job #1886280) | Cod sursa (job #654861) | Cod sursa (job #2626912)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("zvon.in");
ofstream fout("zvon.out");
vector <int> v[100005];
int dfs(int nod)
{
int i, nr=0;
int timp[100005];
if(v[nod].size())
{
for(i=0; i<v[nod].size(); i++)
{
nr++;
timp[nr]=dfs(v[nod][i]);
}
int maxx=0;
sort(timp+1, timp+nr+1);
for(i=nr; i>0; i--)
if(timp[nr-i+1]+i>maxx) maxx=timp[nr-i+1]+i;
return maxx;
}
else return 0;
}
int main()
{
int t, n, i, a, b;
fin >> t;
while(t)
{
fin >> n;
for(i=1; i<n; i++)
{
fin >> a >> b;
v[a].push_back(b);
}
fout << dfs(1) << "\n";
for(i=0; i<=n; i++)
v[i].clear();
t--;
}
return 0;
}