Submission #865129

#TimeUsernameProblemLanguageResultExecution timeMemory
865129LittleOrangeDesignated Cities (JOI19_designated_cities)C++17
22 / 100
259 ms56040 KiB
#include<bits/stdc++.h> using namespace std; using ll = long long; struct obj{ ll to,c0,c1;// to, back }; int main(){ ios::sync_with_stdio(0);cin.tie(0); ll n,q; cin >> n; vector<vector<obj>> con(n); ll tot = 0; for(ll i = 1;i<n;i++){ ll a,b,c,d; cin >> a >> b >> c >> d;a--;b--; con[a].push_back({b,c,d}); con[b].push_back({a,d,c}); tot += c+d; } ll leaf_cnt = 0; for(auto &o : con) if (o.size()==1) leaf_cnt++; cin >> q; bool use16 = true; if (n<=16&&use16){ vector<ll> ans(n+1,tot); //vector<ll> ch(n+1,0); ll cur = tot; vector<vector<ll>> used(n,vector<ll>(n,0)); function<void(ll,ll)> dfs; dfs = [&](ll i, ll p){ for(obj l : con[i]){ if (l.to!=p){ if (!used[i][l.to]){ used[i][l.to] = 1; cur -= l.c1; } dfs(l.to,i); } } }; for(ll x = 1;x<(1<<n);x++){ cur = tot; for(ll i = 0;i<n;i++) for(ll j = 0;j<n;j++) used[i][j] = 0; ll lvl = __builtin_popcountll(x); for(ll i = 0;i<n;i++) if(x>>i&1) dfs(i,-1); //if (cur<ans[lvl]) ch[lvl] = x; ans[lvl] = min(ans[lvl],cur); } while(q--){ ll e; cin >> e; cout << ans[e] << "\n"; //for(ll i = 0;i<n;i++) cout << (ch[e]>>i&1); //cout << "\n"; } return 0; } vector<ll> dp(n,0); { function<ll(ll,ll)> dfs; dfs = [&](ll i, ll p){ ll cur = 0; for(obj l : con[i]){ if (l.to!=p){ cur += l.c1 + dfs(l.to,i); } } return cur; }; dp[0] = dfs(0,-1); } { function<void(ll,ll,ll)> dfs; dfs = [&](ll i, ll p, ll v){ dp[i] = v; for(obj l : con[i]){ if (l.to!=p){ dfs(l.to,i,v+l.c0-l.c1); } } }; dfs(0,-1,dp[0]); } ll ans_1 = tot; for(ll i : dp) ans_1 = min(tot-i,ans_1); ll ans_2 = tot; vector<ll> go(n,0),ch(n,0); { function<void(ll,ll)> dfs; dfs = [&](ll i, ll p){ ll de = 0; for(obj l : con[i]){ if (l.to!=p){ dfs(l.to,i); go[i] += go[l.to]+l.c0; de = max(de,go[l.to]+l.c0 - ch[l.to]); } } ch[i] = go[i] - de; }; dfs(0,-1); } { function<void(ll,ll,ll)> dfs; dfs = [&](ll i, ll p, ll v){ ll w = v; for(obj l : con[i]){ if (l.to!=p){ w += go[l.to]+l.c0; } } ll a = 0,b = 0; for(obj l : con[i]){ if (l.to!=p){ dfs(l.to,i,w-(go[l.to]+l.c0)+l.c1); ll cur = go[l.to]+l.c0 - ch[l.to]; if (cur>=a){ b = a; a = cur; }else b = max(b,cur); } } ans_2 = min(ans_2, w-a-b); }; dfs(0,-1, 0); } while(q--){ ll e; cin >> e; if (e>=leaf_cnt){ cout << "0\n"; continue; } if (e==1){ cout << ans_1 << "\n"; continue; } if (e==2){ cout << ans_2 << "\n"; continue; } } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...