Cod sursa(job #206499)

Utilizator mika17Mihai Alex Ionescu mika17 Data 7 septembrie 2008 11:43:52
Problema Asmax Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#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;
}