This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define wiwihorz
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse")
#pragma loop-opt(on)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define rrep(i, a, b) for(int i = b; i >= a; i --)
#define all(x) x.begin(), x.end()
#define ceil(a, b) ((a + b - 1) / (b))
#define int long long int
#define lld long double
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())
#define INF 1000000000000000000
#define MOD 1000000007
#define eps (1e-9)
using namespace std;
#ifdef wiwihorz
#define print(a...)cerr<<"Line "<<__LINE__<<":",kout("["+string(#a)+"] = ", a)
void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
void kout() { cerr << endl; }
template<class T1,class ... T2>void kout(T1 a,T2 ... e){cerr<<a<<" ",kout(e...);}
#else
#define print(...) 0
#define vprint(...) 0
#endif
#define x first
#define y second
namespace solver {
int n, ans = 0;
vector<int> leaf, dp1, dp2;
vector<vector<pii>> mp;
void init_(int _n) {
n = _n, ans = 0;
mp.assign(n + 1, vector<pii>());
dp1.assign(n + 1, 0);
dp2.assign(n + 1, 0);
leaf.assign(n + 1, 1);
}
void dfs(int x, int par) {
int v = -INF, sum = 0;
for(auto i : mp[x]) if(i.x != par) {
dfs(i.x, x);
leaf[x] = 0;
if(leaf[i.x]) v = max(v, i.y);
else {
int val = max(dp1[i.x] + i.y, dp2[i.x]);
sum += val;
v = max(v, -val + dp2[i.x] + i.y);
}
}
dp1[x] = sum + v;
dp2[x] = sum;
}
void dfs2(int x, int par, int cost, int v1, int v2) {
int sum = x == par ? 0 : max(v1 + cost, v2);
int mx = -sum + v2 + cost, sc = -INF;
for(auto i : mp[x]) if(i.x != par) {
if(leaf[i.x]) {
int tmp = i.y;
if(tmp > mx) swap(tmp, mx);
if(tmp > sc) swap(tmp, sc);
}
else {
int val = max(dp1[i.x] + i.y, dp2[i.x]);
int tmp = -val + dp2[i.x] + i.y;
sum += val;
if(tmp > mx) swap(tmp, mx);
if(tmp > sc) swap(tmp, sc);
}
}
ans = max(ans, sum);
for(auto i : mp[x]) if(i.x != par) {
if(leaf[i.x]) {
int cur = (mx == i.y ? sc : mx);
dfs2(i.x, x, i.y, sum + cur, sum);
}
else {
int val = max(dp1[i.x] + i.y, dp2[i.x]);
int tmp = -val + dp2[i.x] + i.y;
int cur = (mx == tmp ? sc : mx);
dfs2(i.x, x, i.y, sum - val + cur, sum - val);
}
}
}
void solve() {
int root = 1;
rep(i, 1, n) if(mp[i].size() != 1) root = i;
if(mp[root].size() == 1) cout << "0\n";
else {
dfs(root, root);
dfs2(root, root, 0, -INF, -INF);
ans = max(ans, dp2[root]);
cout << ans << "\n";
}
}
};
using namespace solver;
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int n; cin >> n;
init_(n);
rep(i, 1, n - 1) {
int a, b, c;
cin >> a >> b >> c;
mp[a].push_back({b, c});
mp[b].push_back({a, c});
}
solve();
return 0;
}
Compilation message (stderr)
beads.cpp:5: warning: ignoring '#pragma loop ' [-Wunknown-pragmas]
5 | #pragma loop-opt(on)
|
beads.cpp:20:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
20 | void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
| ^~~~
beads.cpp:20:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
20 | void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
| ^~~~
# | 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... |