#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
int N, Q;
vector<int> adj[MAXN+10];
int par[MAXN+10][25];
int dep[MAXN+10];
int L[MAXN+10], R[MAXN+10], cnt;
struct Query
{
int u, v, w;
};
vector<Query> A[MAXN+10];
void dfs(int now, int bef, int d)
{
dep[now]=d;
par[now][0]=bef;
L[now]=++cnt;
for(int nxt : adj[now])
{
if(nxt==bef) continue;
dfs(nxt, now, d+1);
}
R[now]=cnt;
}
int lca(int x, int y)
{
int i, j;
if(dep[x]<dep[y]) swap(x, y);
for(i=20; i>=0; i--) if(dep[par[x][i]]>=dep[y]) x=par[x][i];
if(x==y) return x;
for(i=20; i>=0; i--) if(par[x][i]!=par[y][i]) x=par[x][i], y=par[y][i];
return par[x][0];
}
struct SEG
{
int tree[MAXN*4+10];
void update(int node, int tl, int tr, int l, int r, int k)
{
if(r<tl || tr<l) return;
if(l<=tl && tr<=r) { tree[node]+=k; return; }
int mid=tl+tr>>1;
update(node*2, tl, mid, l, r, k);
update(node*2+1, mid+1, tr, l, r, k);
}
int query(int node, int tl, int tr, int p)
{
if(tl==tr) return tree[node];
int mid=tl+tr>>1;
if(p<=mid) return tree[node]+query(node*2, tl, mid, p);
else return tree[node]+query(node*2+1, mid+1, tr, p);
}
} seg;
int dp[MAXN+10];
void dfs2(int now, int bef)
{
int i, j;
vector<int> chd;
int sum=0;
for(int nxt : adj[now])
{
if(nxt==bef) continue;
dfs2(nxt, now);
chd.push_back(nxt);
sum+=dp[nxt];
}
dp[now]=sum;
for(auto nxt : chd) seg.update(1, 1, N, L[nxt], R[nxt], -dp[nxt]);
for(auto &it : A[now])
{
int v=seg.query(1, 1, N, L[it.u])+seg.query(1, 1, N, L[it.v])+sum+it.w;
dp[now]=max(dp[now], v);
}
seg.update(1, 1, N, L[now], R[now], sum);
}
int main()
{
int i, j;
scanf("%d", &N);
for(i=1; i<N; i++)
{
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(1, 1, 1);
for(i=1; i<=20; i++) for(j=1; j<=N; j++) par[j][i]=par[par[j][i-1]][i-1];
scanf("%d", &Q);
while(Q--)
{
int u, v, w, k;
scanf("%d%d%d", &u, &v, &k);
w=lca(u, v);
A[w].push_back({u, v, k});
}
dfs2(1, 1);
printf("%d\n", dp[1]);
}
Compilation message
election_campaign.cpp: In function 'int lca(int, int)':
election_campaign.cpp:38:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
election_campaign.cpp: In member function 'void SEG::update(int, int, int, int, int, int)':
election_campaign.cpp:53:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
election_campaign.cpp: In member function 'int SEG::query(int, int, int, int)':
election_campaign.cpp:60:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
election_campaign.cpp: In function 'void dfs2(int, int)':
election_campaign.cpp:69:6: warning: unused variable 'i' [-Wunused-variable]
int i, j;
^
election_campaign.cpp:69:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:97:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
election_campaign.cpp:101:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~
election_campaign.cpp:109:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &Q);
~~~~~^~~~~~~~~~
election_campaign.cpp:113:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &u, &v, &k);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
4992 KB |
Output is correct |
2 |
Correct |
7 ms |
4992 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5248 KB |
Output is correct |
5 |
Correct |
133 ms |
21780 KB |
Output is correct |
6 |
Correct |
107 ms |
37360 KB |
Output is correct |
7 |
Correct |
134 ms |
31992 KB |
Output is correct |
8 |
Correct |
114 ms |
22136 KB |
Output is correct |
9 |
Correct |
135 ms |
29176 KB |
Output is correct |
10 |
Correct |
112 ms |
22264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
7 ms |
4992 KB |
Output is correct |
3 |
Correct |
8 ms |
5376 KB |
Output is correct |
4 |
Correct |
205 ms |
40696 KB |
Output is correct |
5 |
Correct |
204 ms |
40696 KB |
Output is correct |
6 |
Correct |
188 ms |
40696 KB |
Output is correct |
7 |
Correct |
201 ms |
40696 KB |
Output is correct |
8 |
Correct |
206 ms |
40696 KB |
Output is correct |
9 |
Correct |
193 ms |
40824 KB |
Output is correct |
10 |
Correct |
204 ms |
40696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
7 ms |
4992 KB |
Output is correct |
3 |
Correct |
8 ms |
5376 KB |
Output is correct |
4 |
Correct |
205 ms |
40696 KB |
Output is correct |
5 |
Correct |
204 ms |
40696 KB |
Output is correct |
6 |
Correct |
188 ms |
40696 KB |
Output is correct |
7 |
Correct |
201 ms |
40696 KB |
Output is correct |
8 |
Correct |
206 ms |
40696 KB |
Output is correct |
9 |
Correct |
193 ms |
40824 KB |
Output is correct |
10 |
Correct |
204 ms |
40696 KB |
Output is correct |
11 |
Correct |
26 ms |
6144 KB |
Output is correct |
12 |
Correct |
209 ms |
40952 KB |
Output is correct |
13 |
Correct |
208 ms |
40952 KB |
Output is correct |
14 |
Correct |
196 ms |
41048 KB |
Output is correct |
15 |
Correct |
210 ms |
40952 KB |
Output is correct |
16 |
Correct |
196 ms |
40952 KB |
Output is correct |
17 |
Correct |
207 ms |
40956 KB |
Output is correct |
18 |
Correct |
206 ms |
40952 KB |
Output is correct |
19 |
Correct |
197 ms |
40944 KB |
Output is correct |
20 |
Correct |
214 ms |
40952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
237 ms |
24620 KB |
Output is correct |
2 |
Correct |
193 ms |
40696 KB |
Output is correct |
3 |
Correct |
303 ms |
35064 KB |
Output is correct |
4 |
Correct |
188 ms |
24880 KB |
Output is correct |
5 |
Correct |
271 ms |
34040 KB |
Output is correct |
6 |
Correct |
187 ms |
24808 KB |
Output is correct |
7 |
Correct |
309 ms |
33692 KB |
Output is correct |
8 |
Correct |
239 ms |
24952 KB |
Output is correct |
9 |
Correct |
192 ms |
40696 KB |
Output is correct |
10 |
Correct |
315 ms |
31536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
4992 KB |
Output is correct |
2 |
Correct |
7 ms |
4992 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5248 KB |
Output is correct |
5 |
Correct |
133 ms |
21780 KB |
Output is correct |
6 |
Correct |
107 ms |
37360 KB |
Output is correct |
7 |
Correct |
134 ms |
31992 KB |
Output is correct |
8 |
Correct |
114 ms |
22136 KB |
Output is correct |
9 |
Correct |
135 ms |
29176 KB |
Output is correct |
10 |
Correct |
112 ms |
22264 KB |
Output is correct |
11 |
Correct |
8 ms |
5248 KB |
Output is correct |
12 |
Correct |
8 ms |
5376 KB |
Output is correct |
13 |
Correct |
8 ms |
5376 KB |
Output is correct |
14 |
Correct |
8 ms |
5248 KB |
Output is correct |
15 |
Correct |
9 ms |
5248 KB |
Output is correct |
16 |
Correct |
8 ms |
5248 KB |
Output is correct |
17 |
Correct |
8 ms |
5248 KB |
Output is correct |
18 |
Correct |
8 ms |
5376 KB |
Output is correct |
19 |
Correct |
8 ms |
5248 KB |
Output is correct |
20 |
Correct |
8 ms |
5376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
4992 KB |
Output is correct |
2 |
Correct |
7 ms |
4992 KB |
Output is correct |
3 |
Correct |
7 ms |
5120 KB |
Output is correct |
4 |
Correct |
7 ms |
5248 KB |
Output is correct |
5 |
Correct |
133 ms |
21780 KB |
Output is correct |
6 |
Correct |
107 ms |
37360 KB |
Output is correct |
7 |
Correct |
134 ms |
31992 KB |
Output is correct |
8 |
Correct |
114 ms |
22136 KB |
Output is correct |
9 |
Correct |
135 ms |
29176 KB |
Output is correct |
10 |
Correct |
112 ms |
22264 KB |
Output is correct |
11 |
Correct |
7 ms |
4992 KB |
Output is correct |
12 |
Correct |
7 ms |
4992 KB |
Output is correct |
13 |
Correct |
8 ms |
5376 KB |
Output is correct |
14 |
Correct |
205 ms |
40696 KB |
Output is correct |
15 |
Correct |
204 ms |
40696 KB |
Output is correct |
16 |
Correct |
188 ms |
40696 KB |
Output is correct |
17 |
Correct |
201 ms |
40696 KB |
Output is correct |
18 |
Correct |
206 ms |
40696 KB |
Output is correct |
19 |
Correct |
193 ms |
40824 KB |
Output is correct |
20 |
Correct |
204 ms |
40696 KB |
Output is correct |
21 |
Correct |
26 ms |
6144 KB |
Output is correct |
22 |
Correct |
209 ms |
40952 KB |
Output is correct |
23 |
Correct |
208 ms |
40952 KB |
Output is correct |
24 |
Correct |
196 ms |
41048 KB |
Output is correct |
25 |
Correct |
210 ms |
40952 KB |
Output is correct |
26 |
Correct |
196 ms |
40952 KB |
Output is correct |
27 |
Correct |
207 ms |
40956 KB |
Output is correct |
28 |
Correct |
206 ms |
40952 KB |
Output is correct |
29 |
Correct |
197 ms |
40944 KB |
Output is correct |
30 |
Correct |
214 ms |
40952 KB |
Output is correct |
31 |
Correct |
237 ms |
24620 KB |
Output is correct |
32 |
Correct |
193 ms |
40696 KB |
Output is correct |
33 |
Correct |
303 ms |
35064 KB |
Output is correct |
34 |
Correct |
188 ms |
24880 KB |
Output is correct |
35 |
Correct |
271 ms |
34040 KB |
Output is correct |
36 |
Correct |
187 ms |
24808 KB |
Output is correct |
37 |
Correct |
309 ms |
33692 KB |
Output is correct |
38 |
Correct |
239 ms |
24952 KB |
Output is correct |
39 |
Correct |
192 ms |
40696 KB |
Output is correct |
40 |
Correct |
315 ms |
31536 KB |
Output is correct |
41 |
Correct |
8 ms |
5248 KB |
Output is correct |
42 |
Correct |
8 ms |
5376 KB |
Output is correct |
43 |
Correct |
8 ms |
5376 KB |
Output is correct |
44 |
Correct |
8 ms |
5248 KB |
Output is correct |
45 |
Correct |
9 ms |
5248 KB |
Output is correct |
46 |
Correct |
8 ms |
5248 KB |
Output is correct |
47 |
Correct |
8 ms |
5248 KB |
Output is correct |
48 |
Correct |
8 ms |
5376 KB |
Output is correct |
49 |
Correct |
8 ms |
5248 KB |
Output is correct |
50 |
Correct |
8 ms |
5376 KB |
Output is correct |
51 |
Correct |
249 ms |
25336 KB |
Output is correct |
52 |
Correct |
218 ms |
40956 KB |
Output is correct |
53 |
Correct |
325 ms |
32432 KB |
Output is correct |
54 |
Correct |
181 ms |
25136 KB |
Output is correct |
55 |
Correct |
236 ms |
25008 KB |
Output is correct |
56 |
Correct |
205 ms |
40952 KB |
Output is correct |
57 |
Correct |
277 ms |
33016 KB |
Output is correct |
58 |
Correct |
187 ms |
25136 KB |
Output is correct |
59 |
Correct |
243 ms |
25208 KB |
Output is correct |
60 |
Correct |
201 ms |
40952 KB |
Output is correct |
61 |
Correct |
284 ms |
33272 KB |
Output is correct |
62 |
Correct |
190 ms |
25040 KB |
Output is correct |
63 |
Correct |
238 ms |
24800 KB |
Output is correct |
64 |
Correct |
213 ms |
41040 KB |
Output is correct |
65 |
Correct |
303 ms |
33184 KB |
Output is correct |
66 |
Correct |
186 ms |
25268 KB |
Output is correct |
67 |
Correct |
242 ms |
25064 KB |
Output is correct |
68 |
Correct |
206 ms |
40952 KB |
Output is correct |
69 |
Correct |
282 ms |
30840 KB |
Output is correct |
70 |
Correct |
189 ms |
25308 KB |
Output is correct |