#include <bits/stdc++.h>
#define ll long long
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 2e5 + 7
using namespace std;
int n, m;
int cha[maxn][20];
ll dp[maxn], Sum[maxn];
struct dl { int x, y, w; } a[maxn];
vector<int> edges[maxn], store[maxn];
int In[maxn], Out[maxn];
void pre_dfs(int u, int par) {
static int Time = 0;
In[u] = ++Time;
for(int v : edges[u]) if(v != par) {
cha[v][0] = u;
fi(i, 1, 17) cha[v][i] = cha[cha[v][i - 1]][i - 1];
pre_dfs(v, u);
}
Out[u] = Time;
}
bool insubtree(int u, int p) { return p == 0 || In[p] <= In[u] && In[u] <= Out[p]; }
int lca(int u, int v) {
if(insubtree(u, v)) return v;
if(insubtree(v, u)) return u;
fid(i, 17, 0) if(!insubtree(v, cha[u][i])) u = cha[u][i];
return cha[u][0];
}
struct BIT {
ll bit[maxn];
void update(int l, int r, ll val) {
for(; l < maxn; l += (l & -l)) bit[l] += val;
for(++r; r < maxn; r += (r & -r)) bit[r] -= val;
}
ll get(int x, ll ans = 0) {
for(; x > 0; x -= (x & -x)) ans += bit[x];
return ans;
}
} Bit;
void dfs(int u, int par)
{
for(int v : edges[u]) if(v != par) dfs(v, u), Sum[u] += dp[v];
dp[u] = Sum[u];
for(int id : store[u]) {
ll val = a[id].w;
if(a[id].x != u) val += Bit.get(In[a[id].x]);
if(a[id].y != u) val += Bit.get(In[a[id].y]);
dp[u] = max(dp[u], Sum[u] + val);
}
Bit.update(In[u], Out[u], Sum[u] - dp[u]);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
fi(i, 1, n - 1) {
int x, y; cin >> x >> y;
edges[x].push_back(y), edges[y].push_back(x);
}
pre_dfs(1, 0);
cin >> m;
fi(i, 1, m) {
int x, y, w; cin >> x >> y >> w;
store[lca(x, y)].push_back(i);
a[i] = {x, y, w};
}
dfs(1, 0);
cout << dp[1];
return 0;
}
Compilation message
election_campaign.cpp: In function 'bool insubtree(int, int)':
election_campaign.cpp:28:64: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
28 | bool insubtree(int u, int p) { return p == 0 || In[p] <= In[u] && In[u] <= Out[p]; }
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16732 KB |
Output is correct |
4 |
Correct |
4 ms |
16732 KB |
Output is correct |
5 |
Correct |
70 ms |
28752 KB |
Output is correct |
6 |
Correct |
38 ms |
38676 KB |
Output is correct |
7 |
Correct |
57 ms |
35408 KB |
Output is correct |
8 |
Correct |
41 ms |
28252 KB |
Output is correct |
9 |
Correct |
64 ms |
32824 KB |
Output is correct |
10 |
Correct |
42 ms |
28312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
77 ms |
39876 KB |
Output is correct |
5 |
Correct |
68 ms |
39656 KB |
Output is correct |
6 |
Correct |
64 ms |
39636 KB |
Output is correct |
7 |
Correct |
73 ms |
39764 KB |
Output is correct |
8 |
Correct |
65 ms |
39752 KB |
Output is correct |
9 |
Correct |
64 ms |
39744 KB |
Output is correct |
10 |
Correct |
64 ms |
39764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16988 KB |
Output is correct |
4 |
Correct |
77 ms |
39876 KB |
Output is correct |
5 |
Correct |
68 ms |
39656 KB |
Output is correct |
6 |
Correct |
64 ms |
39636 KB |
Output is correct |
7 |
Correct |
73 ms |
39764 KB |
Output is correct |
8 |
Correct |
65 ms |
39752 KB |
Output is correct |
9 |
Correct |
64 ms |
39744 KB |
Output is correct |
10 |
Correct |
64 ms |
39764 KB |
Output is correct |
11 |
Correct |
8 ms |
17240 KB |
Output is correct |
12 |
Correct |
70 ms |
39736 KB |
Output is correct |
13 |
Correct |
72 ms |
39760 KB |
Output is correct |
14 |
Correct |
70 ms |
39768 KB |
Output is correct |
15 |
Correct |
67 ms |
39552 KB |
Output is correct |
16 |
Correct |
65 ms |
39732 KB |
Output is correct |
17 |
Correct |
67 ms |
39712 KB |
Output is correct |
18 |
Correct |
65 ms |
39548 KB |
Output is correct |
19 |
Correct |
67 ms |
39776 KB |
Output is correct |
20 |
Correct |
75 ms |
39632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
29132 KB |
Output is correct |
2 |
Correct |
63 ms |
39660 KB |
Output is correct |
3 |
Correct |
102 ms |
35156 KB |
Output is correct |
4 |
Correct |
74 ms |
28620 KB |
Output is correct |
5 |
Correct |
109 ms |
34640 KB |
Output is correct |
6 |
Correct |
69 ms |
28880 KB |
Output is correct |
7 |
Correct |
116 ms |
34668 KB |
Output is correct |
8 |
Correct |
80 ms |
29636 KB |
Output is correct |
9 |
Correct |
68 ms |
39600 KB |
Output is correct |
10 |
Correct |
97 ms |
34148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16732 KB |
Output is correct |
4 |
Correct |
4 ms |
16732 KB |
Output is correct |
5 |
Correct |
70 ms |
28752 KB |
Output is correct |
6 |
Correct |
38 ms |
38676 KB |
Output is correct |
7 |
Correct |
57 ms |
35408 KB |
Output is correct |
8 |
Correct |
41 ms |
28252 KB |
Output is correct |
9 |
Correct |
64 ms |
32824 KB |
Output is correct |
10 |
Correct |
42 ms |
28312 KB |
Output is correct |
11 |
Correct |
4 ms |
16916 KB |
Output is correct |
12 |
Correct |
4 ms |
16988 KB |
Output is correct |
13 |
Correct |
4 ms |
16988 KB |
Output is correct |
14 |
Correct |
3 ms |
16732 KB |
Output is correct |
15 |
Correct |
5 ms |
16732 KB |
Output is correct |
16 |
Correct |
3 ms |
16988 KB |
Output is correct |
17 |
Correct |
3 ms |
16732 KB |
Output is correct |
18 |
Correct |
4 ms |
16988 KB |
Output is correct |
19 |
Correct |
4 ms |
16728 KB |
Output is correct |
20 |
Correct |
4 ms |
17184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16728 KB |
Output is correct |
2 |
Correct |
3 ms |
16732 KB |
Output is correct |
3 |
Correct |
3 ms |
16732 KB |
Output is correct |
4 |
Correct |
4 ms |
16732 KB |
Output is correct |
5 |
Correct |
70 ms |
28752 KB |
Output is correct |
6 |
Correct |
38 ms |
38676 KB |
Output is correct |
7 |
Correct |
57 ms |
35408 KB |
Output is correct |
8 |
Correct |
41 ms |
28252 KB |
Output is correct |
9 |
Correct |
64 ms |
32824 KB |
Output is correct |
10 |
Correct |
42 ms |
28312 KB |
Output is correct |
11 |
Correct |
3 ms |
16728 KB |
Output is correct |
12 |
Correct |
3 ms |
16732 KB |
Output is correct |
13 |
Correct |
3 ms |
16988 KB |
Output is correct |
14 |
Correct |
77 ms |
39876 KB |
Output is correct |
15 |
Correct |
68 ms |
39656 KB |
Output is correct |
16 |
Correct |
64 ms |
39636 KB |
Output is correct |
17 |
Correct |
73 ms |
39764 KB |
Output is correct |
18 |
Correct |
65 ms |
39752 KB |
Output is correct |
19 |
Correct |
64 ms |
39744 KB |
Output is correct |
20 |
Correct |
64 ms |
39764 KB |
Output is correct |
21 |
Correct |
8 ms |
17240 KB |
Output is correct |
22 |
Correct |
70 ms |
39736 KB |
Output is correct |
23 |
Correct |
72 ms |
39760 KB |
Output is correct |
24 |
Correct |
70 ms |
39768 KB |
Output is correct |
25 |
Correct |
67 ms |
39552 KB |
Output is correct |
26 |
Correct |
65 ms |
39732 KB |
Output is correct |
27 |
Correct |
67 ms |
39712 KB |
Output is correct |
28 |
Correct |
65 ms |
39548 KB |
Output is correct |
29 |
Correct |
67 ms |
39776 KB |
Output is correct |
30 |
Correct |
75 ms |
39632 KB |
Output is correct |
31 |
Correct |
81 ms |
29132 KB |
Output is correct |
32 |
Correct |
63 ms |
39660 KB |
Output is correct |
33 |
Correct |
102 ms |
35156 KB |
Output is correct |
34 |
Correct |
74 ms |
28620 KB |
Output is correct |
35 |
Correct |
109 ms |
34640 KB |
Output is correct |
36 |
Correct |
69 ms |
28880 KB |
Output is correct |
37 |
Correct |
116 ms |
34668 KB |
Output is correct |
38 |
Correct |
80 ms |
29636 KB |
Output is correct |
39 |
Correct |
68 ms |
39600 KB |
Output is correct |
40 |
Correct |
97 ms |
34148 KB |
Output is correct |
41 |
Correct |
4 ms |
16916 KB |
Output is correct |
42 |
Correct |
4 ms |
16988 KB |
Output is correct |
43 |
Correct |
4 ms |
16988 KB |
Output is correct |
44 |
Correct |
3 ms |
16732 KB |
Output is correct |
45 |
Correct |
5 ms |
16732 KB |
Output is correct |
46 |
Correct |
3 ms |
16988 KB |
Output is correct |
47 |
Correct |
3 ms |
16732 KB |
Output is correct |
48 |
Correct |
4 ms |
16988 KB |
Output is correct |
49 |
Correct |
4 ms |
16728 KB |
Output is correct |
50 |
Correct |
4 ms |
17184 KB |
Output is correct |
51 |
Correct |
89 ms |
29592 KB |
Output is correct |
52 |
Correct |
68 ms |
39568 KB |
Output is correct |
53 |
Correct |
117 ms |
33492 KB |
Output is correct |
54 |
Correct |
78 ms |
28756 KB |
Output is correct |
55 |
Correct |
89 ms |
29136 KB |
Output is correct |
56 |
Correct |
66 ms |
39572 KB |
Output is correct |
57 |
Correct |
107 ms |
34388 KB |
Output is correct |
58 |
Correct |
70 ms |
28872 KB |
Output is correct |
59 |
Correct |
102 ms |
29552 KB |
Output is correct |
60 |
Correct |
69 ms |
39760 KB |
Output is correct |
61 |
Correct |
91 ms |
34388 KB |
Output is correct |
62 |
Correct |
66 ms |
28692 KB |
Output is correct |
63 |
Correct |
97 ms |
29928 KB |
Output is correct |
64 |
Correct |
67 ms |
39732 KB |
Output is correct |
65 |
Correct |
97 ms |
34256 KB |
Output is correct |
66 |
Correct |
69 ms |
29520 KB |
Output is correct |
67 |
Correct |
89 ms |
30248 KB |
Output is correct |
68 |
Correct |
65 ms |
39588 KB |
Output is correct |
69 |
Correct |
91 ms |
32848 KB |
Output is correct |
70 |
Correct |
69 ms |
28756 KB |
Output is correct |