제출 #1128513

#제출 시각아이디문제언어결과실행 시간메모리
1128513cbnhtmanhElection Campaign (JOI15_election_campaign)C++20
100 / 100
156 ms41276 KiB
#include <bits/stdc++.h> #define fi(i, a, b) for( int i = a; i <= b; i++ ) #define fid(i, a, b) for( int i = a; i >= b; i-- ) #define getbit(x, i) ((x>>i)&1) #define ll long long #define pb push_back #define pii pair<int,int> #define pli pair<ll,int> #define pll pair<ll,ll> #define st first #define nd second #define mp make_pair #define HTManh "" #define maxn 100009 #define endl '\n' using namespace std; int n; vector<int> a[100009]; int m; struct dl { int u, v; int c; } tv[100009]; bool check() { fi(u,1,n) for(int v: a[u]) if (u < v && u != v-1) return 0; return 1; } vector<pii> truyvan[100009]; ll mx[100009]; void sub23() { //cout << "gay" << endl; fi(i,1,m) { int u = tv[i].u, v = tv[i].v, c = tv[i].c; if (u > v) swap(u, v); truyvan[v].pb({u, c}); } fi(i,1,n) { ll gt = 0; for(pii tg: truyvan[i]) { gt = max(gt, mx[tg.st - 1] + tg.nd); } mx[i] = max(mx[i-1], gt); } cout << mx[n] << endl; } int h[100009]; int euler[200009], cnt; int dau[200009]; void dfs(int u, int pre = 0) { euler[++cnt] = u; dau[u] = cnt; for(int v: a[u]) { if (v == pre) continue; h[v] = h[u] + 1; dfs(v,u); euler[++cnt] = u; } } int anc[200009][21]; void build_lca() { fi(i,1,cnt) anc[i][0] = euler[i]; fi(k,1,__lg(cnt)) { fi(i,1,cnt-(1<<k)+1) { anc[i][k] = (h[anc[i][k-1]] < h[anc[i+(1<<(k-1))][k-1]]) ? anc[i][k-1] : anc[i+(1<<(k-1))][k-1]; } } } int lca(int u, int v) { if (dau[u] > dau[v]) swap(u,v); int k = __lg(dau[v]-dau[u]+1); return (h[anc[dau[u]][k]] < h[anc[dau[v]-(1<<k)+1][k]] ? anc[dau[u]][k] : anc[dau[v]-(1<<k)+1][k]); } int dis(int u, int v) { return h[u] + h[v] - 2*h[lca(u, v)]; } void sub1() { dfs(1); build_lca(); ll res = 0; fi(x,1,(1<<m)-1) { bool flag = 1; fi(i,1,m) if (getbit(x,(i-1))) { fi(j,1,i-1) if (getbit(x,(j-1))) { int dinh1 = lca(tv[i].u, tv[i].v); int dinh2 = lca(tv[j].u, tv[j].v); if (dis(tv[i].u, tv[i].v) == dis(tv[i].u, dinh2) + dis(tv[i].v, dinh2)) flag = 0; if (dis(tv[j].u, tv[j].v) == dis(tv[j].u, dinh1) + dis(tv[j].v, dinh1)) flag = 0; } } if (!flag) continue; ll kq = 0; fi(i,1,m) if (getbit(x,(i-1))) kq += tv[i].c; res = max(res, kq); } cout << res << endl; } vector<pair<pii, int>> query[100009]; int st[100009], ed[100009], dem; void dfs2(int u, int pre = 0) { st[u] = ++dem; for(int v: a[u]) { if (v == pre) continue; dfs2(v,u); } ed[u] = dem; } ll dp[100009][2]; ll bit[100009][2]; void update(int tt, int vt, int gt) { while (vt <= n) { bit[vt][tt] += gt; vt += vt & -vt; } } ll get(int tt, int vt) { ll s = 0; while(vt > 0) { s += bit[vt][tt]; vt -= vt & -vt; } return s; } void solve(int u, int pre = 0) { for(int v: a[u]) { if (v == pre) continue; solve(v,u); dp[u][0] += max(dp[v][0], dp[v][1]); } update(0,st[u],dp[u][0]); update(0,ed[u]+1,-dp[u][0]); for(pair<pii, int> tg: query[u]) { int x = tg.st.st, y = tg.st.nd, gt = tg.nd; if (h[x] > h[y]) swap(x,y); //cout << x << " " << y << " " << gt << endl; if (x == u) { dp[u][1] = max(dp[u][1], get(0, st[y]) - get(1, st[y]) + gt); } else { dp[u][1] = max(dp[u][1], get(0, st[x]) - get(1, st[x]) + get(0, st[y]) - get(1, st[y]) + gt - dp[u][0]); } } ll gt = max(dp[u][0], dp[u][1]); update(1,st[u],gt); update(1,ed[u]+1,-gt); } void subac() { dfs(1); build_lca(); dfs2(1); fi(i,1,m) { int dinh = lca(tv[i].u, tv[i].v); query[dinh].pb({{tv[i].u, tv[i].v}, tv[i].c}); } solve(1); cout << max(dp[1][0], dp[1][1]) << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); if (fopen(HTManh".inp", "r")) { freopen(HTManh".inp", "r", stdin); freopen(HTManh".out", "w", stdout); } cin >> n; fi(i,1,n-1) { int x, y; cin >> x >> y; a[x].pb(y); a[y].pb(x); } cin >> m; fi(i,1,m) cin >> tv[i].u >> tv[i].v >> tv[i].c; if (check()) sub23(); else { if (m <= 15) sub1(); else { subac(); } } }

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

election_campaign.cpp: In function 'int main()':
election_campaign.cpp:215:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  215 |         freopen(HTManh".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:216:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  216 |         freopen(HTManh".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...