Cod sursa(job #1348753)

Utilizator dianaa21Diana Pislaru dianaa21 Data 19 februarie 2015 20:51:19
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>
#include <queue>
#include <vector>
#define nmax 16001
#define INF 0x3f3f3f3f
using namespace std;
ifstream is ("asmax.in");
ofstream os ("asmax.out");

vector <int> V[nmax];
int SUM[nmax], N, x, y, ANSW = -INF;
bool Vis[nmax];

void Read();
void DFS(int);

int main()
{
    Read();
    DFS(1);
    for(int i = 1; i <= N; ++i)
        ANSW = max(ANSW, SUM[i]);
    os << ANSW;

    is.close();
    os.close();
    return 0;
}
void Read()
{
    is >> N;
    for(int i = 1; i <= N; ++i)
        is >> SUM[i];
    for(int i = 1; i < N; ++i)
    {
        is >> x >> y;
        V[x].push_back(y);
        V[y].push_back(x);
    }
}
void DFS(int node)
{
    Vis[node] = true;
    vector<int>::iterator it;
    for(it = V[node].begin(); it != V[node].end(); ++it)
        if(!Vis[*it])
        {
            DFS(*it);
            if(SUM[*it] > 0)
                SUM[node] += SUM[*it];
        }
}