Cod sursa(job #102178)

Utilizator coderninuHasna Robert coderninu Data 14 noiembrie 2007 01:45:36
Problema Zvon Scor 0
Compilator cpp Status done
Runda Happy Coding 2007 Marime 1.26 kb
#include <stdio.h>
#define Nmax 100001


struct nod { long inf; struct nod * urm;};
typedef nod * list;

int t;
long rad, n, i, Rez;
short radPos[Nmax];
list gr[Nmax];

void readNext();
void stergeVect();
long rez(long);
inline long max(long x, long y) { return (x==y)?(x+1): ( x>y?x:y  );  }


int main()
{
	freopen("zvon.in", "r", stdin);
	freopen("zvon.out", "w", stdout);
	scanf("%d\n", &t);
	for (int I=1; I<=t; I++)
	{
		readNext();
		Rez=rez(rad);
		printf("%ld\n", Rez);
		stergeVect();
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}

void readNext()
{
	long x, y;
	scanf("%ld\n", &n);
	for (i=1; i<n; i++)
	{
		scanf("%ld %ld\n", &x, &y);
		list q=new nod;
		q->inf=y;
		if (gr[x])
		{
			q->urm=gr[x];
			gr[x]=q;
		}
		else
		{
			gr[x]=new nod;
			gr[x]->inf=y;
			gr[x]->urm=NULL;
		}

		radPos[y]=1;
	}
	for (i=1; i<=n & radPos[i]; i++);
	rad=i;
}

void stergeVect()
{
	list p;
	for (i=1; i<=n; i++)
	{
		while (gr[i])
		{
			list q=gr[i];
			gr[i]=gr[i]->urm;
			delete q;
		}
		radPos[i]=0;
	}
}

long rez(long x)
{
	long temp;
	list p;
	if (!gr[x]) return 0;
	else
	{
		temp=rez(gr[x]->inf);
		for (p=gr[x]->urm; p; p=p->urm)
			temp=max(temp,rez(p->inf));
		return temp+1;
	}
}