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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define endl '\n'
#define fi first
#define se second
#define For(i, l, r) for (auto i = (l); i < (r); i++)
#define ForE(i, l, r) for (auto i = (l); i <= (r); i++)
#define FordE(i, l, r) for (auto i = (l); i >= (r); i--)
#define Fora(v, a) for (auto v: (a))
#define bend(a) (a).begin(), (a).end()
#define isz(a) ((signed)(a).size())
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
using vi = vector <int>;
using vpii = vector <pii>;
using vvi = vector <vi>;
const int N = 2e5 + 5;
const ll infll = (ll)1e18 + 7;
int n;
vpii adj[N];
int wpar[N];
void dfs(int u, int p){
Fora(&edge, adj[u]){
int v, w; tie(v, w) = edge;
if (v == p){
continue;
}
wpar[v] = w;
dfs(v, u);
}
}
ll dp[N][2];
void dfs_dp(int u, int p){
ll sum = 0, mx = -infll;
Fora(&edge, adj[u]){
int v, w; tie(v, w) = edge;
if (v == p){
continue;
}
dfs_dp(v, u);
sum += dp[v][0];
mx = max(mx, dp[v][1] - dp[v][0]);
}
dp[u][0] = max(sum, sum + mx + wpar[u]);
dp[u][1] = sum + wpar[u];
}
ll ans = -infll;
void dfs_reroot(int u, int p){
ans = max(ans, dp[u][0]);
vector <ll> prefmx(isz(adj[u])), suffmx(isz(adj[u]));
For(i, 0, isz(adj[u])){
int v, w; tie(v, w) = adj[u][i];
prefmx[i] = max(i > 0 ? prefmx[i - 1] : -infll, dp[v][1] - dp[v][0]);
}
FordE(i, isz(adj[u]) - 1, 0){
int v, w; tie(v, w) = adj[u][i];
suffmx[i] = max(i < isz(adj[u]) - 1 ? suffmx[i + 1] : -infll, dp[v][1] - dp[v][0]);
}
int owparu = wpar[u]; ll odpu0 = dp[u][0], odpu1 = dp[u][1], osumu = dp[u][1] - wpar[u];
For(i, 0, isz(adj[u])){
int v, w; tie(v, w) = adj[u][i];
if (v == p){
continue;
}
int owparv = wpar[v]; ll odpv0 = dp[v][0], odpv1 = dp[v][1], osumv = dp[v][1] - wpar[v];
ll mx = -infll;
Fora(&edge, adj[v]){
int tv, tw; tie(tv, tw) = edge;
if (tv == u){
continue;
}
mx = max(mx, dp[tv][1] - dp[tv][0]);
}
wpar[u] = w;
dp[u][0] = max(osumu - dp[v][0], osumu - dp[v][0] + max(i > 0 ? prefmx[i - 1] : -infll, i < isz(adj[u]) - 1 ? suffmx[i + 1] : -infll) + wpar[u]);
dp[u][1] = osumu - dp[v][0] + wpar[u];
wpar[v] = -1e9;
dp[v][0] = max(osumv + dp[u][0], osumv + dp[u][0] + max(mx, dp[u][1] - dp[u][0]) + wpar[v]);
dp[v][1] = osumv + dp[u][0] + wpar[v];
dfs_reroot(v, u);
wpar[v] = owparv; dp[v][0] = odpv0; dp[v][1] = odpv1;
}
wpar[u] = owparu; dp[u][0] = odpu0; dp[u][1] = odpu1;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("KEK.inp", "r", stdin);
// freopen("KEK.out", "w", stdout);
cin >> n;
For(i, 1, n){
int u, v, w; cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
wpar[1] = -1e9;
dfs(1, 0);
dfs_dp(1, 0);
dfs_reroot(1, 0);
cout << ans << endl;
}
/*
==================================================+
INPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
# | 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... |