Cod sursa(job #1238975)

Utilizator cojocarugabiReality cojocarugabi Data 7 octombrie 2014 23:35:52
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
# include <cstdio>
# include <iostream>
# include <algorithm>
using namespace std;
typedef struct node
{
    int x;
    node *next;
    node(){x=0;next=0;}
} *nod;
const int nmax = 16005;
nod S[nmax];
void add(int x,int y)
{
    nod p=new node;
    p->x=y;
    p->next=S[x];
    S[x]=p;
}
int s[nmax],D[nmax];
bool b[nmax];
int dfs(int x)
{
    if (b[x]) return 0;
    b[x]=1;
    D[x]=s[x];
    for (nod p=S[x];p;p=p->next) D[x]+=max(0,dfs(p->x));
    return (D[x]);
}
int main(void)
{
    int n;
    freopen("asmax.in","r",stdin);
    scanf("%d\n",&n);
    for (int i=1;i<=n;++i) scanf("%d",s+i);
    for (int i=1,x,y;i<n;++i) scanf("%d%d",&x,&y),add(x,y),add(y,x);
    dfs(1);
    int Max=s[max_element(s+1,s+1+n)-s];
    for (int i=1;i<=n;++i) Max=max(Max,D[i]);
    fprintf(fopen("asmax.out","w"),"%d\n",Max);
    return 0;
}