Pagini recente » Cod sursa (job #2070093) | Cod sursa (job #2584103) | Cod sursa (job #3188136) | Cod sursa (job #2618727) | Cod sursa (job #1883229)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream in("zvon.in");
ofstream out("zvon.out");
const int N = 100003;
int t[N], n, test;
vector <int> a[N];
bool cmp( int x, int y )
{
return t[x] > t[y];
}
void dfs( int x )
{
int i, y;
for ( i = 0; i < a[x].size(); i++ )
{
y = a[x][i];
dfs(y);
}
sort( a[x].begin(), a[x].end(), cmp);
t[x] = 0;
for ( i = 0; i < a[x].size(); i++ )
{
y = a[x][i];
t[x] = max(t[x], 1 + i + t[y]);
}
}
int main()
{
int i, j, x, y;
in >> test;
for ( i = 1; i <= test; i++ )
{
in >> n;
for ( j = 1; j < n; j++ )
{
in >> x >> y;
a[x].push_back(y);
}
dfs(1);
out << t[1]<<'\n';
for ( j = 1; j <= n; j++ )
{
a[j].clear();
t[j] = 0;
}
}
return 0;
}