Pagini recente » Cod sursa (job #1850344) | Cod sursa (job #652738) | Cod sursa (job #2289819) | Cod sursa (job #1523846) | Cod sursa (job #206499)
Cod sursa(job #206499)
#include <cstdio>
#include <cstdlib>
#define NMAX 16001
#define max(a,b) (a) < (b) ? (b) : (a)
int N, M, c[NMAX], vis[NMAX], smax;
int * G[NMAX]; // O(M + N)
void readData()
{
freopen("asmax.in","r",stdin);
scanf("%d",&N);
for(int i = 1 ; i <= N; ++i)
scanf("%d",&c[i]);
for(int i = 1; i <= N ; ++i)
{
G[i] = (int*) malloc(sizeof (int) );
G[i][0] = 0;
}
for(int x1,x2; scanf("%d %d",&x1,&x2) != EOF; )
{
++G[x1][0];
G[x1] = (int*) realloc(G[x1], (G[x1][0] + 1) * sizeof (int) );
++G[x2][0];
G[x2] = (int*) realloc(G[x2], (G[x2][0] + 1) * sizeof (int) );
G[x1][G[x1][0]] = x2;
G[x2][G[x2][0]] = x1;
}
}
void writeData()
{
freopen("asmax.out","w",stdout);
printf("%d",smax);
}
void dfs(int node)
{
vis[node] = 1;
for(int j = 1; j <= G[node][0]; ++j)
if(!vis[G[node][j]])
{
dfs(G[node][j]);
c[node] = max(c[node], c[node] + c[G[node][j]]);
}
if(smax < c[node]) smax = c[node];
}
int main()
{
readData();
dfs(1);
writeData();
return 0;
}