This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 10;
const ll inf = 1e16;
ll dp[N][2][2]; // node, par edge, bad path
vector<pair<int, int>> g[N];
int n, w[N];
void setup() {
for (int i = 1; i <= n; i++)
for (int x = 0; x < 2; x++)
for (int y = 0; y < 2; y++)
dp[i][x][y] = -inf;
}
void solve(int x, int fa, int uw) {
vector<int> ch;
for (auto& p : g[x]) {
int y = p.first;
if (y == fa) continue;
w[y] = p.second;
solve(y, x, w[y]);
ch.push_back(y);
}
if (ch.empty()) {
dp[x][0][0] = 0;
return;
}
// don't take x
{
vector<ll> cur(2, -inf);
cur[0] = 0;
for (int y : ch) {
vector<ll> ndp(2, -inf);
ndp[0] = max(ndp[0], cur[0] + dp[y][0][0]);
ndp[0] = max(ndp[0], cur[0] + dp[y][1][0]);
ndp[1] = max(ndp[1], cur[0] + dp[y][0][1]);
ndp[1] = max(ndp[1], cur[0] + dp[y][1][1]);
ndp[1] = max(ndp[1], cur[1] + dp[y][0][0]);
ndp[1] = max(ndp[1], cur[1] + dp[y][1][0]);
swap(cur, ndp);
}
dp[x][0][0] = cur[0];
dp[x][0][1] = cur[1];
}
// take x, from bad path
{
vector<vector<ll>> cur(3, vector<ll>(2, -inf));
cur[0][0] = 0;
for (int y : ch) {
vector<vector<ll>> ndp(3, vector<ll>(2, -inf));
// don't take y
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
ndp[i][j] = max(ndp[i][j], cur[i][j] + dp[y][0][0]);
ndp[i][j] = max(ndp[i][j], cur[i][j] + dp[y][1][0]);
}
}
// take y
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int o = 0; o < 2; o++) {
if (j + o == 2) continue;
ndp[i + 1][j + o] = max(ndp[i + 1][j + o], cur[i][j] + dp[y][0][o] + w[y]);
}
}
}
swap(cur, ndp);
}
dp[x][0][1] = max(cur[2][0], cur[2][1]);
}
// take x, no bad path
{
vector<vector<ll>> cur(2, vector<ll>(2, -inf));
cur[0][0] = uw;
for (int y : ch) {
vector<vector<ll>> ndp(2, vector<ll>(2, -inf));
// don't take y
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int o = 0; o < 2; o++) {
if (j + o == 2) continue;
ndp[i][j + o] = max(ndp[i][j + o], cur[i][j] + dp[y][0][o]);
ndp[i][j + o] = max(ndp[i][j + o], cur[i][j] + dp[y][1][o]);
}
}
}
// take y
for (int i = 0; i < 1; i++) {
for (int j = 0; j < 2; j++) {
for (int o = 0; o < 2; o++) {
if (j + o == 2) continue;
ndp[i + 1][j + o] = max(ndp[i + 1][j + o], cur[i][j] + dp[y][0][o] + w[y]);
}
}
}
swap(cur, ndp);
}
dp[x][1][0] = cur[1][0];
dp[x][1][1] = cur[1][1];
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
g[a].push_back({b, c});
g[b].push_back({a, c});
}
setup();
solve(1, 1, -inf / 10);
printf("%lld\n", max(dp[1][0][0], dp[1][0][1]));
return 0;
}
/*
5
1 2 10
1 3 40
1 4 15
1 5 20
60
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
140
9
1 3 100
2 3 100
3 7 1
4 5 100
4 6 100
7 4 1
8 7 100
9 7 100
*/
Compilation message (stderr)
beads.cpp: In function 'int main()':
beads.cpp:123:22: warning: overflow in conversion from 'long long int' to 'int' changes value from '-1000000000000000' to '1530494976' [-Woverflow]
123 | solve(1, 1, -inf / 10);
| ~~~~~^~~~
beads.cpp:115:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
beads.cpp:118:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | scanf("%d%d%d", &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |