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, ll> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
const int N = 2e5 + 10, MOD = 1e9 + 7;
ll dp[2][2][N], shit[N]; int n;
vector<pii> adj[N];
void DFS(int v, int p = -1) {
dp[0][0][v] = 0, dp[1][0][v] = dp[0][1][v] = dp[1][1][v] = shit[v] = -MOD;
for (pii u : adj[v]) {
if (u.F != p) DFS(u.F, v);
}
for (pii u : adj[v]) {
if (u.F != p) {
//printf("%d %lld %lld %lld %lld %lld\n", v, dp[0][0][v], dp[0][1][v], dp[1][0][v], dp[1][1][v], shit[v]);
int x11 = dp[1][1][v], x01 = dp[0][1][v], x10 = dp[1][0][v], x00 = dp[0][0][v], sv = shit[v];
dp[0][0][v] += max(dp[0][0][u.F], dp[0][1][u.F] + u.S);
dp[0][1][v] = max(x01 + max(dp[0][0][u.F], dp[0][1][u.F] + u.S), x00 + dp[0][0][u.F] + u.S);
dp[1][0][v] = max(u.S + max(x01 + max(dp[0][0][u.F], dp[1][0][u.F]), x11 + dp[0][0][u.F]), x10 + max(dp[0][0][u.F], dp[0][1][u.F] + u.S));
dp[1][1][v] = max(x11 + max(dp[0][0][u.F], dp[0][1][u.F] + u.S), x01 + max(dp[1][0][u.F], dp[1][1][u.F] + u.S));
dp[1][1][v] = max({dp[1][1][v], u.S + sv + dp[0][0][u.F], u.S + x00 + dp[1][0][u.F]});
shit[v] = max(sv + max(dp[0][0][u.F], dp[0][1][u.F] + u.S), x00 + max(dp[1][0][u.F], dp[1][1][u.F] + u.S));
}
}
//printf("%d %lld %lld %lld %lld %lld\n", v, dp[0][0][v], dp[0][1][v], dp[1][0][v], dp[1][1][v], shit[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", max({dp[1][0][1], dp[0][0][1], shit[1]}));
return 0;
}
Compilation message (stderr)
beads.cpp: In function 'int main()':
beads.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
38 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
beads.cpp:40:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
40 | int u, v; ll w; scanf("%d%d%lld", &u, &v, &w);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |