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>
typedef int ll;
typedef long double ld;
#define pb push_back
#define pii pair < int, int >
#define F first
#define S second
//#define int long long int
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl '\n'
using namespace std;
const int N=10002;
vector <pii> g[N];
ll vis[N];
ll ans=0;
ll dp[N][2];
void dfs(ll v,ll p,ll w){
ll ma=-1e9;
ll d=0;
for (auto u : g[v]){
if (u.F==p) continue;
dfs(u.F,v,u.S);
dp[v][1]+=dp[u.F][0];
d+=dp[u.F][0];
ma=max(ma,u.S+dp[u.F][1]-dp[u.F][0]);
}
if (p!=-1 && ma>-1e9){
ll cnt=d+ma+w;
dp[v][0]=max(dp[v][1],cnt);
}
}
ll up[N][2];
ll dfs2(ll v,ll p){
ll cnt=0,wp=0;
for (auto u : g[v]){
if (u.F==p){
wp=u.S;
continue;
}
cnt+=dp[u.F][0];
}
cnt+=up[v][0];
for (auto u : g[v]){
if (u.F==p) continue;
up[u.F][1]=cnt-dp[u.F][0];
}
for (auto u : g[v]){
if (u.F==p || v==1) continue;
up[u.F][0]=max(up[u.F][0],u.S+wp+cnt-dp[u.F][0]-up[v][0]+up[v][1]);
}
ll mx=-1e9;
for (auto u : g[v]){
ll z=cnt;
if (u.F==p) continue;
/*
for (auto q : g[v]){
if (u.F==q.F || q.F==p) continue;
ll t=z-dp[u.F][0]-dp[q.F][0]+dp[q.F][1]+u.S+q.S;
up[u.F][0]=max(up[u.F][0],t);
}
*/
ll t=z+mx-dp[u.F][0]+u.S;
up[u.F][0]=max(up[u.F][0],t);
up[u.F][0]=max(up[u.F][0],up[u.F][1]);
mx=max(mx,-dp[u.F][0]+dp[u.F][1]+u.S);
}
mx=-1e9;
reverse(g[v].begin(),g[v].end());
for (auto u : g[v]){
ll z=cnt;
if (u.F==p) continue;
/*
for (auto q : g[v]){
if (u.F==q.F || q.F==p) continue;
ll t=z-dp[u.F][0]-dp[q.F][0]+dp[q.F][1]+u.S+q.S;
up[u.F][0]=max(up[u.F][0],t);
}
*/
ll t=z+mx-dp[u.F][0]+u.S;
up[u.F][0]=max(up[u.F][0],t);
up[u.F][0]=max(up[u.F][0],up[u.F][1]);
mx=max(mx,-dp[u.F][0]+dp[u.F][1]+u.S);
}
for (auto u : g[v]){
if (u.F!=p) dfs2(u.F,v);
}
}
int32_t main(){
sync;
ll n;
cin >> n;
for (int i=0;i<n-1;i++){
ll u,v,w;
cin >> u >> v >> w;
g[u].pb({v,w});
g[v].pb({u,w});
}
for (int i=1;i<=n;i++){
vector <pii> a;
for (auto q : g[i]){
ll u=q.F;
if (g[u].size()==1) a.pb({q.S,u});
}
sort(a.begin(),a.end());
for (int j=1;j<a.size();j++){
vis[a[j].S]=1;
}
}
ll ma=0;
ll cnt=0;
dfs(1,-1,0);
dfs2(1,1);
for (int i=1;i<=n;i++){
ma=max(ma,dp[i][1]+up[i][0]);
//cout << i << " " << dp[i][1] << " " << up[i][0] << " " << ma << endl;
}
cout << ma << endl;
}
/*
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
*/
/*
6
1 2 20
2 3 20
3 4
10
3 5 10
3 6 10
*/
Compilation message (stderr)
beads.cpp: In function 'll dfs2(ll, ll)':
beads.cpp:88:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
beads.cpp: In function 'int32_t main()':
beads.cpp:106:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j=1;j<a.size();j++){
~^~~~~~~~~
beads.cpp:111:8: warning: unused variable 'cnt' [-Wunused-variable]
ll cnt=0;
^~~
# | 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... |