Pagini recente » Cod sursa (job #2473774) | Cod sursa (job #387657) | Cod sursa (job #682166) | Cod sursa (job #302518) | Cod sursa (job #152215)
Cod sursa(job #152215)
#include <fstream>
#include <vector>
using namespace std;
#define MAX 100000
vector < int > G[MAX], tmp[MAX];
int t[MAX], N,T;
int cmp(const int& a,const int& b)
{
return a>b;
}
void df( int nod )
{
for ( vector <int> :: iterator it = G[nod].begin(); it!=G[nod].end(); it++)
{
df( *it );
tmp[nod].push_back(t[*it]);
}
sort(tmp[nod].begin(), tmp[nod].end(), cmp);
t[nod] = 0;
int cnt = 1;
for ( vector <int> :: iterator it = tmp[nod].begin(); it!=tmp[nod].end(); it++, cnt++)
{
if ( *it + cnt > t[nod])
t[nod] = *it + cnt;
};
};
int main()
{
ifstream fin("zvon.in");
ofstream fout("zvon.out");
fin>>T;
for ( ; T>0; T--)
{
fin>>N;
for (int i = 1; i<=N; i++)
G[i].clear(), tmp[i].clear();
for ( int i = 1; i<N; i++)
{
int a,b;
fin>>a>>b;
G[a].push_back(b);
}
t[1] = 0;
df(1);
fout<<t[1]<<"\n";
}
return 0;
}