Cod sursa(job #2150157)

Utilizator cosceexcosceex cosceex Data 3 martie 2018 12:07:01
Problema Asmax Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <bits/stdc++.h>
#define nmax 1000001
using namespace std;

ifstream f("asmax.in");
ofstream g("asmax.out");

int n;
int v[nmax];
bool viz[nmax];
int a,b;
vector <int> mat[nmax];
int ras;

void dfs(int x)
{
    viz[x]=true;
    int m=mat[x].size();
    for (int i=0; i<m; i++)
    {
        int y=mat[x][i];
        if (viz[y]==false)
        {
            dfs(y);
            if (v[y]>0)
            {
                v[x]+=v[y];
            }
        }
    }
    ras=max(ras, v[x]);
}

int main()
{
    f>>n;
    for(int i=1;i<=n;i++)
        f>>v[i];
    for(int i=1;i<n;i++)
    {
        f>>a>>b;
        mat[a].push_back(b);
        mat[b].push_back(a);
    }

    dfs(1);
    g<<ras;
    return 0;
}