Cod sursa(job #1165026)

Utilizator stefanzzzStefan Popa stefanzzz Data 2 aprilie 2014 13:44:43
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <fstream>
#include <vector>
#define MAXN 16005
#define INF 2000000000
using namespace std;
ifstream f("asmax.in");
ofstream g("asmax.out");

int n,x,y,pd[MAXN],tata[MAXN],sol=-INF;
vector<int> G[MAXN];

void DFS(int p);

int main()
{
    int i;
    f>>n;
    for(i=1;i<=n;i++)
        f>>pd[i];
    for(i=1;i<n;i++){
        f>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);}
    DFS(1);
    g<<sol<<'\n';
    f.close();
    g.close();
    return 0;
}

void DFS(int p){
    int i;
    for(i=0;i<G[p].size();i++){
        x=G[p][i];
        if(x==tata[p])
            continue;
        tata[x]=p;
        DFS(x);
        if(pd[G[p][i]]>0)
            pd[p]+=pd[G[p][i]];}
    if(pd[p]>sol)
        sol=pd[p];}