#include <bits/stdc++.h>
using namespace std;
int par[100005][20], sum[100005][20], depth[100005], dp1[100005], dp2[100005];
vector<pair<pair<int, int>, int> > vec[100005];
vector<pair<int, int> > upd[100005];
vector<int> adj[100005];
int lca(int u, int v)
{
if (depth[u]<depth[v])
swap(u, v);
int d=depth[u]-depth[v];
for (int i=19; i>=0; i--)
if (d&(1<<i))
u=par[u][i];
if (u==v)
return u;
for (int i=19; i>=0; i--)
{
if (par[u][i]!=par[v][i])
{
u=par[u][i];
v=par[v][i];
}
}
return par[u][0];
}
int calc(int u, int d)
{
int res=0;
for (int i=19; i>=0; i--)
{
if (d&(1<<i))
{
res+=sum[u][i];
u=par[u][i];
}
}
return res;
}
void dfs1(int u, int p)
{
par[u][0]=p;
for (int i=1; i<20; i++)
{
par[u][i]=par[par[u][i-1]][i-1];
if (depth[u]-depth[par[u][i]]==(1<<i))
upd[par[u][i]].push_back({u, i});
}
for (int v:adj[u])
{
if (v==p)
continue;
depth[v]=depth[u]+1;
dfs1(v, u);
}
}
void dfs2(int u)
{
for (int v:adj[u])
{
if (v==par[u][0])
continue;
dfs2(v);
dp2[u]+=dp1[v];
}
for (int i=0; i<upd[u].size(); i++)
{
int v=upd[u][i].first, x=upd[u][i].second;
sum[v][x]=sum[v][x-1]+sum[par[v][x-1]][x-1];
}
dp1[u]=dp2[u];
for (int i=0; i<vec[u].size(); i++)
{
int a=vec[u][i].first.first, b=vec[u][i].first.second, c=vec[u][i].second;
dp1[u]=max(dp1[u], dp2[u]+c-calc(a, depth[a]-depth[u])-calc(b, depth[b]-depth[u]));
}
sum[u][0]=dp1[u]-dp2[u];
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n;
for (int i=1; i<n; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs1(1, 1);
cin >> m;
for (int i=1; i<=m; i++)
{
int a, b, c;
cin >> a >> b >> c;
vec[lca(a, b)].push_back({{a, b}, c});
}
dfs2(1);
cout << dp1[1];
}
Compilation message
election_campaign.cpp: In function 'void dfs2(int)':
election_campaign.cpp:66:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for (int i=0; i<upd[u].size(); i++)
| ~^~~~~~~~~~~~~~
election_campaign.cpp:72:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i=0; i<vec[u].size(); i++)
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
1 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
46 ms |
32496 KB |
Output is correct |
6 |
Correct |
58 ms |
51800 KB |
Output is correct |
7 |
Correct |
108 ms |
53076 KB |
Output is correct |
8 |
Correct |
34 ms |
30792 KB |
Output is correct |
9 |
Correct |
97 ms |
50236 KB |
Output is correct |
10 |
Correct |
33 ms |
30460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10912 KB |
Output is correct |
3 |
Correct |
2 ms |
11100 KB |
Output is correct |
4 |
Correct |
115 ms |
55224 KB |
Output is correct |
5 |
Correct |
113 ms |
55120 KB |
Output is correct |
6 |
Correct |
102 ms |
55376 KB |
Output is correct |
7 |
Correct |
112 ms |
55216 KB |
Output is correct |
8 |
Correct |
112 ms |
55172 KB |
Output is correct |
9 |
Correct |
107 ms |
55272 KB |
Output is correct |
10 |
Correct |
114 ms |
55120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
10912 KB |
Output is correct |
3 |
Correct |
2 ms |
11100 KB |
Output is correct |
4 |
Correct |
115 ms |
55224 KB |
Output is correct |
5 |
Correct |
113 ms |
55120 KB |
Output is correct |
6 |
Correct |
102 ms |
55376 KB |
Output is correct |
7 |
Correct |
112 ms |
55216 KB |
Output is correct |
8 |
Correct |
112 ms |
55172 KB |
Output is correct |
9 |
Correct |
107 ms |
55272 KB |
Output is correct |
10 |
Correct |
114 ms |
55120 KB |
Output is correct |
11 |
Correct |
8 ms |
11864 KB |
Output is correct |
12 |
Correct |
117 ms |
55460 KB |
Output is correct |
13 |
Correct |
117 ms |
55440 KB |
Output is correct |
14 |
Correct |
101 ms |
55636 KB |
Output is correct |
15 |
Correct |
112 ms |
55620 KB |
Output is correct |
16 |
Correct |
104 ms |
55576 KB |
Output is correct |
17 |
Correct |
117 ms |
55636 KB |
Output is correct |
18 |
Correct |
118 ms |
55632 KB |
Output is correct |
19 |
Correct |
109 ms |
55632 KB |
Output is correct |
20 |
Correct |
118 ms |
55636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
35808 KB |
Output is correct |
2 |
Correct |
104 ms |
55372 KB |
Output is correct |
3 |
Correct |
163 ms |
54596 KB |
Output is correct |
4 |
Correct |
57 ms |
33256 KB |
Output is correct |
5 |
Correct |
133 ms |
53840 KB |
Output is correct |
6 |
Correct |
53 ms |
33480 KB |
Output is correct |
7 |
Correct |
151 ms |
53472 KB |
Output is correct |
8 |
Correct |
73 ms |
35924 KB |
Output is correct |
9 |
Correct |
102 ms |
55212 KB |
Output is correct |
10 |
Correct |
147 ms |
52572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
1 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
46 ms |
32496 KB |
Output is correct |
6 |
Correct |
58 ms |
51800 KB |
Output is correct |
7 |
Correct |
108 ms |
53076 KB |
Output is correct |
8 |
Correct |
34 ms |
30792 KB |
Output is correct |
9 |
Correct |
97 ms |
50236 KB |
Output is correct |
10 |
Correct |
33 ms |
30460 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
11164 KB |
Output is correct |
13 |
Correct |
2 ms |
11100 KB |
Output is correct |
14 |
Correct |
2 ms |
11096 KB |
Output is correct |
15 |
Correct |
3 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
3 ms |
11100 KB |
Output is correct |
19 |
Correct |
2 ms |
11000 KB |
Output is correct |
20 |
Correct |
2 ms |
11100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10840 KB |
Output is correct |
2 |
Correct |
1 ms |
10844 KB |
Output is correct |
3 |
Correct |
1 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
46 ms |
32496 KB |
Output is correct |
6 |
Correct |
58 ms |
51800 KB |
Output is correct |
7 |
Correct |
108 ms |
53076 KB |
Output is correct |
8 |
Correct |
34 ms |
30792 KB |
Output is correct |
9 |
Correct |
97 ms |
50236 KB |
Output is correct |
10 |
Correct |
33 ms |
30460 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Correct |
2 ms |
10912 KB |
Output is correct |
13 |
Correct |
2 ms |
11100 KB |
Output is correct |
14 |
Correct |
115 ms |
55224 KB |
Output is correct |
15 |
Correct |
113 ms |
55120 KB |
Output is correct |
16 |
Correct |
102 ms |
55376 KB |
Output is correct |
17 |
Correct |
112 ms |
55216 KB |
Output is correct |
18 |
Correct |
112 ms |
55172 KB |
Output is correct |
19 |
Correct |
107 ms |
55272 KB |
Output is correct |
20 |
Correct |
114 ms |
55120 KB |
Output is correct |
21 |
Correct |
8 ms |
11864 KB |
Output is correct |
22 |
Correct |
117 ms |
55460 KB |
Output is correct |
23 |
Correct |
117 ms |
55440 KB |
Output is correct |
24 |
Correct |
101 ms |
55636 KB |
Output is correct |
25 |
Correct |
112 ms |
55620 KB |
Output is correct |
26 |
Correct |
104 ms |
55576 KB |
Output is correct |
27 |
Correct |
117 ms |
55636 KB |
Output is correct |
28 |
Correct |
118 ms |
55632 KB |
Output is correct |
29 |
Correct |
109 ms |
55632 KB |
Output is correct |
30 |
Correct |
118 ms |
55636 KB |
Output is correct |
31 |
Correct |
76 ms |
35808 KB |
Output is correct |
32 |
Correct |
104 ms |
55372 KB |
Output is correct |
33 |
Correct |
163 ms |
54596 KB |
Output is correct |
34 |
Correct |
57 ms |
33256 KB |
Output is correct |
35 |
Correct |
133 ms |
53840 KB |
Output is correct |
36 |
Correct |
53 ms |
33480 KB |
Output is correct |
37 |
Correct |
151 ms |
53472 KB |
Output is correct |
38 |
Correct |
73 ms |
35924 KB |
Output is correct |
39 |
Correct |
102 ms |
55212 KB |
Output is correct |
40 |
Correct |
147 ms |
52572 KB |
Output is correct |
41 |
Correct |
2 ms |
10844 KB |
Output is correct |
42 |
Correct |
2 ms |
11164 KB |
Output is correct |
43 |
Correct |
2 ms |
11100 KB |
Output is correct |
44 |
Correct |
2 ms |
11096 KB |
Output is correct |
45 |
Correct |
3 ms |
10844 KB |
Output is correct |
46 |
Correct |
2 ms |
10844 KB |
Output is correct |
47 |
Correct |
2 ms |
10844 KB |
Output is correct |
48 |
Correct |
3 ms |
11100 KB |
Output is correct |
49 |
Correct |
2 ms |
11000 KB |
Output is correct |
50 |
Correct |
2 ms |
11100 KB |
Output is correct |
51 |
Correct |
78 ms |
36396 KB |
Output is correct |
52 |
Correct |
116 ms |
55428 KB |
Output is correct |
53 |
Correct |
162 ms |
52876 KB |
Output is correct |
54 |
Correct |
62 ms |
33396 KB |
Output is correct |
55 |
Correct |
77 ms |
35980 KB |
Output is correct |
56 |
Correct |
103 ms |
55648 KB |
Output is correct |
57 |
Correct |
153 ms |
53660 KB |
Output is correct |
58 |
Correct |
60 ms |
33468 KB |
Output is correct |
59 |
Correct |
76 ms |
36180 KB |
Output is correct |
60 |
Correct |
120 ms |
55636 KB |
Output is correct |
61 |
Correct |
133 ms |
53844 KB |
Output is correct |
62 |
Correct |
59 ms |
33528 KB |
Output is correct |
63 |
Correct |
76 ms |
36028 KB |
Output is correct |
64 |
Correct |
113 ms |
55636 KB |
Output is correct |
65 |
Correct |
155 ms |
53704 KB |
Output is correct |
66 |
Correct |
59 ms |
33736 KB |
Output is correct |
67 |
Correct |
73 ms |
35908 KB |
Output is correct |
68 |
Correct |
111 ms |
55632 KB |
Output is correct |
69 |
Correct |
120 ms |
52308 KB |
Output is correct |
70 |
Correct |
61 ms |
34328 KB |
Output is correct |