Cod sursa(job #13588)

Utilizator nemesisIchim Alexandru Eugen nemesis Data 7 februarie 2007 09:45:01
Problema Asmax Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include<stdio.h>
#include<stdlib.h>
#define maxn 16010

int n, v[maxn], max=-1000*16000-10, uz[maxn], maxi, *leg[maxn], sum=0, s[maxn];
//struct nod{ int x, y;} l[maxn];


int dfs(int x)
{
  uz[x]=1;
  s[x]=v[x];
  for(int i=1; i<=leg[x][0]; ++i) if( !uz[ leg[x][i]])
    s[x]+= dfs(leg[x][i]);

  uz[x]=0;
  if( s[x]<0) return 0;
  return s[x];
}

int main()
{
  freopen("asmax.in","r",stdin);
  scanf("%d",&n);
  for(int i=1; i<=n; ++i) {
    leg[i]= (int*)malloc( sizeof(int)*2);
    leg[i][0]=0;
  }
  for(int i=1; i<=n; ++i) scanf("%d",&v[i]);
  int x, y;
  for(int i=1; i<=n-1; ++i) {
    scanf("%d %d",&x,&y);
    leg[x][0]++;
    leg[x]=(int*)realloc(leg[x], sizeof(int)*(leg[x][0]+5));
    leg[x][ leg[x][0]]= y;

    leg[y][0]++;
    leg[y]=(int*)realloc(leg[y], sizeof(int)*(leg[y][0]+5));
    leg[y][ leg[y][0]]= x;
  }

  for(int i=1; i<=n; ++i) {
    dfs(i);
    for(int j=1; j<=n; ++j) if( s[i]>max) max=s[i];
  }
  freopen("asmax.out","w",stdout);
  printf("%d\n",max);



  return 0;
}