# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
106176 | cki86201 | Election Campaign (JOI15_election_campaign) | C++11 | 445 ms | 32560 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
#include <unordered_set>
#include <bitset>
#include <time.h>
#include <limits.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define Fi first
#define Se second
#define pb push_back
#define szz(x) (int)x.size()
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) x.begin(),x.end()
typedef tuple<int, int, int> t3;
int N, M;
vector <int> E[100010];
int In[100010][3];
vector <int> qs[100010];
int ST[100010], EN[100010], cs;
int dep[100010];
int up[100010][20];
void pdfs(int x, int fa) {
ST[x] = ++cs;
for(int e : E[x]) if(e != fa) {
dep[e] = dep[x] + 1;
up[e][0] = x;
for(int i=1;i<20;i++) up[e][i] = up[ up[e][i-1] ][i-1];
pdfs(e, x);
}
EN[x] = cs;
}
int get_lca(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
rep(i, 20) if(1<<i & (dep[x] - dep[y])) x = up[x][i];
for(int i=19;i>=0;i--) if(up[x][i] != up[y][i]) x = up[x][i], y = up[y][i];
return x == y ? x : up[x][0];
}
ll T[100010];
void update_s(int x, ll v) {
int l = ST[x], r = EN[x];
for(int i=l;i<=N;i+=(i&-i)) T[i] += v;
for(int i=r+1;i<=N;i+=(i&-i)) T[i] -= v;
}
ll read(int x) {
ll res = 0;
for(int i=ST[x];i;i-=(i&-i)) res += T[i];
return res;
}
ll A[100010], B[100010];
void dfs(int x, int fa) {
for(int e : E[x]) if(e != fa) {
dfs(e, x);
B[x] += A[e];
}
A[x] = B[x];
for(int e : qs[x]) {
int u = In[e][0], v = In[e][1], cost = In[e][2];
ll val = B[x] + cost - read(u) - read(v);
if(A[x] < val) A[x] = val;
}
update_s(x, A[x] - B[x]);
}
int main() {
scanf("%d", &N);
for(int i=1;i<N;i++) {
int x, y; scanf("%d%d", &x, &y);
E[x].pb(y);
E[y].pb(x);
}
scanf("%d", &M);
pdfs(1, -1);
for(int i=1;i<=M;i++) {
int x, y, z; scanf("%d%d%d", &x, &y, &z);
int lca = get_lca(x, y);
qs[lca].pb(i);
In[i][0] = x; In[i][1] = y; In[i][2] = z;
}
dfs(1, -1);
printf("%lld\n", A[1]);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |