제출 #553563

#제출 시각아이디문제언어결과실행 시간메모리
553563kwongweng구슬과 끈 (APIO14_beads)C++17
13 / 100
1 ms468 KiB
#include <bits/stdc++.h> using namespace std; #pragma GCC target ("avx2") #pragma GCC optimization ("Ofast") #pragma GCC optimization ("unroll-loops") typedef long long ll; typedef vector<int> vi; typedef pair<int, int> ii; typedef vector<ii> vii; typedef long double ld; typedef pair<ll, ll> pll; #define FOR(i, a, b) for(int i = a; i < b; i++) #define ROF(i, a, b) for(int i = a; i >= b; i--) #define ms memset #define pb push_back #define fi first #define se second ll MOD = 1000000007; ll power(ll base, ll n){ if (n == 0) return 1; if (n == 1) return base; ll halfn = power(base, n/2); if (n % 2 == 0) return (halfn * halfn) % MOD; return (((halfn * halfn) % MOD) * base) % MOD; } ll inverse(ll n){ return power(n, MOD-2); } ll add(ll a, ll b){ return (a+b) % MOD; } ll mul(ll a, ll b){ a %= MOD; return (a*b) % MOD; } ll gcd(ll a, ll b){ if (a == 1) return 1; if (a == 0) return b; return gcd(b%a, a); } const int N = 10001; vii g[N]; int dp[N][4]; //dp[i][0] without edge i, with parent of i //dp[i][1] with the edge //dp[i][2-3] from below, void dfs(int u, int p){ dp[u][0]=0;dp[u][2]=0; vii incre_up, incre_down; for (ii edge : g[u]){ int v=edge.fi; int w=edge.se; if (v==p) continue; dfs(v,u); dp[u][0]+=max(dp[v][0],dp[v][1]+w); incre_up.pb({dp[v][0]+w-max(dp[v][0],dp[v][1]+w),v}); incre_down.pb({dp[v][2]+w-max(dp[v][0],dp[v][1]+w),v}); } sort(incre_up.rbegin(),incre_up.rend()); sort(incre_down.rbegin(),incre_down.rend()); int sz=incre_up.size(); if (sz==0){ dp[u][1]=-MOD; dp[u][3]=-MOD; return; } dp[u][1]=dp[u][0]+incre_up[0].fi; for (ii edge : g[u]){ int v = edge.fi; int w =edge.se; if (v==p) continue; dp[u][2]=max(dp[u][2],dp[u][0]-max(dp[v][0],dp[v][1]+w)+max(dp[v][2],dp[v][3]+w)); } if (sz>1){ if (incre_up[0].se!=incre_down[0].se){ dp[u][2]=dp[u][0]+incre_up[0].fi+incre_down[0].fi; }else{ dp[u][2]=dp[u][0]+max(incre_up[1].fi+incre_down[0].fi,incre_up[0].fi+incre_down[1].fi); } } dp[u][3]=dp[u][0]+max(incre_up[0].fi,incre_down[0].fi); //cout<<u<<" "; //FOR(i,0,4) cout<<dp[u][i]<<" "; //cout<<'\n'; //if (mini!=MOD) dp[u][1]=max(dp[u][0],dp[u][0]-mini+W); } void solve(){ int n; cin >> n; FOR(i,0,n-1){ int u,v,w; cin>>u>>v>>w; g[u].pb({v,w}); g[v].pb({u,w}); } dfs(1,0); cout<<max(dp[1][0],dp[1][2])<<'\n'; //dfs(1,0,-MOD); //cout<<max({dp[1][0],dp[1][1],dp[1][2],dp[1][3]}); return; } int main() { ios::sync_with_stdio(false); int TC = 1; //cin >> TC; FOR(i, 1, TC+1){ //cout << "Case #" << i << ": "; solve(); } }

컴파일 시 표준 에러 (stderr) 메시지

beads.cpp:5: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    5 | #pragma GCC optimization ("Ofast")
      | 
beads.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
    6 | #pragma GCC optimization ("unroll-loops")
      |
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...