Pagini recente » Cod sursa (job #1916043) | Cod sursa (job #538745) | Cod sursa (job #1525114) | Cod sursa (job #1812044) | Cod sursa (job #193610)
Cod sursa(job #193610)
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>
using namespace std;
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
vector<vector<pair<int,int> > > a(100005);
int t[100005],trecut[100005];
inline int maxc(int x,int y) { return x>y?x:y;}
bool comp(const pair<int,int> &x,const pair<int,int> &y)
{
return x.first>=y.first?1:0;
}
void dfs(int x)
{
trecut[x]=1;
for (vector<pair<int,int> >::iterator it=a[x].begin();it!=a[x].end();it++)
if(!trecut[it->second])
{
dfs(it->second);
it->first=t[it->second];
}
sort(all(a[x]),comp);
for (vector<pair<int,int> >::iterator it=a[x].begin();it!=a[x].end();it++)
{
t[x]=maxc(t[x],it->first+it-a[x].begin()+1);
// printf("%d\n",
}
}
int main()
{
FILE *in,*out;
int n,T,i,x,y;
in=fopen("zvon.in","r");
out=fopen("zvon.out","w");
fscanf(in,"%d",&T);
for (;T;T--)
{
for (i=1;i<=n;i++)
a[i].erase(all(a[i]));
fscanf(in,"%d",&n);
for (i=1;i<=n-1;i++)
{
fscanf(in,"%d%d",&x,&y);
a[x].pb(mp(0,y));
}
fill(t,t+n+3,0);
fill(trecut,trecut+n+3,0);
dfs(1);
fprintf(out,"%d\n",t[1]);
}
fclose(in);
fclose(out);
return 0;
}