#include <bits/stdc++.h>
#define fi first
#define se second
#define pp pop_back
#define ll long long
#define pb push_back
#define ld long double
#define debug cout << "OK\n";
#define all(x) x.begin(), x.end()
#define mp make_pair
using namespace std;
const ll N = 3e5 + 200;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll pe = mod2;
const ll pe2 = 570210983;
const ld eps = 1e-6;
/*
Rucode: jaqVYNrpMj
JUDGE_ID: 295965SY
dl:160532
*/
void data() {
#ifdef NURS
freopen("main.in", "r", stdin);
freopen("main.out", "w", stdout);
#endif
}
int n, m, timer;
int tin[N], tout[N], up[25][N];
ll dp[N], dp2[N], l[N], r[N], w[N];
vector<int> g[N], g2[N];
struct fenw
{
ll f[N * 2];
void upd(int pos, ll val)
{
for (int i = pos; i <= N; i = (i | (i + 1)))
f[i] += val;
}
ll sum(int pos)
{
ll res = 0;
for (int i = pos; i >= 1; i = (i & (i + 1)) - 1)
res += f[i];
return res;
}
ll get(int l, int r)
{
if (l > r)
return 0;
return sum(r) - sum(l - 1);
}
} tr, tr2;
void dfs(int v, int p)
{
tin[v] = ++timer;
up[0][v] = max(p, 1);
for (int i = 1; i <= 18; i++)
{
up[i][v] = up[i - 1][up[i - 1][v]];
}
for (int i = 0; i < g[v].size(); i++)
{
int to = g[v][i];
if (to == p)
continue;
dfs(to, v);
}
tout[v] = ++timer;
}
bool upper(int a, int b)
{
return (tin[a] <= tin[b] && tout[b] <= tout[a]);
}
int lca(int a, int b)
{
if (upper(a, b))
return a;
if (upper(b, a))
return b;
for (int i = 18; i >= 0; i--)
{
if (!upper(up[i][a], b))
a = up[i][a];
}
return up[0][a];
}
void dfs2(int v, int p)
{
for (int i = 0; i < g[v].size(); i++)
{
int to = g[v][i];
if (to == p)
continue;
dfs2(to, v);
dp[v] += dp2[to];
}
dp2[v] = dp[v];
for (int i = 0; i < g2[v].size(); i++)
{
int pos = g2[v][i];
int a = l[pos], b = r[pos];
dp2[v] = max(dp2[v] , dp[v] + tr.get(tin[v], tin[a]) + tr.get(tin[v], tin[b])
- tr2.get(tin[v], tin[a]) - tr2.get(tin[v], tin[b]) + w[pos]);
}
tr.upd(tin[v], dp[v]);
tr.upd(tout[v], -dp[v]);
tr2.upd(tin[v], dp2[v]);
tr2.upd(tout[v], -dp2[v]);
}
main()
{
data();
ios_base::sync_with_stdio(0),
cin.tie(0),cout.tie(0);
cin >> n;
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
g[u].pb(v), g[v].pb(u);
}
dfs(1, 0);
cin >> m;
for (int i = 1; i <= m; i++)
{
cin >> l[i] >> r[i] >> w[i];
int lc = lca(l[i], r[i]);
g2[lc].pb(i);
}
dfs2(1, 0);
cout << dp2[1];
}
Compilation message
election_campaign.cpp: In function 'void dfs(int, int)':
election_campaign.cpp:68:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (int i = 0; i < g[v].size(); i++)
| ~~^~~~~~~~~~~~~
election_campaign.cpp: In function 'void dfs2(int, int)':
election_campaign.cpp:96:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for (int i = 0; i < g[v].size(); i++)
| ~~^~~~~~~~~~~~~
election_campaign.cpp:105:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for (int i = 0; i < g2[v].size(); i++)
| ~~^~~~~~~~~~~~~~
election_campaign.cpp: At global scope:
election_campaign.cpp:117:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
117 | main()
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14700 KB |
Output is correct |
2 |
Correct |
10 ms |
14700 KB |
Output is correct |
3 |
Correct |
9 ms |
14700 KB |
Output is correct |
4 |
Correct |
12 ms |
14828 KB |
Output is correct |
5 |
Correct |
151 ms |
30828 KB |
Output is correct |
6 |
Correct |
65 ms |
40044 KB |
Output is correct |
7 |
Correct |
180 ms |
36800 KB |
Output is correct |
8 |
Correct |
110 ms |
30572 KB |
Output is correct |
9 |
Correct |
150 ms |
35052 KB |
Output is correct |
10 |
Correct |
101 ms |
30444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14700 KB |
Output is correct |
2 |
Correct |
10 ms |
14828 KB |
Output is correct |
3 |
Correct |
10 ms |
14956 KB |
Output is correct |
4 |
Correct |
124 ms |
43428 KB |
Output is correct |
5 |
Correct |
121 ms |
43628 KB |
Output is correct |
6 |
Correct |
127 ms |
43568 KB |
Output is correct |
7 |
Correct |
131 ms |
43500 KB |
Output is correct |
8 |
Correct |
126 ms |
43500 KB |
Output is correct |
9 |
Correct |
122 ms |
43500 KB |
Output is correct |
10 |
Correct |
134 ms |
43500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14700 KB |
Output is correct |
2 |
Correct |
10 ms |
14828 KB |
Output is correct |
3 |
Correct |
10 ms |
14956 KB |
Output is correct |
4 |
Correct |
124 ms |
43428 KB |
Output is correct |
5 |
Correct |
121 ms |
43628 KB |
Output is correct |
6 |
Correct |
127 ms |
43568 KB |
Output is correct |
7 |
Correct |
131 ms |
43500 KB |
Output is correct |
8 |
Correct |
126 ms |
43500 KB |
Output is correct |
9 |
Correct |
122 ms |
43500 KB |
Output is correct |
10 |
Correct |
134 ms |
43500 KB |
Output is correct |
11 |
Correct |
19 ms |
15724 KB |
Output is correct |
12 |
Correct |
136 ms |
43572 KB |
Output is correct |
13 |
Correct |
130 ms |
43576 KB |
Output is correct |
14 |
Correct |
127 ms |
43532 KB |
Output is correct |
15 |
Correct |
146 ms |
43484 KB |
Output is correct |
16 |
Correct |
140 ms |
43600 KB |
Output is correct |
17 |
Correct |
129 ms |
43500 KB |
Output is correct |
18 |
Correct |
132 ms |
43500 KB |
Output is correct |
19 |
Correct |
124 ms |
43572 KB |
Output is correct |
20 |
Correct |
134 ms |
43548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
266 ms |
33768 KB |
Output is correct |
2 |
Correct |
134 ms |
43496 KB |
Output is correct |
3 |
Correct |
310 ms |
39924 KB |
Output is correct |
4 |
Correct |
206 ms |
33256 KB |
Output is correct |
5 |
Correct |
266 ms |
39404 KB |
Output is correct |
6 |
Correct |
219 ms |
33384 KB |
Output is correct |
7 |
Correct |
315 ms |
39020 KB |
Output is correct |
8 |
Correct |
259 ms |
34156 KB |
Output is correct |
9 |
Correct |
125 ms |
43500 KB |
Output is correct |
10 |
Correct |
321 ms |
38084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14700 KB |
Output is correct |
2 |
Correct |
10 ms |
14700 KB |
Output is correct |
3 |
Correct |
9 ms |
14700 KB |
Output is correct |
4 |
Correct |
12 ms |
14828 KB |
Output is correct |
5 |
Correct |
151 ms |
30828 KB |
Output is correct |
6 |
Correct |
65 ms |
40044 KB |
Output is correct |
7 |
Correct |
180 ms |
36800 KB |
Output is correct |
8 |
Correct |
110 ms |
30572 KB |
Output is correct |
9 |
Correct |
150 ms |
35052 KB |
Output is correct |
10 |
Correct |
101 ms |
30444 KB |
Output is correct |
11 |
Correct |
11 ms |
14828 KB |
Output is correct |
12 |
Correct |
11 ms |
14972 KB |
Output is correct |
13 |
Correct |
21 ms |
14908 KB |
Output is correct |
14 |
Correct |
12 ms |
14828 KB |
Output is correct |
15 |
Correct |
11 ms |
14828 KB |
Output is correct |
16 |
Correct |
13 ms |
14956 KB |
Output is correct |
17 |
Correct |
12 ms |
14828 KB |
Output is correct |
18 |
Correct |
14 ms |
14956 KB |
Output is correct |
19 |
Correct |
10 ms |
14828 KB |
Output is correct |
20 |
Correct |
11 ms |
15104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14700 KB |
Output is correct |
2 |
Correct |
10 ms |
14700 KB |
Output is correct |
3 |
Correct |
9 ms |
14700 KB |
Output is correct |
4 |
Correct |
12 ms |
14828 KB |
Output is correct |
5 |
Correct |
151 ms |
30828 KB |
Output is correct |
6 |
Correct |
65 ms |
40044 KB |
Output is correct |
7 |
Correct |
180 ms |
36800 KB |
Output is correct |
8 |
Correct |
110 ms |
30572 KB |
Output is correct |
9 |
Correct |
150 ms |
35052 KB |
Output is correct |
10 |
Correct |
101 ms |
30444 KB |
Output is correct |
11 |
Correct |
9 ms |
14700 KB |
Output is correct |
12 |
Correct |
10 ms |
14828 KB |
Output is correct |
13 |
Correct |
10 ms |
14956 KB |
Output is correct |
14 |
Correct |
124 ms |
43428 KB |
Output is correct |
15 |
Correct |
121 ms |
43628 KB |
Output is correct |
16 |
Correct |
127 ms |
43568 KB |
Output is correct |
17 |
Correct |
131 ms |
43500 KB |
Output is correct |
18 |
Correct |
126 ms |
43500 KB |
Output is correct |
19 |
Correct |
122 ms |
43500 KB |
Output is correct |
20 |
Correct |
134 ms |
43500 KB |
Output is correct |
21 |
Correct |
19 ms |
15724 KB |
Output is correct |
22 |
Correct |
136 ms |
43572 KB |
Output is correct |
23 |
Correct |
130 ms |
43576 KB |
Output is correct |
24 |
Correct |
127 ms |
43532 KB |
Output is correct |
25 |
Correct |
146 ms |
43484 KB |
Output is correct |
26 |
Correct |
140 ms |
43600 KB |
Output is correct |
27 |
Correct |
129 ms |
43500 KB |
Output is correct |
28 |
Correct |
132 ms |
43500 KB |
Output is correct |
29 |
Correct |
124 ms |
43572 KB |
Output is correct |
30 |
Correct |
134 ms |
43548 KB |
Output is correct |
31 |
Correct |
266 ms |
33768 KB |
Output is correct |
32 |
Correct |
134 ms |
43496 KB |
Output is correct |
33 |
Correct |
310 ms |
39924 KB |
Output is correct |
34 |
Correct |
206 ms |
33256 KB |
Output is correct |
35 |
Correct |
266 ms |
39404 KB |
Output is correct |
36 |
Correct |
219 ms |
33384 KB |
Output is correct |
37 |
Correct |
315 ms |
39020 KB |
Output is correct |
38 |
Correct |
259 ms |
34156 KB |
Output is correct |
39 |
Correct |
125 ms |
43500 KB |
Output is correct |
40 |
Correct |
321 ms |
38084 KB |
Output is correct |
41 |
Correct |
11 ms |
14828 KB |
Output is correct |
42 |
Correct |
11 ms |
14972 KB |
Output is correct |
43 |
Correct |
21 ms |
14908 KB |
Output is correct |
44 |
Correct |
12 ms |
14828 KB |
Output is correct |
45 |
Correct |
11 ms |
14828 KB |
Output is correct |
46 |
Correct |
13 ms |
14956 KB |
Output is correct |
47 |
Correct |
12 ms |
14828 KB |
Output is correct |
48 |
Correct |
14 ms |
14956 KB |
Output is correct |
49 |
Correct |
10 ms |
14828 KB |
Output is correct |
50 |
Correct |
11 ms |
15104 KB |
Output is correct |
51 |
Correct |
270 ms |
34096 KB |
Output is correct |
52 |
Correct |
143 ms |
43540 KB |
Output is correct |
53 |
Correct |
344 ms |
38140 KB |
Output is correct |
54 |
Correct |
223 ms |
33340 KB |
Output is correct |
55 |
Correct |
290 ms |
33708 KB |
Output is correct |
56 |
Correct |
134 ms |
43472 KB |
Output is correct |
57 |
Correct |
289 ms |
38896 KB |
Output is correct |
58 |
Correct |
254 ms |
33388 KB |
Output is correct |
59 |
Correct |
251 ms |
34128 KB |
Output is correct |
60 |
Correct |
130 ms |
43500 KB |
Output is correct |
61 |
Correct |
281 ms |
39020 KB |
Output is correct |
62 |
Correct |
251 ms |
33324 KB |
Output is correct |
63 |
Correct |
256 ms |
33792 KB |
Output is correct |
64 |
Correct |
133 ms |
43500 KB |
Output is correct |
65 |
Correct |
393 ms |
38740 KB |
Output is correct |
66 |
Correct |
219 ms |
33388 KB |
Output is correct |
67 |
Correct |
260 ms |
33928 KB |
Output is correct |
68 |
Correct |
135 ms |
43500 KB |
Output is correct |
69 |
Correct |
276 ms |
37528 KB |
Output is correct |
70 |
Correct |
242 ms |
33388 KB |
Output is correct |