#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int mxn = 1e5+10;
int N,Q,rt;
vector<int> tree[mxn];
vector<pair<pii,int>> req[mxn];
inline char readchar() {
const int S = 1<<20; // buffer size
static char buf[S], *p = buf, *q = buf;
if(p == q && (q = (p=buf)+fread(buf,1,S,stdin)) == buf) return EOF;
return *p++;
}
inline int nextint() {
int x = 0, c = readchar(), neg = false;
while(('0' > c || c > '9') && c!='-' && c!=EOF) c = readchar();
if(c == '-') neg = true, c = getchar();
while('0' <= c && c <= '9') x = x*10 + (c^'0'), c = readchar();
if(neg) x = -x;
return x; // returns 0 if EOF
}
namespace HLD{
int idx[mxn],link_top[mxn],par[mxn],dep[mxn],bs[mxn],sz[mxn];
ll bit[mxn];
void modify(int p,ll v){
for(;p<mxn;p+=p&-p)bit[p] += v;
return;
}
ll getval(int s,int e){
int re = 0;
for(;e>0;e-= e&-e)re += bit[e];
s--;
for(;s>0;s-= s&-s)re -= bit[s];
return re;
}
int cnt = 0;
void dfs1(int now){
sz[now] = 1;
for(auto nxt:tree[now]){
if(nxt == par[now])continue;
par[nxt] = now;
dep[nxt] = dep[now]+1;
dfs1(nxt);
sz[now] += sz[nxt];
if(!bs[now]||sz[bs[now]]<sz[nxt])bs[now] = nxt;
}
return;
}
void dfs2(int now,int top){
idx[now] = ++cnt;
link_top[now] = top;
if(bs[now])dfs2(bs[now],top);
for(auto nxt:tree[now]){
if(nxt == par[now]||nxt == bs[now])continue;
dfs2(nxt,nxt);
}
return;
}
pll LCA(int a,int b){
pll re = pll(0,0);
int ta = link_top[a],tb = link_top[b];
while(ta != tb){
if(dep[ta]<dep[tb]){
swap(a,b);
swap(ta,tb);
}
re.sc += getval(idx[ta],idx[a]);
a = par[ta];ta = link_top[a];
}
if(idx[a]>idx[b])swap(a,b);
re.sc += getval(idx[a],idx[b]);
re.fs = a;
return re;
}
}
namespace CALC{
ll ans = 0;
ll sum[mxn],dp[mxn];
void dfs(int now){
for(auto nxt:tree[now]){
if(nxt == HLD::par[now])continue;
dfs(nxt);
sum[now] += dp[nxt];
}
dp[now] = max(dp[now],sum[now]);
for(auto &i:req[now]){
auto [a,b] = i.fs;
int w = i.sc;
ll ts = sum[now]+HLD::LCA(a,b).sc+w;
dp[now] = max(dp[now],ts);
}
HLD::modify(HLD::idx[now],sum[now]-dp[now]);
}
void GO(){
dfs(rt);
printf("%d\n",dp[1]);
return;
}
}
int main(){
N = nextint();
for(int i = 1;i<N;i++){
int a,b;
a = nextint(),b = nextint();
tree[a].push_back(b);
tree[b].push_back(a);
}
rt = (N+1)>>1;
HLD::dfs1(rt);
HLD::dfs2(rt,rt);
Q = nextint();
while(Q--){
int a,b,c;
a = nextint();b = nextint();c = nextint();
int h = HLD::LCA(a,b).fs;
req[h].push_back(make_pair(pii(a,b),c));
}
CALC::GO();
}
Compilation message
election_campaign.cpp: In function 'void CALC::GO()':
election_campaign.cpp:109:12: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
109 | printf("%d\n",dp[1]);
| ~^ ~~~~~
| | |
| int long long int
| %lld
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
115 ms |
15580 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Incorrect |
2 ms |
8540 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |