#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
const int N = 200001;
vector<pair<int, int>> g[N];
ll d0[N], d1[N], d2[N], d3[N], pw[N], inf = 1e11;
void dfs(int v, int p) {
ll t1 = -inf, t2 = -inf, t3 = -inf, w0 = -inf, w2 = -inf, z2 = -inf;
for (auto &e : g[v]) {
int u = e.first;
if (u == p)
continue;
dfs(u, v);
pw[u] = e.second;
d1[u] += e.second; d3[u] += e.second;
d1[u] = max(d1[u], d0[u]);
d2[u] = max(d2[u], d0[u]);
d3[u] = max(d3[u], d2[u]);
// d3[u] = max(d3[u], d1[u]);
d0[v] += d1[u];
t1 = max(t1, d0[u] - d1[u] + pw[u]);
t2 = max(t2, d3[u] - d1[u]);
t3 = max(t3, d2[u] - d1[u] + pw[u]);
d2[v] = max(d2[v], w0 + d2[u] + pw[u] - d1[u]);
d2[v] = max(d2[v], w2 + d0[u] + pw[u] - d1[u]);
// d3[v] = max(d3[v], w0 + d2[u] - d1[u]);
// d3[v] = max(d3[v], z2 + d0[u] + pw[u] - d1[u]);
w0 = max(w0, d0[u] + pw[u] - d1[u]);
w2 = max(w2, d2[u] + pw[u] - d1[u]);
// z2 = max(z2, d2[u] - d1[u]);
}
d1[v] = d0[v] + t1;
d2[v] = d0[v] + max(d2[v], t2);
d3[v] = d0[v] + max(d3[v], t3);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, u, v, w;
cin >> n;
rep(i, n - 1) {
cin >> u >> v >> w;
u--; v--;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
dfs(0, -1);
cout << max(d0[0], d2[0]);
}
Compilation message
beads.cpp: In function 'void dfs(int, int)':
beads.cpp:34:63: warning: unused variable 'z2' [-Wunused-variable]
ll t1 = -inf, t2 = -inf, t3 = -inf, w0 = -inf, w2 = -inf, z2 = -inf;
^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Incorrect |
3 ms |
5120 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Incorrect |
3 ms |
5120 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Incorrect |
3 ms |
5120 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Incorrect |
3 ms |
5120 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |