# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
347743 | parsabahrami | Beads and wires (APIO14_beads) | C++17 | 4 ms | 4972 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.
// Call my Name and Save me from The Dark
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
const int N = 2e5 + 10;
ll dp[2][N]; int n;
vector<pair<int, ll>> adj[N];
void DFS(int v, int p = -1) {
dp[0][v] = 0, dp[1][v] = -1e18;
for (pair<int, ll> u : adj[v]) {
if (u.F != p) DFS(u.F, v);
}
ll sum = 0;
for (pair<int, ll> u : adj[v])
if (u.F != p) {
dp[0][v] = max(dp[0][v] + max(dp[0][u.F], dp[1][u.F] + u.S), dp[1][v] + dp[0][u.F] + u.S);
dp[1][v] = max(dp[1][v] + max(dp[0][u.F], dp[1][u.F] + u.S), sum + dp[0][u.F] + u.S);
sum += max(dp[0][u.F], dp[1][u.F] + u.S);
}
dp[0][v] = max(dp[0][v], sum);
//printf("%d %d %d\n", v, dp[0][v], dp[1][v]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v; ll w; scanf("%d%d%lld", &u, &v, &w);
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
DFS(1);
printf("%lld\n", dp[0][1]);
return 0;
}
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... |