#include <bits/stdc++.h>
using namespace std;
#define all(s) s.begin(), s.end()
#define ll long long
#define fr first
#define sc second
#define pb push_back
#define mk make_pair
const int N = 1e5 + 7;
const int INF = 1e9 + 7;
ll dp[N][2];
int tin[N], tout[N], t;
int x[N], y[N], z[N];
int p[N][20];
int n, m;
int gr[N], gr_p[N];
ll gr_s[N], s[N];
vector < int > g[N], q[N];
void prec (int v, int pr = 0){
tin[v] = ++t;
p[v][0] = pr;
for (int j = 1; j < 20; j++)
p[v][j] = p[p[v][j - 1]][j - 1];
for (int to : g[v])
if (to != pr)
prec(to, v);
tout[v] = t;
}
bool check (int v, int u){
return tin[v] <= tin[u] && tout[u] <= tout[v];
}
int lca (int v, int u){
if (check(v, u))
return v;
if (check(u, v))
return u;
for (int j = 19; j >= 0; j--)
if (p[v][j] && !check(p[v][j], u))
v = p[v][j];
return p[v][0];
}
int cnt = 0;
ll get (int v, int pr){
ll res = 0;
while (v != pr){
res += gr_s[gr[v]] - s[v];
v = gr_p[gr[v]];
}
return res;
}
void dfs (int v){
for (int to : g[v]){
if (to != p[v][0]){
dfs(to);
dp[v][1] += dp[to][0];
gr[v] = gr[to];
}
}
dp[v][0] = dp[v][1];
for (int it : q[v]){
dp[v][0] = max(dp[v][0], get(x[it], v) + get(y[it], v) + dp[v][1] + z[it]);
}
dp[v][1] -= dp[v][0];
if (!gr[v] || g[v].size() > 2 || v == 1)
gr[v] = ++cnt;
s[v] = gr_s[gr[v]];
gr_s[gr[v]] += dp[v][1];
gr_p[gr[v]] = p[v][0];
}
main (){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i < n; i++){
int a, b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
prec(1);
cin >> m;
for (int i = 1; i <= m; i++){
cin >> x[i] >> y[i] >> z[i];
int l = lca(x[i], y[i]);
q[l].pb(i);
}
dfs(1);
cout << dp[1][0];
}
Compilation message
election_campaign.cpp:83:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
83 | main (){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5196 KB |
Output is correct |
5 |
Correct |
96 ms |
21652 KB |
Output is correct |
6 |
Correct |
59 ms |
28472 KB |
Output is correct |
7 |
Correct |
139 ms |
26308 KB |
Output is correct |
8 |
Correct |
92 ms |
22312 KB |
Output is correct |
9 |
Correct |
108 ms |
24784 KB |
Output is correct |
10 |
Correct |
87 ms |
22264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5196 KB |
Output is correct |
4 |
Correct |
101 ms |
32004 KB |
Output is correct |
5 |
Correct |
116 ms |
32016 KB |
Output is correct |
6 |
Correct |
98 ms |
32064 KB |
Output is correct |
7 |
Correct |
100 ms |
31956 KB |
Output is correct |
8 |
Correct |
101 ms |
32020 KB |
Output is correct |
9 |
Correct |
100 ms |
32048 KB |
Output is correct |
10 |
Correct |
113 ms |
32000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5196 KB |
Output is correct |
4 |
Correct |
101 ms |
32004 KB |
Output is correct |
5 |
Correct |
116 ms |
32016 KB |
Output is correct |
6 |
Correct |
98 ms |
32064 KB |
Output is correct |
7 |
Correct |
100 ms |
31956 KB |
Output is correct |
8 |
Correct |
101 ms |
32020 KB |
Output is correct |
9 |
Correct |
100 ms |
32048 KB |
Output is correct |
10 |
Correct |
113 ms |
32000 KB |
Output is correct |
11 |
Correct |
14 ms |
5964 KB |
Output is correct |
12 |
Correct |
110 ms |
32296 KB |
Output is correct |
13 |
Correct |
109 ms |
32288 KB |
Output is correct |
14 |
Correct |
105 ms |
32228 KB |
Output is correct |
15 |
Correct |
116 ms |
32272 KB |
Output is correct |
16 |
Correct |
120 ms |
32324 KB |
Output is correct |
17 |
Correct |
103 ms |
32200 KB |
Output is correct |
18 |
Correct |
132 ms |
32276 KB |
Output is correct |
19 |
Correct |
121 ms |
32324 KB |
Output is correct |
20 |
Correct |
104 ms |
32200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
180 ms |
24672 KB |
Output is correct |
2 |
Correct |
100 ms |
32052 KB |
Output is correct |
3 |
Execution timed out |
1101 ms |
29252 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5196 KB |
Output is correct |
5 |
Correct |
96 ms |
21652 KB |
Output is correct |
6 |
Correct |
59 ms |
28472 KB |
Output is correct |
7 |
Correct |
139 ms |
26308 KB |
Output is correct |
8 |
Correct |
92 ms |
22312 KB |
Output is correct |
9 |
Correct |
108 ms |
24784 KB |
Output is correct |
10 |
Correct |
87 ms |
22264 KB |
Output is correct |
11 |
Correct |
5 ms |
5196 KB |
Output is correct |
12 |
Correct |
4 ms |
5288 KB |
Output is correct |
13 |
Correct |
4 ms |
5196 KB |
Output is correct |
14 |
Correct |
5 ms |
5160 KB |
Output is correct |
15 |
Correct |
5 ms |
5156 KB |
Output is correct |
16 |
Correct |
5 ms |
5196 KB |
Output is correct |
17 |
Correct |
4 ms |
5196 KB |
Output is correct |
18 |
Correct |
5 ms |
5196 KB |
Output is correct |
19 |
Correct |
4 ms |
5196 KB |
Output is correct |
20 |
Correct |
4 ms |
5324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
5196 KB |
Output is correct |
5 |
Correct |
96 ms |
21652 KB |
Output is correct |
6 |
Correct |
59 ms |
28472 KB |
Output is correct |
7 |
Correct |
139 ms |
26308 KB |
Output is correct |
8 |
Correct |
92 ms |
22312 KB |
Output is correct |
9 |
Correct |
108 ms |
24784 KB |
Output is correct |
10 |
Correct |
87 ms |
22264 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
5068 KB |
Output is correct |
13 |
Correct |
4 ms |
5196 KB |
Output is correct |
14 |
Correct |
101 ms |
32004 KB |
Output is correct |
15 |
Correct |
116 ms |
32016 KB |
Output is correct |
16 |
Correct |
98 ms |
32064 KB |
Output is correct |
17 |
Correct |
100 ms |
31956 KB |
Output is correct |
18 |
Correct |
101 ms |
32020 KB |
Output is correct |
19 |
Correct |
100 ms |
32048 KB |
Output is correct |
20 |
Correct |
113 ms |
32000 KB |
Output is correct |
21 |
Correct |
14 ms |
5964 KB |
Output is correct |
22 |
Correct |
110 ms |
32296 KB |
Output is correct |
23 |
Correct |
109 ms |
32288 KB |
Output is correct |
24 |
Correct |
105 ms |
32228 KB |
Output is correct |
25 |
Correct |
116 ms |
32272 KB |
Output is correct |
26 |
Correct |
120 ms |
32324 KB |
Output is correct |
27 |
Correct |
103 ms |
32200 KB |
Output is correct |
28 |
Correct |
132 ms |
32276 KB |
Output is correct |
29 |
Correct |
121 ms |
32324 KB |
Output is correct |
30 |
Correct |
104 ms |
32200 KB |
Output is correct |
31 |
Correct |
180 ms |
24672 KB |
Output is correct |
32 |
Correct |
100 ms |
32052 KB |
Output is correct |
33 |
Execution timed out |
1101 ms |
29252 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |