제출 #198268

#제출 시각아이디문제언어결과실행 시간메모리
198268alishahali1382구슬과 끈 (APIO14_beads)C++14
100 / 100
286 ms36476 KiB
#include <bits/stdc++.h> #pragma GCC optimize ("O2") #pragma GCC optimize ("unroll-loops") //#pragma GCC optimize("no-stack-protector,fast-math") #define int long long using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<pii, int> piii; typedef pair<ll, ll> pll; #define debug(x) cerr<<#x<<'='<<(x)<<endl; #define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl; #define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl; #define debugv(v) cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl; #define all(x) x.begin(), x.end() #define pb push_back #define kill(x) return cout<<x<<'\n', 0; const ld eps=1e-7; const int inf=2000000010; const ll INF=10000000000000010LL; const int mod = 1000000007; const int MAXN = 200010, LOG=20; int n, m, k, u, v, x, y, t, a, b, ans; int dp[MAXN][2]; int sum[MAXN]; //int dp2[MAXN][2]; pii mx[MAXN][2]; vector<pii> G[MAXN]; void dfs1(int node, int par){ dp[node][0]=0; dp[node][1]=-inf; for (pii p:G[node]) if (p.first!=par) dfs1(p.first, node); sum[node]=0; for (pii p:G[node]) if (p.first!=par) sum[node]+=max(dp[p.first][0], dp[p.first][1]+p.second); dp[node][0]=sum[node]; mx[node][0]=mx[node][1]={-inf, 0}; for (pii p:G[node]) if (p.first!=par){ pii tmp={min(p.second, dp[p.first][0]-dp[p.first][1]), p.first}; if (tmp>mx[node][1]) mx[node][1]=tmp; if (mx[node][1]>mx[node][0]) swap(mx[node][0], mx[node][1]); } dp[node][1]=sum[node]+mx[node][0].first; //dp[node][0]=sum; } void dfs2(int node, int par){ ans=max(ans, dp[node][0]); int parw=0; for (pii p:G[node]) if (p.first==par) parw=p.second; for (pii p:G[node]) if (p.first!=par){ int v=p.first, w=p.second; int nd0=dp[node][0], nd1=dp[node][1], ns=sum[node]; int vd0=dp[v][0], vd1=dp[v][1], vs=sum[v]; sum[node]-=max(dp[v][0], dp[v][1]+w); dp[node][0]=sum[node]; dp[node][1]=-inf; dp[node][1]=sum[node]+mx[node][0].first; if (v==mx[node][0].second) dp[node][1]=sum[node]+mx[node][1].first; if (node!=1) dp[node][1]=max(dp[node][1], sum[node]+min(parw, dp[par][0]-dp[par][1])); //for (pii pp:G[node]) if (/*pp.first!=par && */pp.first!=v) dp[node][1]=max(dp[node][1], sum[node]+min(pp.second, dp[pp.first][0]-dp[pp.first][1])); sum[v]+=max(dp[node][0], dp[node][1]+w); dp[v][0]=sum[v]; dp[v][1]=max(sum[v]+mx[v][0].first, sum[v]+min(w, dp[node][0]-dp[node][1])); dfs2(v, node); dp[node][0]=nd0; dp[node][1]=nd1; sum[node]=ns; dp[v][0]=vd0; dp[v][1]=vd1; sum[v]=vs; } } int32_t main(){ ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); cin>>n; //assert(n<=10000); for (int i=1; i<n; i++){ cin>>u>>v>>x; G[u].pb({v, x}); G[v].pb({u, x}); } /* for (int i=1; i<=n; i++){ for (int j=1; j<=n; j++) dp[j][0]=0, dp[j][1]=-inf, sum[j]=0; dfs1(i, i); ans=max(ans, dp[i][0]); //debug2(i, dp[i][0]) }*/ dfs1(1, 1); dfs2(1, 1); cout<<ans<<'\n'; return 0; } /* 5 1 2 10 1 3 40 1 4 15 1 5 20 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 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...