Pagini recente » Cod sursa (job #1958519) | Cod sursa (job #290077) | Cod sursa (job #2892797) | Cod sursa (job #2796055) | Cod sursa (job #2830416)
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
const int NMAX = 16003;
int costMax = -32000;
bool visited[NMAX];
int costs[NMAX];
int dfs(int node, vector<vector<int>> edges) {
visited[node] = true;
int cost = costs[node];
for(auto neighbour: edges[node])
{
if(!visited[neighbour] ){
cost = max(cost, cost + dfs(neighbour, edges));
}
}
if(cost > costMax )
costMax = cost;
return cost;
}
int main()
{
vector<vector<int>> edges;
ifstream f("dfs.in");
ofstream g("dfs.out");
int n, x, y;
f >> n;
for(int i = 0; i< n; i++) {
f >> x;
costs[i+1] = x;
}
edges.resize(n+1);
for(int i = 0; i< n-1; i++) {
f >> x >> y;
edges[x].push_back(y);
edges[y].push_back(x);
}
dfs(1, edges);
g << costMax;
}