Cod sursa(job #104663)

Utilizator ScrazyRobert Szasz Scrazy Data 16 noiembrie 2007 15:42:19
Problema Zvon Scor 0
Compilator cpp Status done
Runda Happy Coding 2007 Marime 1.14 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NMAX 100002

int t;
long n;

typedef struct _nod{int n; _nod *urm;} nod;

nod *a[NMAX]; 
long sz=sizeof(nod);
int viz[NMAX];
long rad;

long dfs(long node)
{
    long max=0,t=0,dd=0,hh=0;
    nod *p=a[node];

    viz[node]=1;

    while (p)
    {
	if (!viz[p->n])
	{
	
	    t=dfs(p->n); 
	    if (t==max) ++dd; 
	    else if (t > max) {max=t;dd=0;} 
	    ++hh;
	} 
	p=p->urm;
    } 

    max=max+dd;
    if (hh>max) max=hh;
    if (node==rad) return max;
    else return max+1;
}


int main()
{
    freopen("zvon.in","r",stdin);
    freopen("zvon.out","w",stdout);

    long i, x, y;
    nod *p;
    for (scanf("%d", &t); t; --t)
    {
	memset(viz,0,sizeof(viz));
	memset(a,NULL,sizeof(a));

	scanf("%ld", &n);
	for (i=1; i<=n-1; ++i)
	{
	    scanf("%ld %ld", &x, &y);
	    p=(nod *)malloc(sz);
	    p->n=y;
	    p->urm=a[x];
	    a[x]=p;
	    viz[y]=1;
	} 

	for (i=1; i<=n && viz[i]; ++i);
	rad=i;

	memset(viz,0,sizeof(viz));

	printf("%ld\n", dfs(rad));
    }

    fclose(stdin); 
    fclose(stdout); 
    return 0; 
}