# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
103606 | luciocf | Election Campaign (JOI15_election_campaign) | C++14 | 260 ms | 20884 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;
const int maxn = 1e5+10;
int n, m;
int A[maxn], B[maxn], C[maxn];
int nivel[maxn], pai[maxn], sz[maxn];
int chain[maxn], head[maxn], pos[maxn];
int cc, pp;
int bit[maxn];
int dp[maxn];
vector<int> grafo[maxn], op[maxn];
void upd(int x, int v)
{
for (; x < maxn; x += (x&-x))
bit[x] += v;
}
int soma(int x)
{
int ans = 0;
for (; x > 0; x -= (x&-x))
ans += bit[x];
return ans;
}
void dfs(int u, int p)
{
sz[u] = 1;
for (auto v: grafo[u])
{
if (v == p) continue;
pai[v] = u, nivel[v] = nivel[u]+1;
dfs(v, u);
sz[u] += sz[v];
}
}
void hld(int u)
{
if (!head[cc]) head[cc] = u;
chain[u] = cc, pos[u] = ++pp;
int maior = -1, ind = -1;
for (auto v: grafo[u])
if (v != pai[u] && sz[v] > maior)
maior = sz[v], ind = v;
if (ind != -1) hld(ind);
for (auto v: grafo[u])
if (v != pai[u] && v != ind)
cc++, hld(v);
}
int lca(int u, int v)
{
while (chain[u] != chain[v])
{
if (nivel[head[chain[u]]] > nivel[head[chain[v]]])
u = pai[head[chain[u]]];
else
v = pai[head[chain[v]]];
}
if (nivel[u] > nivel[v]) return v;
return u;
}
int query(int u, int v)
{
int ans = 0;
while (true)
{
if (chain[u] == chain[v]) return ans+soma(pos[u])-soma(pos[v]);
ans += soma(pos[u])-soma(pos[head[chain[u]]]-1);
u = pai[head[chain[u]]];
}
}
int solve(int u, int p)
{
int ans = 0;
for (auto v: grafo[u])
{
if (v == p) continue;
solve(v, u);
ans += dp[v];
}
dp[u] = ans;
int cur = 0;
for (auto i: op[u])
{
int a = A[i], b = B[i], c = C[i];
int x = query(a, u) + query(b, u);
if (c > x && (ans-x+c > dp[u] || ans-x+c == dp[u] && c < cur))
{
dp[u] = ans-x+c;
upd(pos[u], c-cur);
cur = c;
}
}
}
int main(void)
{
scanf("%d", &n);
for (int i = 1; i < n; i++)
{
int u, v;
scanf("%d %d", &u, &v);
grafo[u].push_back(v); grafo[v].push_back(u);
}
dfs(1, 0);
hld(1);
scanf("%d", &m);
for (int i = 1; i <= m; i++)
{
scanf("%d %d %d", &A[i], &B[i], &C[i]);
op[lca(A[i], B[i])].push_back(i);
}
solve(1, 0);
printf("%d\n", dp[1]);
}
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... |