Cod sursa(job #1450523)

Utilizator cristian.caldareaCaldarea Cristian Daniel cristian.caldarea Data 13 iunie 2015 16:09:49
Problema Asmax Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 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();
void Dfs(int x);
int Solve(int x );

int main()
{
    Read();
    r[1] = 1;
    Dfs(1);
    for ( int i = 1; i <= n; ++i )
        if ( G[i].size() == 1 )
            Solve(i);
    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);
    }
}

void Dfs(int x)
{
    b[x] = true;

	for (const int& y : G[x])
		if ( !b[y] )
		{
		    r[y] = r[x] + 1;
            Dfs(y);
		}

}

int Solve(int x)
{
    int y;
    for (const int& p : G[x])
		if ( r[p] < r[x]   )
		{
            if (s[x] > 0)
            {
                s[p] += s[x];
                smax = max(s[p], smax);

            }
            Solve(p);
        }
    return 0;
}