Submission #906732

#TimeUsernameProblemLanguageResultExecution timeMemory
906732boxElection Campaign (JOI15_election_campaign)C++17
100 / 100
126 ms31060 KiB
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL template <class T, class... U> void bug_h(const T& t, const U&... u) { cerr << t; ((cerr << " | " << u), ...); cerr << endl; } #define bug(...) cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: ", bug_h(__VA_ARGS__) #else #define cerr if (0) cerr #define bug(...) #endif #define ar array #define all(v) std::begin(v), std::end(v) #define sz(v) int(std::size(v)) typedef long long i64; typedef vector<int> vi; typedef pair<int, int> pi; const int MAXN = 1e5; int N; vi adj[MAXN]; int depth[MAXN], jmp[MAXN][17], tin[MAXN], tout[MAXN], tt; vector<ar<int, 3>> paths[MAXN]; i64 dp[MAXN], dq[MAXN]; struct { i64 t[MAXN]; void add(int i, i64 x) { while (i < MAXN) { t[i] += x; i |= i+1; } } i64 qry(int i) { i64 x = 0; while (i >= 0) { x += t[i]; i &= i+1, i--; } return x; } void add(int l, int r, i64 x) { add(l, x), add(r+1, -x); } } T; void dfs(int i) { tin[i] = tt++; for (int l = 1; l < 17; l++) jmp[i][l] = jmp[jmp[i][l-1]][l-1]; for (int j : adj[i]) if (jmp[i][0]^j) { depth[j] = depth[i]+1; jmp[j][0] = i; dfs(j); } tout[i] = tt-1; } int jump(int i, int d) { for (int l = 0; l < 17; l++) if (d>>l&1) i = jmp[i][l]; return i; } int lca(int i, int j) { if (depth[i] < depth[j]) swap(i, j); i = jump(i, depth[i]-depth[j]); if (i == j) return i; for (int l = 16; l >= 0; l--) if (jmp[i][l]^jmp[j][l]) i = jmp[i][l], j = jmp[j][l]; return jmp[i][0]; } void rec(int i) { for (int j : adj[i]) if (jmp[i][0]^j) rec(j); for (int j : adj[i]) if (jmp[i][0]^j) { T.add(tin[i]+1, tout[i], dp[j]); T.add(tin[j], tout[j], -dp[j]); dq[i] += dp[j]; } dp[i] = dq[i]; for (auto [x, y, w] : paths[i]) if (x^i) dp[i] = max(dp[i], w+T.qry(tin[x])+T.qry(tin[y])-dq[i]+dq[x]+dq[y]); else dp[i] = max(dp[i], w+T.qry(tin[y])+dq[y]); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> N; for (int i = 0; i < N-1; i++) { int x, y; cin >> x >> y, x--, y--; adj[x].push_back(y); adj[y].push_back(x); } jmp[0][0] = 0; dfs(0); int M; cin >> M; while (M--) { int x, y, w; cin >> x >> y >> w, x--, y--; if (depth[x] > depth[y]) swap(x, y); int z = lca(x, y); paths[z].push_back({x, y, w}); } rec(0); cout << dp[0] << '\n'; }
#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...