This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimization O2
#pragma GCC optimization "unroll-loop"
#pragma target ("avx2")
#include <bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
using namespace std;
typedef pair<ll,ll> LL;
const ll N = 2e5 + 9;
const ll inf = 1e9 + 7;
vector<LL> g[N];
ll n,x,y,w,dp[N][2][2],ans,val1[N],val2[N];
void DFS(ll u,ll p){
ll child = 0; multiset<ll> s;
for (auto i : g[u]) if (i.fs != p) DFS(i.fs,u),child++;
dp[u][0][0] = dp[u][0][1] = 0;
if (!child) dp[u][1][0] = dp[u][1][1] = -inf;
for (auto i : g[u]){
ll v = i.fs,cost = i.sc;
if (v == p) continue;
dp[u][0][0] += max(dp[v][1][0] + cost,dp[v][0][0]);
val1[v] = dp[v][0][1] + cost - max(dp[v][1][0] + cost,dp[v][0][0]);
val2[v] = dp[v][0][0] + cost - max(dp[v][1][0] + cost,dp[v][0][0]); s.insert(val2[v]);
}
for (auto i : g[u]){
ll v = i.fs,cost = i.sc;
if (v == p) continue;
ll now = dp[u][0][0] - max(dp[v][1][0] + cost,dp[v][0][0]) + dp[v][0][0] + cost;
dp[u][1][0] = max(dp[u][1][0],now);
dp[u][1][1] = max(dp[u][1][1],now - dp[v][0][0] + dp[v][0][1]);
}
for (auto i : g[u]){
ll v = i.fs,cost = i.sc;
if (v == p) continue;
ll now = dp[u][0][0] - max(dp[v][1][0] + cost,dp[v][0][0]) + max(dp[v][1][1] + cost,dp[v][0][1]);
dp[u][0][1] = max(dp[u][0][1],now);
}
if (child < 2) return;
for (auto i : g[u]){
ll v = i.fs,cost = i.sc;
if (v == p) continue;
s.erase(s.find(val2[v]));
dp[u][0][1] = max(dp[u][0][1],dp[u][0][0] + val1[v] + *s.rbegin());
s.insert(val2[v]);
}
}
int main(){
ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
#define task "test"
if (fopen(task".INP","r")){
freopen(task".INP","r",stdin);
freopen(task".OUT","w",stdout);
}
cin>>n;
for (ll i = 1;i < n;i++){
cin>>x>>y>>w;
g[x].push_back({y,w}); g[y].push_back({x,w});
}
DFS(1,0); //cout<<dp[1][0][0]<<" "<<dp[1][0][1]; return 0;
cout<<max(dp[1][0][0],dp[1][0][1]);
}
Compilation message (stderr)
beads.cpp:1: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
1 | #pragma GCC optimization O2
|
beads.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
2 | #pragma GCC optimization "unroll-loop"
|
beads.cpp:3: warning: ignoring '#pragma target ' [-Wunknown-pragmas]
3 | #pragma target ("avx2")
|
beads.cpp: In function 'void DFS(int, int)':
beads.cpp:46:21: warning: unused variable 'cost' [-Wunused-variable]
46 | ll v = i.fs,cost = i.sc;
| ^~~~
beads.cpp: In function 'int main()':
beads.cpp:57:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | freopen(task".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
beads.cpp:58:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | freopen(task".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |