Cod sursa(job #1426834)

Utilizator diana-t95FMI Tudoreanu Diana Elena diana-t95 Data 30 aprilie 2015 18:47:22
Problema Asmax Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
#define maxn 16006
vector<int> g[maxn];
int n, m;
int c[maxn], viz[maxn];
void dfs(int x)
{
    viz[x] = 1;
    for (int i = 0; i < g[x].size(); i++)
        if (!viz[g[x][i]])
    {
        dfs(g[x][i]);
        if (c[x] + c[g[x][i]] > c[x]) c[x] +=c[g[x][i]];
    }

}
int main()
{
    ifstream f("asmax.in");
    ofstream e("asmax.out");
    f>>n;
    int maxim = -1001, imax;
    for (int i = 1; i <= n; i++)
    {
        f>>c[i];
        if (c[i] > maxim)
        {
            maxim = c[i];
            imax = i;
        }
    }
    int x, y;
    for (int i = 0; i < n; i++)
    {
        f>>x>>y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    if (maxim < 0) e<<maxim;
        else {
            dfs(imax);
            e<<c[imax];
        }
}