Cod sursa(job #2841845)

Utilizator DananauStefanRazvanDananau Stefan-Razvan DananauStefanRazvan Data 30 ianuarie 2022 15:50:42
Problema Asmax Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

int n, m, maxim = -1;

vector<vector<int>> adjList;
vector<bool> vis;
vector<int> cost;

void read() {
    fin >> n;

    adjList.resize(n + 1);
    vis.resize(n + 1);
    cost.resize(n + 1);

    for (int i = 1; i <= n; i++)
        fin >> cost[i];

    for (int i = 0; i < n; i++) {
        int x, y;
        fin >> x >> y;
        adjList[x].push_back(y);
        adjList[y].push_back(x);
    }
}

void dfs(int node) {
    vis[node] = true;

    for (int i = 0; i < adjList[node].size(); i++) { // Eroare la auto??
        int x = adjList[node][i];

        if (!vis[x]) {
            dfs(x);

            if (cost[node] < cost[node] + cost[x])
                cost[node] = cost[node] + cost[x];
        }
    }

    maxim = max(maxim, cost[node]);
}

void printSol() {
    dfs(1);
    fout << maxim;
}

int main()
{
    read();
    printSol();
    return 0;
}