제출 #198925

#제출 시각아이디문제언어결과실행 시간메모리
198925red1108구슬과 끈 (APIO14_beads)C++17
100 / 100
335 ms41064 KiB
#include<bits/stdc++.h> #include<ext/rope> using namespace std; using namespace __gnu_cxx; #define fi first #define se second #define fastio ios_base::sync_with_stdio(false);cin.tie(0) #define fopen freopen("input.txt", "r", stdin) #define eb emplace_back #define em emplace #define prec(a) cout<<fixed;cout.precision(a); #define all(a) (a).begin(), (a).end() typedef long long ll; typedef long double ld; typedef pair<int,int> pii; typedef pair<ll,ll> pll; typedef tuple<int,int,int> tiii; const ll INF = 2e16; const ll inf = 2e9; template<class T> void pr(T t) {cout << t << " ";} template<class T, class ...Args> void pr(T a, Args ...args) {cout << a << " ";pr(args...);} template<class ...Args> void prl(Args ...args) {pr(args...);cout << endl;} int n; const int MAXN=200010; vector<pll> tree[MAXN]; ll dp1[MAXN],dp2[MAXN],dp3[MAXN],dp4[MAXN],ans; void dfs(int x, int par, ll bac){ dp2[x]=dp3[x]=dp4[x]=-inf; ll sum=0; vector<pll> h1,h3,h4,hh; int c=0; for(auto i:tree[x]) if(i.fi!=par){ dfs(i.fi,x,i.se); ll tmp = max(dp1[i.fi], dp2[i.fi]); dp1[x]+=tmp; sum+=tmp; c++; h1.eb(dp1[i.fi]+i.se-tmp,i.fi); h3.eb(dp3[i.fi]+i.se-tmp,i.fi); h4.eb(max(dp3[i.fi]+i.se,dp4[i.fi])-tmp,i.fi); hh.eb(max(dp3[i.fi],dp4[i.fi])-tmp,i.fi); } sort(all(h1));reverse(all(h1)); sort(all(h3));reverse(all(h3)); sort(all(h4));reverse(all(h4)); sort(all(hh));reverse(all(hh)); if(c>=1){ dp2[x] = max(dp2[x],sum + h1[0].fi + bac); dp4[x] = max(dp4[x],sum + h3[0].fi + bac); dp3[x] = max(dp3[x],sum + hh[0].fi); } if(c>=2){ for(int i=0;i<2;i++) for(int j=0;j<2;j++) if(h1[i].se!=h3[j].se) dp3[x] = max(dp3[x], sum + h1[i].fi+h3[j].fi); dp3[x] = max(dp3[x], sum + h1[0].fi+h1[1].fi); } } int main(){ fastio; cin>>n; for(int i=1;i<n;i++){ int a, b, c;cin>>a>>b>>c; tree[a].eb(b,c); tree[b].eb(a,c); } dfs(1,0,0); cout<<max(dp1[1],dp3[1]); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...