Submission #732876

#TimeUsernameProblemLanguageResultExecution timeMemory
732876vjudge1Beads and wires (APIO14_beads)C++17
100 / 100
197 ms26556 KiB
#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
using pii=pair<int,int>;
using pli=pair<long long,int>;
const int maxn=2e5+9;
vector<pii>a[maxn];
int p[maxn];
const long long inf=-2e9-7;
long long dp[maxn][2];
long long cop[maxn][2];
void dfs1(int u,int par){
p[u]=par;
dp[u][1]=inf;
dp[u][0]=0;
for (auto v:a[u]){
    if (v.fi==par)continue;
    dfs1(v.fi,u);
    dp[u][0]+=max(dp[v.fi][1]+v.se,dp[v.fi][0]);
}
long long choose=inf,notchoose=0;
for (auto v:a[u]){
    if (v.fi==par)continue;
    choose=max(choose+max(dp[v.fi][0],dp[v.fi][1]+v.se),notchoose+dp[v.fi][0]+v.se);
    notchoose=notchoose+max(dp[v.fi][0],dp[v.fi][1]+v.se);
}
dp[u][1]=choose;
cop[u][0]=dp[u][0];
cop[u][1]=dp[u][1];
}
long long ans=0;
long long f[3];
long long g[maxn][2];
void reroot(int u,int par){
dp[u][0]=0;
dp[u][1]=inf;
for (auto v:a[u]){
    dp[u][0]+=max(dp[v.fi][1]+v.se,dp[v.fi][0]);
}
long long choose=inf,notchoose=0;
for (auto v:a[u]){
    //if (v.fi==par)continue;
    if (v.fi!=par){
        g[v.fi][0]=notchoose;
        g[v.fi][1]=choose;
    }
    choose=max(choose+max(dp[v.fi][0],dp[v.fi][1]+v.se),notchoose+dp[v.fi][0]+v.se);
    notchoose=notchoose+max(dp[v.fi][0],dp[v.fi][1]+v.se);
}
dp[u][1]=choose;
f[0]=0;
f[1]=inf;
f[2]=inf;
for (auto v:a[u]){
    f[2]=max(f[2]+max(dp[v.fi][0],dp[v.fi][1]+v.se),f[1]+dp[v.fi][0]+v.se);
    f[1]=max(f[1]+max(dp[v.fi][0],dp[v.fi][1]+v.se),f[0]+dp[v.fi][0]+v.se);
    f[0]+=max(dp[v.fi][0],dp[v.fi][1]+v.se);
}
ans=max(ans,f[2]);
choose=inf;
notchoose=0;
reverse(all(a[u]));
for (auto v:a[u]){
    //if (v.fi==par)continue;
    if (v.fi!=par){
        g[v.fi][1]+=notchoose;
        g[v.fi][0]+=choose;
    }
    choose=max(choose+max(dp[v.fi][0],dp[v.fi][1]+v.se),notchoose+dp[v.fi][0]+v.se);
    notchoose=notchoose+max(dp[v.fi][0],dp[v.fi][1]+v.se);
}
reverse(all(a[u]));
for (auto v:a[u]){
    if (v.fi==par)continue;
    long long c1=dp[u][0],c2=dp[u][1];
    dp[u][0]-=max(dp[v.fi][0],dp[v.fi][1]+v.se);
    dp[u][1]=max(g[v.fi][0],g[v.fi][1]);
    reroot(v.fi,u);
    dp[u][0]=c1;
    dp[u][1]=c2;
}
dp[u][0]=cop[u][0];
dp[u][1]=cop[u][1];
}
signed main(){
    srand(time(0));
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen(".INP","r",stdin);
    //freopen(".OUT","w",stdout);
    int n;
    cin>>n;
    for1(i,1,n-1){
    int u,v,w;
    cin>>u>>v>>w;
    a[u].pb({v,w});
    a[v].pb({u,w});
    }
    dfs1(1,0);
    ans=dp[1][0];
    reroot(1,0);
    cout<<ans;

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...