Cod sursa(job #13727)

Utilizator megabyteBarsan Paul megabyte Data 7 februarie 2007 13:59:36
Problema Asmax Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <stdio.h>
#define NMAX 16384
#define INF "asmax.in"
#define OUF "asmax.out"
int s[NMAX];

struct nod
{
	int x;
	nod *next;
};
struct lst
{
	nod *p,*q;
}list[NMAX];
void deeps(int vf)
{
    if(list[vf].p!=NULL)
    {
	    nod *op;
	    op=list[vf].p;
	    while(op!=NULL)
	    {
		    deeps(op->x);
		    if(s[op->x]>0) s[vf]+=s[op->x];
		    op=op->next;
	    }
    }
}

int main()
{
	int n,i,a,b,v=1,max,cp=0;
	nod *op;
	FILE *in,*out;
	in=fopen(INF,"r");
	out=fopen(OUF,"w");
	fscanf(in,"%d",&n);
	for(i=1;i<=n;i++)
	{
		fscanf(in,"%d",s+i);
		if(s[i]>0) cp++;
		list[i].p=list[i].q=NULL;
	}
	max=-16777216;
	if(n>1)
	{
	fscanf(in,"%d %d",&a,&b);
	op=new nod;
	op->x=b;op->next=NULL; 
	if(list[a].p==NULL) list[a].p=list[a].q=op;
	   else
	   {
		   list[a].q->next=op;
		   list[a].q=op;
	   }
	   v=a;
	max=s[a]+s[b];}
	for(i=2;i<n;i++)
	{
           fscanf(in,"%d %d",&a,&b);
	   if(s[a]+s[b]>max) max=s[a]+s[b];
	   op=new nod;
	   op->x=b;op->next=NULL; 
	   if(list[a].p==NULL) list[a].p=list[a].q=op;
	   else
	   {
		   list[a].q->next=op;
		   list[a].q=op;
	   }
	}
//	if(cp>1){
	          deeps(v);
	          for(i=1;i<=n;i++) if(s[i]>max) max=s[i];
//	        }
	fprintf(out,"%d",max);
	fclose(in);fclose(out);
	return 0;
}