# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
56841 | Bruteforceman | Election Campaign (JOI15_election_campaign) | C++11 | 622 ms | 143952 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 <bits/stdc++.h>
using namespace std;
vector <int> g[100010];
int anc[20][100010];
const int logn = 19;
int dep[100010];
vector <int> path[100010];
int a[100010], b[100010], c[100010];
void dfs(int x, int par) {
anc[0][x] = par;
for(int i = 1; i <= logn; i++) {
anc[i][x] = anc[i - 1][anc[i - 1][x]];
}
for(auto i : g[x]) {
if(i - par) {
dep[i] = 1 + dep[x];
dfs(i, x);
}
}
}
int lca(int p, int q) {
if(dep[p] > dep[q]) swap(p, q);
for(int i = logn; i >= 0; i--) {
if(dep[q] - (1 << i) >= dep[p]) {
q = anc[i][q];
}
}
if(p == q) return p;
for(int i = logn; i >= 0; i--) {
if(anc[i][p] != anc[i][q]) {
p = anc[i][p];
q = anc[i][q];
}
}
return anc[0][p];
}
long long dp[100010];
long long sum[100010];
long long sib[100010];
long long l[100010], r[100010];
int tym;
int n;
void order(int x, int par) {
l[x] = ++tym;
for(int i : g[x]) {
if(i - par) {
order(i, x);
}
}
r[x] = tym;
}
long long t[100010];
void update(int x, long long val) {
for(int i = x; i <= n; i += i & (-i)) {
t[i] += val;
}
}
long long query(int x) {
long long ans = 0;
for(int i = x; i > 0; i -= i & (-i)) {
ans += t[i];
}
return ans;
}
long long query(int l, int r) {
return query(r) - query(l - 1);
}
int get(int x, int p) {
for(int i = logn; i >= 0; i--) {
if(dep[x] - (1 << i) >= p) {
x = anc[i][x];
}
}
return x;
}
void calc(int x, int par) {
dp[x] = 0;
for(auto i : g[x]) {
if (i - par) {
calc(i, x);
dp[x] += dp[i];
}
}
sum[x] = dp[x];
for(auto i : g[x]) {
if(i - par) {
sib[i] = sum[x] - dp[i];
}
}
for(auto i : path[x]) {
int node = get(a[i], dep[x] + 1);
long long res = c[i];
res += query(l[a[i]]);
if(node != x) res -= dp[node];
node = get(b[i], dep[x] + 1);
res += query(l[b[i]]);
if(node != x) res -= dp[node];
res += sum[x];
if(a[i] != x) res += sum[a[i]];
if(b[i] != x) res += sum[b[i]];
dp[x] = max(dp[x], res);
}
for(auto i : g[x]) {
if(i - par) {
update(l[i], sib[i]);
update(r[i] + 1, -sib[i]);
}
}
}
int main(int argc, char const *argv[])
{
int m;
scanf("%d", &n);
for(int i = 1; i < n; i++) {
int p, q;
scanf("%d %d", &p, &q);
g[p].emplace_back(q);
g[q].emplace_back(p);
}
dfs(1, 0);
order(1, 0);
scanf("%d", &m);
for(int i = 1; i <= m; i++) {
scanf("%d %d %d", &a[i], &b[i], &c[i]);
path[lca(a[i], b[i])].emplace_back(i);
}
calc(1, 0);
printf("%lld\n", dp[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... |