Pagini recente » Cod sursa (job #2731591) | Cod sursa (job #1050347) | Cod sursa (job #808331) | Cod sursa (job #1928129) | Cod sursa (job #2200317)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("zvon.in");
ofstream fout("zvon.out");
const int N = 1e5+2;
int n, d[N];
vector<int>G[N];
vector<int>D;
bool cmp(int a, int b){ return d[a]>d[b]; }
void dfs(int nod)
{
if(G[nod].size())
{
for(auto it:G[nod])
{
dfs(it);
}
sort(G[nod].begin(), G[nod].end(), cmp);
int x=1;
while(x<G[nod].size()&&d[G[nod][0]]==d[G[nod][x]])
{
x++;
}
d[nod]=d[G[nod][0]]+x;
}
else
{
d[nod]=0;
}
}
int main()
{
int q, t, f;
fin>>q;
while(q--)
{
fin>>n;
for(int i=1; i<=n; i++)
{
G[i].clear();
}
for(int i=1; i<n; i++)
{
fin>>t>>f;
G[t].push_back(f);
}
dfs(1);
fout<<d[1]<<'\n';
}
return 0;
}