Cod sursa(job #1439387)

Utilizator YusukeFMI Mares Medar Razvan Yusuke Data 22 mai 2015 11:20:05
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <fstream>
using namespace std;
ifstream f ("asmax.in");
ofstream g ("asmax.out");
#define MAXN 16005
int C[MAXN],t[MAXN],v[MAXN];
int N, i, maxim = -( 1 << 30);
struct nod{
    int val;
    nod *urm;
}*A[MAXN],*p;
void Read(){
    f >> N;
    int aux1, aux2;
    for (i = 1;i <= N;i++)
        f >> C[i];
    for (i = 1;i < N; i++){
        f >> aux1 >> aux2;
        p = new nod;
        p -> val = aux1;
        p -> urm = A[aux2];
        A[aux2] = p;
        p = new nod;
        p -> val = aux2;
        p -> urm = A[aux1];
        A[aux1] = p;
    }
}
void df (int x){
    nod *p;
    v[x] = 1;
    p = A[x];
    while (p){
        if (!v[p->val]){
            t[p -> val] = x;
            df (p -> val);
            if (C[p -> val]>0)
                C[t[p -> val]] = C[t[p -> val]] + C[p->val];
        }
    p = p -> urm;
    }
}
int main ()
{
    Read();
    df (1);
    for(i = 1;i <= N;i++)
        if(C[i] > maxim)
            maxim = C[i];
    g << maxim;
}