# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
908136 | duckindog | Beads and wires (APIO14_beads) | C++14 | 1018 ms | 8792 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// from duckindog wth depression
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10;
int n;
vector<pair<int, int>> ad[N];
int f[2][N];
void dfs(int u, int pre = 0) {
int all = -1e15, none = -1e15;
for (auto duck : ad[u]) {
int v, w; tie(v, w) = duck;
if (v == pre) continue;
if (all == -1e15) all = 0;
dfs(v, u);
int best = max(f[1][v] + w, f[0][v]);
all += best;
none = max(none, f[0][v] + w - best);
}
f[0][u] = max(0ll, all);
f[1][u] = all + none;
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("duck.inp", "r")) {
freopen("duck.inp", "r", stdin);
freopen("duck.out", "w", stdout);
}
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v, w; cin >> u >> v >> w;
ad[u].push_back({v, w});
ad[v].push_back({u, w});
}
int answer = 0;
for (int i = 1; i <= n; ++i) {
dfs(i);
answer = max(answer, *max_element(f[0] + 1, f[0] + n + 1));
}
cout << answer;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |