Pagini recente » Cod sursa (job #2137917) | Cod sursa (job #2340219) | Cod sursa (job #2426421) | Cod sursa (job #372958) | Cod sursa (job #2500728)
//
// main.cpp
// berarii2
//
// Created by Ciprian Ionescu on 11/28/19.
// Copyright © 2019 Ciprian Ionescu. All rights reserved.
//
#include <iostream>
#include <fstream>
#include <array>
#define MAX_N 16001
#define MAX_M 16001
#define INF 16000001
int v[MAX_N], smax = -INF;
class Graph {
public:
void add(int x, int y) {
nodes[++size].node = y;
nodes[size].next = list[x];
list[x] = &nodes[size];
}
int asmax(int x);
private:
size_t size;
struct node {
int node;
struct node* next;
};
std::array<node, MAX_M> nodes;
std::array<node*, MAX_N> list;
std::array<bool, MAX_N> visited;
};
int Graph::asmax(int x) {
visited[x] = true;
node* p = list[x];
int y, d, s;
s = v[x];
while (p) {
y = p->node;
if (!visited[y]) if ((d = asmax(y)) >= 0) s += d;
p = p->next;
}
if (s > smax) smax = s;
return s;
}
Graph g;
std::ifstream fin("asmax.in");
std::ofstream fout("asmax.out");
int main(int argc, const char * argv[]) {
int x, y, n;
// Graph g;
fin >> n;
for (int i = 1 ; i <= n ; i++) fin >> v[i];
for (int i = 1 ; i < n ; i++) {
fin >> x >> y;
g.add(x, y);
g.add(y, x);
}
g.asmax(1);
fout << smax;
return 0;
}