# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
373367 | BartolM | Election Campaign (JOI15_election_campaign) | C++17 | 246 ms | 32644 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;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;
const int INF=0x3f3f3f3f;
const int LOG=17;
const int N=1e5+5;
int n, q, dtime=1;
int L[N], R[N];
int par[LOG][N], dep[N];
vector <ppi> v[N];
vector <int> ls[N];
ll dp[N], F[2*N];
ll query(int x) {
ll ret=0;
for (; x>0; x-=x&-x) ret+=F[x];
return ret;
}
void update(int x, ll val) {
for (; x<=2*n; x+=x&-x) F[x]+=val;
}
void build() {
for (int i=1; i<LOG; ++i) {
for (int j=1; j<=n; ++j) {
par[i][j]=par[i-1][par[i-1][j]];
}
}
}
int digni(int x, int d) {
for (int i=LOG-1; i>=0; --i) {
if (dep[x]-(1<<i)>=d) x=par[i][x];
}
return x;
}
int lca(int x, int y) {
if (dep[x]<dep[y]) swap(x, y);
x=digni(x, dep[y]);
if (x==y) return x;
for (int i=LOG-1; i>=0; --i) {
if (par[i][x]!=par[i][y]) x=par[i][x], y=par[i][y];
}
return par[0][x];
}
void dfs(int node, int rod) {
dep[node]=dep[rod]+1;
L[node]=dtime++;
par[0][node]=rod;
for (int sus:ls[node]) if (sus!=rod) dfs(sus, node);
R[node]=dtime++;
}
ll rek(int node) {
ll sum=0, ret=0;
for (int sus:ls[node]) {
if (sus==par[0][node]) continue;
ret=max(ret, rek(sus));
sum+=dp[sus];
}
dp[node]=sum;
for (ppi pp:v[node]) {
ll curr=sum+pp.Y;
if (pp.X.X!=node) curr+=query(L[pp.X.X]);
if (pp.X.Y!=node) curr+=query(L[pp.X.Y]);
dp[node]=max(dp[node], curr);
}
ret=max(ret, dp[node]);
update(L[node], sum-dp[node]);
update(R[node], dp[node]-sum);
return ret;
}
void load() {
scanf("%d", &n);
for (int i=0; i<n-1; ++i) {
int a, b;
scanf("%d %d", &a, &b);
ls[a].pb(b); ls[b].pb(a);
}
dfs(1, 0);
build();
scanf("%d", &q);
for (int i=0; i<q; ++i) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
int lc=lca(a, b);
v[lc].pb(mp(mp(a, b), c));
}
}
int main() {
load();
printf("%lld\n", rek(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... |