# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
187634 | dndhk | Election Campaign (JOI15_election_campaign) | C++14 | 1067 ms | 29364 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>
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAX_N = 100000;
const ll INFLL = 10000;
const int MAX_K = 20;
int N, M;
vector<int> gp[MAX_N+1];
int lv[MAX_N+1], p[MAX_N+1], up[MAX_N+1][MAX_K+1];
void dfs(int x){
up[x][0] = p[x];
for(int i=1; i<MAX_K; i++) up[x][i] = up[up[x][i-1]][i-1];
for(int i : gp[x]){
if(i==p[x]) continue;
lv[i] = lv[x]+1;
p[i] = x;
dfs(i);
}
}
int lca(int x, int y){
for(int i=MAX_K-1; i>=0; i--){
if(lv[up[x][i]]>=lv[y]) x = up[x][i];
if(lv[up[y][i]]>=lv[x]) y = up[y][i];
}
if(x==y) return x;
for(int i=MAX_K-1; i>=0; i--){
if(up[x][i]!=up[y][i]) {
x = up[x][i];
y = up[y][i];
}
}
return up[x][0];
}
int dist(int x, int y){
int ret = 0;
for(int i=MAX_K-1; i>=0; i--){
if(lv[up[x][i]]>=lv[y]) {
x = up[x][i];
ret+=(1<<i);
}
if(lv[up[y][i]]>=lv[x]) {
y = up[y][i];
ret+=(1<<i);
}
}
if(x==y) return ret;
for(int i=MAX_K-1; i>=0; i--){
if(up[x][i]!=up[y][i]) {
x = up[x][i];
y = up[y][i];
ret+=(1<<(i+1));
}
}
return ret+2;
}
int upk(int x, int y){
for(int i=MAX_K-1; i>=0; i--){
if(y>=(1<<i)){
x = up[x][i];
y-=(1<<i);
}
}
return x;
}
struct Q{
int a, b, c;
};
vector<Q> query[MAX_N+1];
int dp1[MAX_N+1], dp2[MAX_N+1], dp3[MAX_N+1];
void dfs2(int x){
for(int i : gp[x]){
if(i==p[x]) continue;
dfs2(i);
dp2[x]+=dp1[i];
}
dp3[x] = 0;
for(Q q : query[x]){
int t = q.a;
int sum = 0;
while(t!=x){
sum+=dp2[t]-dp1[t];
t = p[t];
}
t = q.b;
while(t!=x){
sum+=dp2[t]-dp1[t];
t=p[t];
}
dp3[x] = max(dp3[x], dp2[x]+sum+q.c);
}
dp1[x] = max(dp2[x], dp3[x]);
}
int main(){
scanf("%d", &N);
for(int i=1; i<N; i++){
int a, b; scanf("%d%d", &a, &b);
gp[a].pb(b); gp[b].pb(a);
}
lv[1] = 1;dfs(1);
scanf("%d", &M);
for(int i=1; i<=M; i++){
int a, b, c; scanf("%d%d%d", &a, &b, &c);
query[lca(a, b)].pb({a, b, c});
}
dfs2(1);
cout<<dp1[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... |