Submission #330855

#TimeUsernameProblemLanguageResultExecution timeMemory
330855VROOM_VARUNElection Campaign (JOI15_election_campaign)C++14
100 / 100
335 ms46700 KiB
/* ID: varunra2 LANG: C++ TASK: election */ #include <bits/stdc++.h> using namespace std; #ifdef DEBUG #include "lib/debug.h" #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define debug_arr(...) \ cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__) #pragma GCC diagnostic ignored "-Wsign-compare" //#pragma GCC diagnostic ignored "-Wunused-parameter" //#pragma GCC diagnostic ignored "-Wunused-variable" #else #define debug(...) 42 #endif #define EPS 1e-9 #define IN(A, B, C) assert(B <= A && A <= C) #define INF (int)1e9 #define MEM(a, b) memset(a, (b), sizeof(a)) #define MOD 1000000007 #define MP make_pair #define PB push_back #define all(cont) cont.begin(), cont.end() #define rall(cont) cont.end(), cont.begin() #define x first #define y second const double PI = acos(-1.0); typedef long long ll; typedef long double ld; typedef pair<int, int> PII; typedef map<int, int> MPII; typedef multiset<int> MSETI; typedef set<int> SETI; typedef set<string> SETS; typedef vector<int> VI; typedef vector<PII> VII; typedef vector<VI> VVI; typedef vector<string> VS; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define trav(a, x) for (auto& a : x) #define sz(x) (int)(x).size() typedef pair<int, int> pii; typedef vector<int> vi; #pragma GCC diagnostic ignored "-Wsign-compare" // util functions struct LCA { VVI adj; VVI st; int n; VI tin; VI tout; int t = 0; const int bits = 20; void dfs(int u, int v) { st[u][0] = v; for (int i = 1; i < bits; i++) { int to = st[u][i - 1]; if (to == -1) st[u][i] = -1; else st[u][i] = st[to][i - 1]; } tin[u] = t++; for (auto& x : adj[u]) { if (x == v) continue; dfs(x, u); } tout[u] = t++; } void init(VVI& _adj) { adj = _adj; n = sz(adj); tin.resize(n); tout.resize(n); st.resize(n); trav(x, st) x.resize(bits); dfs(0, -1); } bool isAnc(int u, int v) { if (u == -1) return true; if (v == -1) return false; return tin[u] <= tin[v] and tout[u] >= tout[v]; } int getLCA(int u, int v) { if (isAnc(u, v)) return u; if (isAnc(v, u)) return v; for (int i = bits - 1; ~i; i--) { if (!isAnc(st[u][i], v)) u = st[u][i]; } return st[u][0]; } } lca; struct BIT { // range update, point query VI fen; int n; void init(int _n) { n = _n; fen.resize(n + 1); } void upd(int ind, int val) { ind++; while (ind <= n) { fen[ind] += val; ind += (ind & -ind); } } void upd(int l, int r, int val) { upd(l, val); upd(r + 1, -val); } int qry(int ind) { int ret = 0; ind++; while (ind >= 1) { ret += fen[ind]; ind -= (ind & -ind); } return ret; } }; VVI adj; VVI events; VI ord; VVI qrys; VI pos; VI dp; VI par; VI lran, rran; int n, m; void init() { events.resize(n); qrys.resize(m); pos.resize(n); lran.resize(n); rran.resize(n); par.resize(n); dp.resize(n); } void dfsord(int u, int v) { pos[u] = sz(ord); lran[u] = sz(ord); par[u] = v; ord.PB(u); trav(x, adj[u]) { if (x == v) continue; dfsord(x, u); } rran[u] = sz(ord) - 1; } BIT chldval, dpval; void dpdfs(int u, int v) { int chldsum = 0; trav(x, adj[u]) { if (x == v) continue; dpdfs(x, u); chldsum += dp[x]; } dp[u] = chldsum; trav(x, events[u]) { int uu, vv, c; uu = qrys[x][0], vv = qrys[x][1], c = qrys[x][2]; int sum = 0; c += chldval.qry(pos[uu]); c += chldval.qry(pos[vv]); c -= dpval.qry(pos[uu]); c -= dpval.qry(pos[vv]); c += chldsum; dp[u] = max(dp[u], c); } chldval.upd(lran[u], rran[u], chldsum); dpval.upd(lran[u], rran[u], dp[u]); } int main() { // #ifndef ONLINE_JUDGE // freopen("election.in", "r", stdin); // freopen("election.out", "w", stdout); // #endif cin.sync_with_stdio(0); cin.tie(0); cin >> n; adj.resize(n); for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; u--, v--; adj[u].PB(v); adj[v].PB(u); } cin >> m; init(); lca.init(adj); for (int i = 0; i < m; i++) { qrys[i].resize(3); cin >> qrys[i][0] >> qrys[i][1] >> qrys[i][2]; qrys[i][0]--, qrys[i][1]--; int lc = lca.getLCA(qrys[i][0], qrys[i][1]); events[lc].PB(i); } dfsord(0, -1); chldval.init(n); dpval.init(n); dpdfs(0, -1); cout << dp[0] << '\n'; return 0; }

Compilation message (stderr)

election_campaign.cpp: In function 'void dpdfs(int, int)':
election_campaign.cpp:190:9: warning: unused variable 'sum' [-Wunused-variable]
  190 |     int sum = 0;
      |         ^~~
#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...