#include <bits/stdc++.h>
#include <vector>
#define fi first
#define se second
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
template<class F, class S> ostream& operator<< (ostream& os, pair<F, S> p){
os << '{' << p.fi << ", " << p.se << '}'; return os;
}
const int mxsz = 1e5 + 3;
int n, m;
vector<int> adj[mxsz];
int dep[mxsz], par[mxsz];
int in[mxsz], out[mxsz], flat[mxsz];
namespace LCA{
int spt[18][mxsz];
void dfs_lca(int u, int prv = -1){
static int tme = 0;
in[u] = ++tme; flat[tme] = u;
if (prv != -1) dep[u] = dep[prv] + 1;
par[u] = spt[0][u] = prv;
for (int nx : adj[u]){
if (nx != prv) dfs_lca(nx, u);
}
out[u] = tme;
}
void computeST(){
memset(spt, -1, sizeof(spt));
dfs_lca(1);
for (int i = 1; i <= 17; i++)
for (int j = 1; j <= n; j++)
if (spt[i-1][j] != -1) spt[i][j] = spt[i-1][spt[i-1][j]];
}
int lca(int u, int v){
if (dep[u] < dep[v]) swap(u, v);
for (int i = 17; i >= 0; --i)
if (spt[i][u] != -1 && dep[spt[i][u]] >= dep[v]) u = spt[i][u];
if (u == v) return u;
for (int i = 17; i >= 0; --i)
if (spt[i][u] != spt[i][v]) u = spt[i][u], v = spt[i][v];
return spt[0][u];
}
}
struct path{
int l, u, v, c;
path(){}
path(int l, int u, int v, int c): l(l), u(u), v(v), c(c) {}
path(int u, int v, int c): u(u), v(v), l(LCA::lca(u, v)), c(c) {};
};
#define md ((l + r) >> 1)
struct Seg{ // :^)
int tree[mxsz << 2], lz[mxsz << 2];
void prop(int p, int l, int r){
if (lz[p]){
tree[p] += lz[p];
if (l < r){ lz[p<<1] += lz[p]; lz[p<<1|1] += lz[p]; }
lz[p] = 0;
}
}
void upd(int i, int j, int v, int p = 1, int l = 1, int r = n){
prop(p, l, r);
if (j < l || i > r) return;
if (i <= l && j >= r){lz[p] += v; prop(p,l,r); return;}
upd(i, j, v, p<<1, l, md);
upd(i, j, v, p<<1|1, md+1, r);
tree[p] = tree[p<<1] + tree[p<<1|1];
}
int query(int i, int p = 1, int l = 1,int r = n){
prop(p, l, r);
if (l == r) return tree[p];
if (i <= md) return query(i, p<<1, l, md);
return query(i, p<<1|1, md+1, r);
}
} segtree; // stores the best value if we do not take any nodes from i to current
struct maxseg{
int tree[mxsz << 2], lz[mxsz << 2];
void prop(int p, int l, int r){
if (lz[p]){
if (l < r) lz[p<<1] = lz[p<<1|1] = lz[p];
tree[p] = lz[p];
lz[p] = 0;
}
}
void upd(int i, int j, int v, int p = 1, int l = 1, int r = n){
prop(p, l, r);
if (j < l || i > r) return;
if (i <= l && j >= r){lz[p] = v; prop(p,l,r); return;}
upd(i, j, v, p<<1, l, md);
upd(i, j, v, p<<1|1, md+1, r);
tree[p] = max(tree[p<<1], tree[p<<1|1]);
}
int get(int i, int p = 1, int l = 1,int r = n){
prop(p, l, r);
if (l == r) return tree[p];
if (i <= md) return get(i, p<<1, l, md);
return get(i, p<<1|1, md+1, r);
}
} curp;
#undef md
vector<path> pths[mxsz];
int dp[mxsz];
void dfs(int u){
dp[u] = 0;
for (int nx : adj[u]){
dfs(nx);
curp.upd(in[nx], out[nx], nx);
}
int tot = 0;
for (int nx : adj[u]) tot += dp[nx];
int mx = 0;
for (path p : pths[u]){
int nx = p.u, ny = p.v;
if (nx == u) swap(nx, ny);
mx = max(mx, segtree.query(in[nx]) + segtree.query(in[ny]) + p.c
+ tot - dp[curp.get(in[nx])] - dp[curp.get(in[ny])]);
}
segtree.upd(in[u], in[u], tot);
for (int nx : adj[u]){
segtree.upd(in[nx], out[nx], tot - dp[nx]);
}
dp[u] = max(mx, tot);
}
bool cmpByIn(int u, int v){
return in[u] < in[v];
}
int getMaximumVotes(int N, int M, vi U, vi V, vi A, vi B, vi C) {
n = N, m = M;
for (int i = 0; i < n-1; i++){
adj[U[i]].push_back(V[i]);
adj[V[i]].push_back(U[i]);
}
LCA::computeST();
for (int i = 0; i < m; i++){
path curr = path(A[i], B[i], C[i]);
pths[curr.l].push_back(curr);
}
for (int i = 1; i <= n; i++){
curp.upd(in[i], in[i], i);
for (int j = 0, t = adj[i].size(); j < t; j++){
// make tree level graph, remove parent
if (dep[adj[i][j]] < dep[i]){
swap(adj[i][j], adj[i][t-1]);
adj[i].pop_back(); break;
}
}
sort(adj[i].begin(), adj[i].end(), cmpByIn);
}
dfs(1);
int mx = 0;
for (int i = 1; i <= n; i++) mx = max(mx, dp[i]);
return mx;
}
int main() {
int N, M;
std::vector<int> U, V;
std::vector<int> A, B, C;
scanf("%d", &N);
U.resize(N-1); V.resize(N-1);
for (int i = 0 ; i < N-1 ; i++) {
scanf("%d %d", &U[i], &V[i]);
}
scanf("%d", &M);
A.resize(M); B.resize(M); C.resize(M);
for (int i = 0 ; i < M ; i++) {
scanf("%d %d %d", &A[i], &B[i], &C[i]);
}
int ans = getMaximumVotes(N, M, U, V, A, B, C);
printf("%d\n", ans);
return 0;
}
Compilation message
election_campaign.cpp: In constructor 'path::path(int, int, int)':
election_campaign.cpp:50:12: warning: 'path::v' will be initialized after [-Wreorder]
int l, u, v, c;
^
election_campaign.cpp:50:6: warning: 'int path::l' [-Wreorder]
int l, u, v, c;
^
election_campaign.cpp:53:2: warning: when initialized here [-Wreorder]
path(int u, int v, int c): u(u), v(v), l(LCA::lca(u, v)), c(c) {};
^~~~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:168:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
election_campaign.cpp:172:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &U[i], &V[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:174:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &M);
~~~~~^~~~~~~~~~
election_campaign.cpp:177:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &A[i], &B[i], &C[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12160 KB |
Output is correct |
2 |
Correct |
11 ms |
12160 KB |
Output is correct |
3 |
Correct |
11 ms |
12160 KB |
Output is correct |
4 |
Correct |
12 ms |
12288 KB |
Output is correct |
5 |
Correct |
208 ms |
24184 KB |
Output is correct |
6 |
Correct |
117 ms |
38088 KB |
Output is correct |
7 |
Correct |
235 ms |
33660 KB |
Output is correct |
8 |
Correct |
193 ms |
24440 KB |
Output is correct |
9 |
Correct |
241 ms |
30648 KB |
Output is correct |
10 |
Correct |
191 ms |
24380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12160 KB |
Output is correct |
2 |
Correct |
11 ms |
12180 KB |
Output is correct |
3 |
Correct |
12 ms |
12288 KB |
Output is correct |
4 |
Correct |
296 ms |
44508 KB |
Output is correct |
5 |
Correct |
324 ms |
44428 KB |
Output is correct |
6 |
Correct |
308 ms |
44464 KB |
Output is correct |
7 |
Correct |
303 ms |
44408 KB |
Output is correct |
8 |
Correct |
295 ms |
44468 KB |
Output is correct |
9 |
Correct |
322 ms |
44380 KB |
Output is correct |
10 |
Correct |
290 ms |
44408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12160 KB |
Output is correct |
2 |
Correct |
11 ms |
12180 KB |
Output is correct |
3 |
Correct |
12 ms |
12288 KB |
Output is correct |
4 |
Correct |
296 ms |
44508 KB |
Output is correct |
5 |
Correct |
324 ms |
44428 KB |
Output is correct |
6 |
Correct |
308 ms |
44464 KB |
Output is correct |
7 |
Correct |
303 ms |
44408 KB |
Output is correct |
8 |
Correct |
295 ms |
44468 KB |
Output is correct |
9 |
Correct |
322 ms |
44380 KB |
Output is correct |
10 |
Correct |
290 ms |
44408 KB |
Output is correct |
11 |
Correct |
27 ms |
13816 KB |
Output is correct |
12 |
Correct |
332 ms |
44592 KB |
Output is correct |
13 |
Correct |
305 ms |
44792 KB |
Output is correct |
14 |
Correct |
272 ms |
44748 KB |
Output is correct |
15 |
Correct |
260 ms |
44608 KB |
Output is correct |
16 |
Correct |
251 ms |
44668 KB |
Output is correct |
17 |
Correct |
258 ms |
44664 KB |
Output is correct |
18 |
Correct |
265 ms |
44600 KB |
Output is correct |
19 |
Correct |
259 ms |
44668 KB |
Output is correct |
20 |
Correct |
261 ms |
44636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
338 ms |
30204 KB |
Output is correct |
2 |
Correct |
247 ms |
44408 KB |
Output is correct |
3 |
Correct |
543 ms |
39388 KB |
Output is correct |
4 |
Correct |
265 ms |
30572 KB |
Output is correct |
5 |
Correct |
470 ms |
38424 KB |
Output is correct |
6 |
Correct |
266 ms |
30436 KB |
Output is correct |
7 |
Correct |
549 ms |
37992 KB |
Output is correct |
8 |
Correct |
362 ms |
30584 KB |
Output is correct |
9 |
Correct |
312 ms |
44472 KB |
Output is correct |
10 |
Correct |
583 ms |
36352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12160 KB |
Output is correct |
2 |
Correct |
11 ms |
12160 KB |
Output is correct |
3 |
Correct |
11 ms |
12160 KB |
Output is correct |
4 |
Correct |
12 ms |
12288 KB |
Output is correct |
5 |
Correct |
208 ms |
24184 KB |
Output is correct |
6 |
Correct |
117 ms |
38088 KB |
Output is correct |
7 |
Correct |
235 ms |
33660 KB |
Output is correct |
8 |
Correct |
193 ms |
24440 KB |
Output is correct |
9 |
Correct |
241 ms |
30648 KB |
Output is correct |
10 |
Correct |
191 ms |
24380 KB |
Output is correct |
11 |
Correct |
13 ms |
12288 KB |
Output is correct |
12 |
Correct |
13 ms |
12416 KB |
Output is correct |
13 |
Correct |
13 ms |
12388 KB |
Output is correct |
14 |
Correct |
13 ms |
12288 KB |
Output is correct |
15 |
Correct |
13 ms |
12288 KB |
Output is correct |
16 |
Correct |
14 ms |
12364 KB |
Output is correct |
17 |
Correct |
12 ms |
12288 KB |
Output is correct |
18 |
Correct |
13 ms |
12416 KB |
Output is correct |
19 |
Correct |
13 ms |
12288 KB |
Output is correct |
20 |
Correct |
13 ms |
12388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12160 KB |
Output is correct |
2 |
Correct |
11 ms |
12160 KB |
Output is correct |
3 |
Correct |
11 ms |
12160 KB |
Output is correct |
4 |
Correct |
12 ms |
12288 KB |
Output is correct |
5 |
Correct |
208 ms |
24184 KB |
Output is correct |
6 |
Correct |
117 ms |
38088 KB |
Output is correct |
7 |
Correct |
235 ms |
33660 KB |
Output is correct |
8 |
Correct |
193 ms |
24440 KB |
Output is correct |
9 |
Correct |
241 ms |
30648 KB |
Output is correct |
10 |
Correct |
191 ms |
24380 KB |
Output is correct |
11 |
Correct |
11 ms |
12160 KB |
Output is correct |
12 |
Correct |
11 ms |
12180 KB |
Output is correct |
13 |
Correct |
12 ms |
12288 KB |
Output is correct |
14 |
Correct |
296 ms |
44508 KB |
Output is correct |
15 |
Correct |
324 ms |
44428 KB |
Output is correct |
16 |
Correct |
308 ms |
44464 KB |
Output is correct |
17 |
Correct |
303 ms |
44408 KB |
Output is correct |
18 |
Correct |
295 ms |
44468 KB |
Output is correct |
19 |
Correct |
322 ms |
44380 KB |
Output is correct |
20 |
Correct |
290 ms |
44408 KB |
Output is correct |
21 |
Correct |
27 ms |
13816 KB |
Output is correct |
22 |
Correct |
332 ms |
44592 KB |
Output is correct |
23 |
Correct |
305 ms |
44792 KB |
Output is correct |
24 |
Correct |
272 ms |
44748 KB |
Output is correct |
25 |
Correct |
260 ms |
44608 KB |
Output is correct |
26 |
Correct |
251 ms |
44668 KB |
Output is correct |
27 |
Correct |
258 ms |
44664 KB |
Output is correct |
28 |
Correct |
265 ms |
44600 KB |
Output is correct |
29 |
Correct |
259 ms |
44668 KB |
Output is correct |
30 |
Correct |
261 ms |
44636 KB |
Output is correct |
31 |
Correct |
338 ms |
30204 KB |
Output is correct |
32 |
Correct |
247 ms |
44408 KB |
Output is correct |
33 |
Correct |
543 ms |
39388 KB |
Output is correct |
34 |
Correct |
265 ms |
30572 KB |
Output is correct |
35 |
Correct |
470 ms |
38424 KB |
Output is correct |
36 |
Correct |
266 ms |
30436 KB |
Output is correct |
37 |
Correct |
549 ms |
37992 KB |
Output is correct |
38 |
Correct |
362 ms |
30584 KB |
Output is correct |
39 |
Correct |
312 ms |
44472 KB |
Output is correct |
40 |
Correct |
583 ms |
36352 KB |
Output is correct |
41 |
Correct |
13 ms |
12288 KB |
Output is correct |
42 |
Correct |
13 ms |
12416 KB |
Output is correct |
43 |
Correct |
13 ms |
12388 KB |
Output is correct |
44 |
Correct |
13 ms |
12288 KB |
Output is correct |
45 |
Correct |
13 ms |
12288 KB |
Output is correct |
46 |
Correct |
14 ms |
12364 KB |
Output is correct |
47 |
Correct |
12 ms |
12288 KB |
Output is correct |
48 |
Correct |
13 ms |
12416 KB |
Output is correct |
49 |
Correct |
13 ms |
12288 KB |
Output is correct |
50 |
Correct |
13 ms |
12388 KB |
Output is correct |
51 |
Correct |
372 ms |
30868 KB |
Output is correct |
52 |
Correct |
279 ms |
44664 KB |
Output is correct |
53 |
Correct |
535 ms |
36744 KB |
Output is correct |
54 |
Correct |
373 ms |
30944 KB |
Output is correct |
55 |
Correct |
355 ms |
30560 KB |
Output is correct |
56 |
Correct |
315 ms |
44608 KB |
Output is correct |
57 |
Correct |
556 ms |
37816 KB |
Output is correct |
58 |
Correct |
294 ms |
30664 KB |
Output is correct |
59 |
Correct |
374 ms |
30920 KB |
Output is correct |
60 |
Correct |
279 ms |
44664 KB |
Output is correct |
61 |
Correct |
578 ms |
37968 KB |
Output is correct |
62 |
Correct |
299 ms |
30500 KB |
Output is correct |
63 |
Correct |
336 ms |
30292 KB |
Output is correct |
64 |
Correct |
260 ms |
44664 KB |
Output is correct |
65 |
Correct |
512 ms |
37944 KB |
Output is correct |
66 |
Correct |
261 ms |
30828 KB |
Output is correct |
67 |
Correct |
333 ms |
30536 KB |
Output is correct |
68 |
Correct |
326 ms |
44628 KB |
Output is correct |
69 |
Correct |
697 ms |
35828 KB |
Output is correct |
70 |
Correct |
304 ms |
30672 KB |
Output is correct |