Cod sursa(job #1452867)

Utilizator bogdan.balanBogdan Balan bogdan.balan Data 22 iunie 2015 09:14:49
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

ifstream fin("asmax.in");
ofstream fout("asmax.out");

const int Inf = 0x3f3f3f3f;

int n, smax = -Inf;
vector<vector<int>> G;
vector<int> v, s, r;
vector<bool> b;

void Read();
int Dfs(int x);


int main()
{
    Read();
    Dfs(1);
    fout << smax;
    fin.close();
    fout.close();
    return 0;
}

void Read()
{
    fin >> n;
    G = vector<vector<int>>(n + 1, vector<int>());
    s = vector<int>(n + 1, 0);
    v = vector<int>(n + 1, 0);
    r = vector<int>(n + 1, 0);
    b = vector<bool>(n + 1, 0);
    int x, y;
    for ( int i = 1; i <= n; i++)
        fin >> s[i];
    while( fin >> x >> y )
    {
        G[x].push_back(y);
        G[y].push_back(x);
    }
}

int Dfs(int x )
{

    int y = 0;
    b[x] = true;
    y += s[x];

    for (const int& p : G[x])
        if ( !b[p] )
        {
            y += Dfs(p);
        }
   // fout << x << ' ' << y << '\n';
    if ( y > smax )
        smax = y;
    if ( y > 0 )
        return y;
    else return 0;
}