#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; const ll mod=1000000007; mt19937 mrand(chrono::high_resolution_clock::now().time_since_epoch().count()); int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head ll inf=1ll<<60; int T=1000; const int N=201000; int n; vector<PII> e[N]; ll dp[N][4],ff[10100][2],gg[10100][2]; void dfs(int u,int f) { vector<array<ll,4>> px; for (auto p:e[u]) if (p.fi!=f) { int v=p.fi; dfs(v,u); ll p1=dp[v][0]+p.se,p2=dp[v][1]+p.se,p3=dp[v][2]+p.se,p0=max(dp[v][3]+p.se,dp[v][0]); px.pb({p0,p1,p2,p3}); } int tt=max(min(T,SZ(px)/2+1),1); shuffle(all(px),mrand); for (int i=0;i<=2*tt;i++) ff[i][0]=ff[i][1]=-inf; ff[tt][0]=0; for (auto p:px) { for (int i=0;i<=2*tt;i++) rep(j,0,2) gg[i][j]=ff[i][j],ff[i][j]=gg[i][j]+p[0]; rep(i,0,2*tt) rep(j,0,2) ff[i][j]=max(ff[i][j],gg[i+1][j]+p[1]); rep(i,0,2*tt) rep(j,0,2) ff[i+1][j]=max(ff[i+1][j],gg[i][j]+p[3]); rep(i,0,2*tt+1) rep(j,0,2) ff[i][j^1]=max(ff[i][j^1],gg[i][j]+p[2]); } dp[u][0]=ff[tt][0]; dp[u][1]=ff[tt-1][0]; dp[u][3]=ff[tt+1][0]; dp[u][2]=ff[tt][1]; //printf("%d %lld %lld %lld\n",u,dp[u][0],dp[u][1],dp[u][2]); } int main() { scanf("%d",&n); rep(i,1,n) { int u,v,w; scanf("%d%d%d",&u,&v,&w); e[u].pb({v,w}); e[v].pb({u,w}); } dfs(1,0); printf("%lld\n",dp[1][0]); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; const ll mod=1000000007; mt19937 mrand(chrono::high_resolution_clock::now().time_since_epoch().count()); int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head ll inf=1ll<<60; int T=1000; const int N=201000; int n; vector<PII> e[N]; ll dp[N][4],ff[10100][2],gg[10100][2]; void dfs(int u,int f) { vector<array<ll,4>> px; for (auto p:e[u]) if (p.fi!=f) { int v=p.fi; dfs(v,u); ll p1=dp[v][0]+p.se,p2=dp[v][1]+p.se,p3=dp[v][2]+p.se,p0=max(dp[v][3]+p.se,dp[v][0]); px.pb({p0,p1,p2,p3}); } int tt=max(min(T,SZ(px)/2+1),1); shuffle(all(px),mrand); for (int i=0;i<=2*tt;i++) ff[i][0]=ff[i][1]=-inf; ff[tt][0]=0; for (auto p:px) { for (int i=0;i<=2*tt;i++) rep(j,0,2) gg[i][j]=ff[i][j],ff[i][j]=gg[i][j]+p[0]; rep(i,0,2*tt) rep(j,0,2) ff[i][j]=max(ff[i][j],gg[i+1][j]+p[1]); rep(i,0,2*tt) rep(j,0,2) ff[i+1][j]=max(ff[i+1][j],gg[i][j]+p[3]); rep(i,0,2*tt+1) rep(j,0,2) ff[i][j^1]=max(ff[i][j^1],gg[i][j]+p[2]); } dp[u][0]=ff[tt][0]; dp[u][1]=ff[tt-1][0]; dp[u][3]=ff[tt+1][0]; dp[u][2]=ff[tt][1]; //printf("%d %lld %lld %lld\n",u,dp[u][0],dp[u][1],dp[u][2]); } int main() { scanf("%d",&n); rep(i,1,n) { int u,v,w; scanf("%d%d%d",&u,&v,&w); e[u].pb({v,w}); e[v].pb({u,w}); } dfs(1,0); printf("%lld\n",dp[1][0]); } |