Pagini recente » Cod sursa (job #2619263) | Cod sursa (job #2512169) | Cod sursa (job #1191787) | Cod sursa (job #21720) | Cod sursa (job #5193)
Cod sursa(job #5193)
#include <stdio.h>
FILE *f;
int n,i,v[16001],k,s[16001],tmax,sum[16001],max,p[16001],t,a,b,q[16001],used[6001];
struct asdf {
int x,y;
} r[32005];
static int cmp (const void * a,const void *b)
{
struct asdf * aa = (struct asdf*) a;
struct asdf * bb = (struct asdf*) b;
if (aa->x == bb->x)
return aa->y - bb->y;
else
return aa->x - bb->x;
}
int main (void)
{
f=fopen("asmax.in","r");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
fscanf(f,"%d",&v[i]);
for(i=1;i<n;i++)
{
fscanf(f,"%d %d",&r[i].x,&r[i].y);
r[i+n].x=r[i].y;
r[i+n].y=r[i].x;
}
qsort(r+1,n*2-2,sizeof(r[0]),cmp);
k=1;
for(i=1;i<=2*n;i++)
if (r[i].x==k) {s[k]=i;k++;}
a=1;
b=2;
q[1]=1;
used[1]=1;
s[2*n+1]=k;
do
{
t=q[a];
for(i=s[t];i<s[t+1];i++)
if (!used[r[i].y]) {q[b++]=r[i].y;used[r[i].y]=1;p[r[i].y]=t;}
a++;
} while (a<b);
max=0;
for(i=n;i>=1;i--)
{
t=q[i];
tmax=sum[t]+v[t];
if (tmax > max) max=tmax;
if (tmax>0) sum[p[t]]+=tmax;
printf("t=%d tmax=%d sum[t]=%d v[t]=%d\n",t,tmax,sum[t],v[t]);
}
f=fopen("asmax.out","w");
fprintf(f,"%d\n",max);
fclose(f);
}