# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
83175 | RezwanArefin01 | Beads and wires (APIO14_beads) | C++17 | 1079 ms | 10112 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.
///usr/bin/g++ -O2 $0 -o ${0%.cpp} && echo "----------" && ./${0%.cpp}; exit;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
vector<int> adj[N], cost[N];
ll dp[N][2];
ll f(int u, int par, bool c) {
if(par && adj[u].size() == 1) {
if(c) return -1e18;
else return 0;
}
ll &ret = dp[u][c];
if(~ret) return ret;
ret = 0;
ll sum = 0;
for(int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i], c = cost[u][i];
if(v == par) continue;
sum += max(f(v, u, 0), f(v, u, 1) + c);
}
if(!c) return ret = sum;
for(int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i], c = cost[u][i];
if(v == par) continue;
ll cont = max(f(v, u, 0), f(v, u, 1) + c);
ret = max(ret, sum - cont + f(v, u, 0) + c);
}
return ret;
}
int main() {
int n; scanf("%d", &n);
for(int i = 1; i < n; i++) {
int u, v, c;
scanf("%d %d %d", &u, &v, &c);
adj[u].push_back(v);
adj[v].push_back(u);
cost[u].push_back(c);
cost[v].push_back(c);
}
ll ans = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++)
dp[j][0] = dp[j][1] = -1;
ans = max(ans, f(i, 0, 0));
}
printf("%lld\n", ans);
}
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... |