Pagini recente » Cod sursa (job #3176167) | Cod sursa (job #537637) | Cod sursa (job #2798274) | Cod sursa (job #1526323) | Cod sursa (job #945463)
Cod sursa(job #945463)
#include <stdio.h>
#define NMAX 16384
#define INF "asmax.in"
#define OUF "asmax.out"
int s[NMAX];
char bit[NMAX]={0};
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)
{
if(!bit[op->x])
{
bit[op->x]=1;
deeps(op->x);
if(s[op->x]>0) s[vf]+=s[op->x];
bit[op->x]=0;
}
op=op->next;
}
}
}
int main()
{
int n,i,a,b,max;
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);
list[i].p=list[i].q=NULL;
}
max=-16777216;
for(i=1;i<n;i++)
{
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;
}
op=new nod;
op->x=a;op->next=NULL;
if(list[b].p==NULL) list[b].p=list[b].q=op;
else
{
list[b].q->next=op;
list[b].q=op;
}
}
bit[1]=1;
deeps(1);
for(i=1;i<=n;i++) if(s[i]>max) max=s[i];
fprintf(out,"%d",max);
fclose(in);fclose(out);
return 0;
}