Cod sursa(job #3197985)

Utilizator KRISTY06Mateiu Ianis Cristian Vasile KRISTY06 Data 27 ianuarie 2024 20:58:02
Problema Asmax Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <bits/stdc++.h>
using namespace std;

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

const int MAX_LENGTH = 16000;
const int MAX_COST = 16000000;

int noNodes;
int cost[MAX_LENGTH + 1], maxNodeCost[MAX_LENGTH + 1];

vector<int> graph[MAX_LENGTH + 1];

void gen(int startNode, int &maxCost, bool isVisited[MAX_LENGTH + 1], int &currentCost) {
    for (vector<int>::iterator it = graph[startNode].begin(); it < graph[startNode].end(); ++it) {
        if (isVisited[*it] == 0) {
            isVisited[*it] = 1;
            currentCost += cost[*it];
            maxCost = max(currentCost, maxCost);
            gen(*it, maxCost, isVisited, currentCost);
        }
    }
}

int main() {
    fin >> noNodes;
    for (int i = 1; i <= noNodes; ++i) {
        fin >> cost[i];
    }
    for (int i = 1; i < noNodes; ++i) {
        int startNode, endNode;
        fin >> startNode >> endNode;
        graph[startNode].push_back(endNode);
        graph[endNode].push_back(startNode);
    }
    int maxCost = -MAX_COST - 1;
    for (int i = 1; i <= noNodes; ++i) {
        bool isVisited[MAX_LENGTH + 1] = {0};
        isVisited[i] = 1;
        int currentCost = cost[i];
        gen(i, maxCost, isVisited, currentCost);
    }
    fout << maxCost;
    return 0;
}