Pagini recente » Cod sursa (job #1225801) | Cod sursa (job #2304652) | Cod sursa (job #2583627) | Cod sursa (job #1502677) | Cod sursa (job #928440)
Cod sursa(job #928440)
#include<stdio.h>
#include<stdlib.h>
#define dim 16005
using namespace std;
int i,n,x,j,y,a,*mat[dim],viz[dim];
long long best[dim];
long long Max=-99999999;
int sum = 0;
void DF(int x)
{int i;
viz[x] = 1;
for(i=1; i<=mat[x][0]; i++)
if(! viz[mat[x][i]] )
{
DF(mat[x][i]);
if(best[x] + best[mat[x][i]] > best[x])
{best[x] = best[x] + best[mat[x][i]];
if(best[x] > Max) Max = best[x];
}
}
}
int main()
{
FILE *f=fopen("asmax.in","r"), *g=fopen("asmax.out","w");
fscanf(f,"%d",&n);
for(i=1; i<=n; i++)
{fscanf(f,"%lld",&best[i]);
if(best[i] > Max) Max = best[i];
}
for(i=1; i<=n; i++)
{mat[i] = (int *)realloc(mat[i],sizeof(int));
mat[i][0] = 0;
}
for(i=1; i<n; i++)
{fscanf(f,"%d%d",&x,&y);
mat[x][0]++;
a = mat[x][0];
mat[x] = (int *)realloc(mat[x], (a+1)*sizeof(int) );
mat[x][a] = y;
mat[y][0]++;
a = mat[y][0];
mat[y] = (int *)realloc(mat[y], (a+1)*sizeof(int) );
mat[y][a] = x;
}
DF(1);
fprintf(g,"%lld\n",Max);
fclose(f);
fclose(g);
return 0;
}