Pagini recente » Cod sursa (job #69598) | Cod sursa (job #2937420) | Cod sursa (job #1795318) | Cod sursa (job #475328) | Cod sursa (job #2612315)
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
ifstream f("zvon.in");
ofstream g("zvon.out");
int t, n;
int s[100001];
vector < int > v[100001];
queue < int > coada;
bool cmp(int a, int b)
{
return s[a] > s[b];
}
void subOrd(int nod)
{
for (int i=0; i<v[nod].size(); i++)
{
subOrd(v[nod][i]);
s[nod] += s[v[nod][i]] + 1;
}
}
void dfs(int nod, int timp, int &timpMax)
{
timpMax = max(timpMax, timp);
for (int i=0; i<v[nod].size(); i++)
dfs(v[nod][i], timp+i+1, timpMax);
}
int main()
{
f >> t;
while (t--)
{
f >> n;
for (int i=1; i<n; i++)
{
int x, y; f >> x >> y;
v[x].push_back(y);
}
subOrd(1);
for (int i=1; i<=n; i++)
sort(v[i].begin(), v[i].end(), cmp);
int timp = 0;
dfs(1, 0, timp);
g << timp << "\n";
for (int i=1; i<=n; i++)
{
v[i].clear();
s[i] = 0;
}
}
return 0;
}