#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>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,popcnt,sse4")
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[rt]);
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:110:12: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
110 | printf("%d\n",dp[rt]);
| ~^ ~~~~~~
| | |
| int long long int
| %lld
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7512 KB |
Output is correct |
5 |
Correct |
34 ms |
14116 KB |
Output is correct |
6 |
Correct |
26 ms |
26460 KB |
Output is correct |
7 |
Correct |
39 ms |
26460 KB |
Output is correct |
8 |
Correct |
22 ms |
13656 KB |
Output is correct |
9 |
Correct |
51 ms |
26708 KB |
Output is correct |
10 |
Correct |
22 ms |
13660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7772 KB |
Output is correct |
4 |
Correct |
49 ms |
28400 KB |
Output is correct |
5 |
Correct |
55 ms |
28320 KB |
Output is correct |
6 |
Correct |
42 ms |
28500 KB |
Output is correct |
7 |
Correct |
48 ms |
28464 KB |
Output is correct |
8 |
Correct |
45 ms |
28496 KB |
Output is correct |
9 |
Correct |
42 ms |
28496 KB |
Output is correct |
10 |
Correct |
51 ms |
28500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7772 KB |
Output is correct |
4 |
Correct |
49 ms |
28400 KB |
Output is correct |
5 |
Correct |
55 ms |
28320 KB |
Output is correct |
6 |
Correct |
42 ms |
28500 KB |
Output is correct |
7 |
Correct |
48 ms |
28464 KB |
Output is correct |
8 |
Correct |
45 ms |
28496 KB |
Output is correct |
9 |
Correct |
42 ms |
28496 KB |
Output is correct |
10 |
Correct |
51 ms |
28500 KB |
Output is correct |
11 |
Correct |
4 ms |
8536 KB |
Output is correct |
12 |
Correct |
43 ms |
28508 KB |
Output is correct |
13 |
Correct |
48 ms |
28324 KB |
Output is correct |
14 |
Correct |
50 ms |
28496 KB |
Output is correct |
15 |
Correct |
48 ms |
28392 KB |
Output is correct |
16 |
Correct |
43 ms |
28496 KB |
Output is correct |
17 |
Correct |
47 ms |
28500 KB |
Output is correct |
18 |
Correct |
44 ms |
28312 KB |
Output is correct |
19 |
Correct |
43 ms |
28500 KB |
Output is correct |
20 |
Correct |
44 ms |
28284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
15624 KB |
Output is correct |
2 |
Correct |
44 ms |
28500 KB |
Output is correct |
3 |
Correct |
82 ms |
25080 KB |
Output is correct |
4 |
Correct |
59 ms |
15016 KB |
Output is correct |
5 |
Correct |
75 ms |
31648 KB |
Output is correct |
6 |
Correct |
53 ms |
15440 KB |
Output is correct |
7 |
Correct |
69 ms |
31492 KB |
Output is correct |
8 |
Correct |
74 ms |
15700 KB |
Output is correct |
9 |
Correct |
43 ms |
28496 KB |
Output is correct |
10 |
Correct |
72 ms |
31132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7512 KB |
Output is correct |
5 |
Correct |
34 ms |
14116 KB |
Output is correct |
6 |
Correct |
26 ms |
26460 KB |
Output is correct |
7 |
Correct |
39 ms |
26460 KB |
Output is correct |
8 |
Correct |
22 ms |
13656 KB |
Output is correct |
9 |
Correct |
51 ms |
26708 KB |
Output is correct |
10 |
Correct |
22 ms |
13660 KB |
Output is correct |
11 |
Correct |
2 ms |
7512 KB |
Output is correct |
12 |
Correct |
2 ms |
7772 KB |
Output is correct |
13 |
Correct |
2 ms |
7772 KB |
Output is correct |
14 |
Correct |
2 ms |
7516 KB |
Output is correct |
15 |
Correct |
2 ms |
7516 KB |
Output is correct |
16 |
Correct |
2 ms |
7772 KB |
Output is correct |
17 |
Correct |
3 ms |
7516 KB |
Output is correct |
18 |
Correct |
2 ms |
7772 KB |
Output is correct |
19 |
Correct |
3 ms |
7512 KB |
Output is correct |
20 |
Correct |
2 ms |
7772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7512 KB |
Output is correct |
2 |
Correct |
2 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
2 ms |
7512 KB |
Output is correct |
5 |
Correct |
34 ms |
14116 KB |
Output is correct |
6 |
Correct |
26 ms |
26460 KB |
Output is correct |
7 |
Correct |
39 ms |
26460 KB |
Output is correct |
8 |
Correct |
22 ms |
13656 KB |
Output is correct |
9 |
Correct |
51 ms |
26708 KB |
Output is correct |
10 |
Correct |
22 ms |
13660 KB |
Output is correct |
11 |
Correct |
2 ms |
7516 KB |
Output is correct |
12 |
Correct |
2 ms |
7516 KB |
Output is correct |
13 |
Correct |
2 ms |
7772 KB |
Output is correct |
14 |
Correct |
49 ms |
28400 KB |
Output is correct |
15 |
Correct |
55 ms |
28320 KB |
Output is correct |
16 |
Correct |
42 ms |
28500 KB |
Output is correct |
17 |
Correct |
48 ms |
28464 KB |
Output is correct |
18 |
Correct |
45 ms |
28496 KB |
Output is correct |
19 |
Correct |
42 ms |
28496 KB |
Output is correct |
20 |
Correct |
51 ms |
28500 KB |
Output is correct |
21 |
Correct |
4 ms |
8536 KB |
Output is correct |
22 |
Correct |
43 ms |
28508 KB |
Output is correct |
23 |
Correct |
48 ms |
28324 KB |
Output is correct |
24 |
Correct |
50 ms |
28496 KB |
Output is correct |
25 |
Correct |
48 ms |
28392 KB |
Output is correct |
26 |
Correct |
43 ms |
28496 KB |
Output is correct |
27 |
Correct |
47 ms |
28500 KB |
Output is correct |
28 |
Correct |
44 ms |
28312 KB |
Output is correct |
29 |
Correct |
43 ms |
28500 KB |
Output is correct |
30 |
Correct |
44 ms |
28284 KB |
Output is correct |
31 |
Correct |
107 ms |
15624 KB |
Output is correct |
32 |
Correct |
44 ms |
28500 KB |
Output is correct |
33 |
Correct |
82 ms |
25080 KB |
Output is correct |
34 |
Correct |
59 ms |
15016 KB |
Output is correct |
35 |
Correct |
75 ms |
31648 KB |
Output is correct |
36 |
Correct |
53 ms |
15440 KB |
Output is correct |
37 |
Correct |
69 ms |
31492 KB |
Output is correct |
38 |
Correct |
74 ms |
15700 KB |
Output is correct |
39 |
Correct |
43 ms |
28496 KB |
Output is correct |
40 |
Correct |
72 ms |
31132 KB |
Output is correct |
41 |
Correct |
2 ms |
7512 KB |
Output is correct |
42 |
Correct |
2 ms |
7772 KB |
Output is correct |
43 |
Correct |
2 ms |
7772 KB |
Output is correct |
44 |
Correct |
2 ms |
7516 KB |
Output is correct |
45 |
Correct |
2 ms |
7516 KB |
Output is correct |
46 |
Correct |
2 ms |
7772 KB |
Output is correct |
47 |
Correct |
3 ms |
7516 KB |
Output is correct |
48 |
Correct |
2 ms |
7772 KB |
Output is correct |
49 |
Correct |
3 ms |
7512 KB |
Output is correct |
50 |
Correct |
2 ms |
7772 KB |
Output is correct |
51 |
Correct |
72 ms |
15696 KB |
Output is correct |
52 |
Correct |
44 ms |
28496 KB |
Output is correct |
53 |
Correct |
81 ms |
26588 KB |
Output is correct |
54 |
Correct |
40 ms |
15060 KB |
Output is correct |
55 |
Correct |
111 ms |
15644 KB |
Output is correct |
56 |
Correct |
42 ms |
28396 KB |
Output is correct |
57 |
Correct |
81 ms |
25596 KB |
Output is correct |
58 |
Correct |
43 ms |
15112 KB |
Output is correct |
59 |
Correct |
69 ms |
15700 KB |
Output is correct |
60 |
Correct |
44 ms |
28508 KB |
Output is correct |
61 |
Correct |
71 ms |
29188 KB |
Output is correct |
62 |
Correct |
53 ms |
15356 KB |
Output is correct |
63 |
Correct |
104 ms |
15404 KB |
Output is correct |
64 |
Correct |
41 ms |
28500 KB |
Output is correct |
65 |
Correct |
68 ms |
24072 KB |
Output is correct |
66 |
Correct |
40 ms |
15332 KB |
Output is correct |
67 |
Correct |
118 ms |
15440 KB |
Output is correct |
68 |
Correct |
42 ms |
28244 KB |
Output is correct |
69 |
Correct |
66 ms |
28644 KB |
Output is correct |
70 |
Correct |
64 ms |
14884 KB |
Output is correct |