Cod sursa(job #478412)

Utilizator andra23Laura Draghici andra23 Data 18 august 2010 15:45:41
Problema Asmax Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include<iostream>
#include<fstream>
#include<vector>

using namespace std;

int a[16010], viz[16010], c[16010];
vector<int> son[16010];

void parcurg(int nod){
    int i, x, s = a[nod];
    viz[nod] = 1;
    if (son[nod].size() > 1){
        for (i = 0; i < son[nod].size(); i++){
            if (viz[son[nod][i]] == 0){
            parcurg(son[nod][i]);
            if (c[son[nod][i]] > 0)
                s = s+c[son[nod][i]];    
            }    
        }
        c[nod] = s; 
    }
}

int main(){
    ifstream f("asmax.in");
    ofstream g("asmax.out");
    int n, i, j, x, y;
    f>>n;
    for (i = 1; i <= n; i++)
        f>>a[i];
    for (i = 1; i <= n-1; i++){
        f>>x>>y;
        son[y].push_back(x);
        son[x].push_back(y);
    }
    
    for (i = 1; i <= n; i++)
        if (son[i].size() == 1)
            c[i] = a[i];
            
    parcurg(1);
    x = -16000100;
    for (i = 1; i <= n; i++)
        if (c[i] > x)
            x = c[i];
            
    g<<x<<'\n';
    
    return 0;
}