#include <iostream>
#include <vector>
using namespace std;
const int NMAX = 1e5;
const int LGMAX = 17;
int N, M;
vector <int> g[NMAX + 2], gNoDad[NMAX + 2];
int kTime, timeIn[NMAX + 2], timeOut[NMAX + 2];
int h[NMAX + 2], dad[NMAX + 2], w[NMAX + 2];
int log2[2 * NMAX + 2], firstAp[NMAX + 2];
vector <int> rmq[LGMAX + 2];
void dfs(int node, int parent = 0) {
dad[node] = parent;
w[node] = 1;
++kTime;
timeIn[node] = kTime;
firstAp[node] = (int)rmq[0].size();
rmq[0].push_back(node);
for(int son : g[node]) {
if(son != parent) {
h[son] = h[node] + 1;
gNoDad[node].push_back(son);
dfs(son, node);
w[node] += w[son];
rmq[0].push_back(node);
}
}
timeOut[node] = kTime;
}
int getMin(int x, int y) {
if(h[x] < h[y]) {
return x;
}
return y;
}
void buildRmq() {
for(int i = 2; i <= 2 * N; i++) {
log2[i] = log2[i / 2] + 1;
}
for(int i = 1; i <= LGMAX; i++) {
if((1 << i) > (int)rmq[0].size()) {
return ;
}
for(int j = 0; j < (int)rmq[0].size(); j++) {
if((1 << i) + j > (int)rmq[0].size()) {
break;
} else {
rmq[i].push_back(getMin(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]));
}
}
}
}
int queryRmq(int x, int y) {
x = firstAp[x];
y = firstAp[y];
if(x > y) {
swap(x, y);
}
int k = log2[y - x + 1];
return getMin(rmq[k][x], rmq[k][y - (1 << k) + 1]);
}
vector <int> heavyOrder;
int head[NMAX + 2], pos[NMAX + 2], hs[NMAX + 2];
void dfsHeavy(int node, int heavyHead) {
head[node] = heavyHead;
heavyOrder.push_back(node);
pos[node] = (int)heavyOrder.size();
int heavySon = g[node][0];
if(g[node].size() == 1) {
if(g[node][0] == dad[node]) {
return ;
}
} else {
if(g[node][0] == dad[node]) {
heavySon = g[node][1];
}
}
for(int son : g[node]) {
if(son != dad[node]) {
if(w[son] > w[heavySon]) {
heavySon = son;
}
}
}
hs[node] = heavySon;
dfsHeavy(heavySon, heavyHead);
for(int son : g[node]) {
if(son != dad[node] && son != heavySon) {
dfsHeavy(son, son);
}
}
}
int dp[NMAX + 2];
struct Road {
int x, y, c;
};
vector <Road> roads[NMAX + 2];
struct Fenwick {
int v[NMAX + 2];
inline int LSB(int x) {
return x & (-x);
}
void Update(int pos, int val) {
for(int i = pos; i <= N; i += LSB(i)) {
v[i] += val;
}
}
int Sum(int pos) {
if(pos <= 0) {
return 0;
}
int ans = 0;
for(int i = pos; i > 0; i -= LSB(i)) {
ans += v[i];
}
return ans;
}
int Query(int st, int dr) {
return Sum(dr) - Sum(st - 1);
}
};
Fenwick aib;
int qr(int x, int y) {
if(head[x] == head[y]) {
if(pos[x] > pos[y]) {
swap(x, y);
}
return aib.Query(pos[x], pos[y]);
}
if(h[head[x]] < h[head[y]]) {
swap(x, y);
}
int ans = qr(dad[head[x]], y);
if(pos[x] > pos[head[x]]) {
ans += aib.Query(pos[head[x]], pos[x]);
} else {
ans += aib.Query(pos[x], pos[head[x]]);
}
x = head[x];
ans -= dp[x];
ans += dp[hs[dad[x]]];
return ans;
}
void solve(int node, int parent = 0) {
int sumDpSons = 0;
for(int son : g[node]) {
if(son != parent) {
solve(son, node);
sumDpSons += dp[son];
}
}
dp[node] = sumDpSons;
for(Road road : roads[node]) {
int newDp = road.c;
pair <int, int> miss = {0, 0};
if(node != road.x) {
int st = 0, dr = (int)gNoDad[node].size() - 1, sol = -1;
while(st <= dr) {
int mid = (st + dr) >> 1;
int trg = gNoDad[node][mid];
if(timeIn[trg] <= timeIn[road.x] && timeOut[road.x] <= timeOut[trg]) {
sol = trg;
break;
}
if(timeIn[trg] > timeOut[road.x]) {
dr = mid - 1;
} else {
st = mid + 1;
}
}
miss.first = sol;
newDp += qr(miss.first, road.x);
newDp += dp[hs[road.x]];
}
if(node != road.y) {
int st = 0, dr = (int)gNoDad[node].size() - 1, sol = -1;
while(st <= dr) {
int mid = (st + dr) >> 1;
int trg = gNoDad[node][mid];
if(timeIn[trg] <= timeIn[road.y] && timeOut[road.y] <= timeOut[trg]) {
sol = trg;
break;
}
if(timeIn[trg] > timeOut[road.y]) {
dr = mid - 1;
} else {
st = mid + 1;
}
}
miss.second = sol;
newDp += qr(miss.second, road.y);
newDp += dp[hs[road.y]];
}
newDp = newDp + sumDpSons - dp[miss.first] - dp[miss.second];
dp[node] = max(dp[node], newDp);
}
///Update for dady!
if(node != 1) {
if(head[node] != head[dad[node]]) {
aib.Update(pos[dad[node]], dp[node]);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
for(int i = 1; i < N; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1);
buildRmq();
dfsHeavy(1, 1);
cin >> M;
for(int i = 1; i <= M; i++) {
int x, y, c;
cin >> x >> y >> c;
int lca = queryRmq(x, y);
roads[lca].push_back({x, y, c});
}
solve(1);
cout << dp[1] << '\n';
return 0;
}
Compilation message
election_campaign.cpp:15:5: warning: built-in function 'log2' declared as non-function [-Wbuiltin-declaration-mismatch]
15 | int log2[2 * NMAX + 2], firstAp[NMAX + 2];
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7532 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7620 KB |
Output is correct |
5 |
Correct |
149 ms |
32484 KB |
Output is correct |
6 |
Correct |
88 ms |
46048 KB |
Output is correct |
7 |
Correct |
138 ms |
41256 KB |
Output is correct |
8 |
Correct |
103 ms |
31076 KB |
Output is correct |
9 |
Correct |
130 ms |
38876 KB |
Output is correct |
10 |
Correct |
105 ms |
31344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7788 KB |
Output is correct |
4 |
Correct |
139 ms |
48864 KB |
Output is correct |
5 |
Correct |
148 ms |
48864 KB |
Output is correct |
6 |
Correct |
141 ms |
48864 KB |
Output is correct |
7 |
Correct |
144 ms |
48864 KB |
Output is correct |
8 |
Correct |
139 ms |
48864 KB |
Output is correct |
9 |
Correct |
135 ms |
48864 KB |
Output is correct |
10 |
Correct |
158 ms |
48864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7404 KB |
Output is correct |
3 |
Correct |
6 ms |
7788 KB |
Output is correct |
4 |
Correct |
139 ms |
48864 KB |
Output is correct |
5 |
Correct |
148 ms |
48864 KB |
Output is correct |
6 |
Correct |
141 ms |
48864 KB |
Output is correct |
7 |
Correct |
144 ms |
48864 KB |
Output is correct |
8 |
Correct |
139 ms |
48864 KB |
Output is correct |
9 |
Correct |
135 ms |
48864 KB |
Output is correct |
10 |
Correct |
158 ms |
48864 KB |
Output is correct |
11 |
Correct |
13 ms |
8556 KB |
Output is correct |
12 |
Correct |
144 ms |
49120 KB |
Output is correct |
13 |
Correct |
147 ms |
49248 KB |
Output is correct |
14 |
Correct |
143 ms |
49120 KB |
Output is correct |
15 |
Correct |
145 ms |
49248 KB |
Output is correct |
16 |
Correct |
141 ms |
49120 KB |
Output is correct |
17 |
Correct |
147 ms |
49248 KB |
Output is correct |
18 |
Correct |
143 ms |
49120 KB |
Output is correct |
19 |
Correct |
139 ms |
49120 KB |
Output is correct |
20 |
Correct |
141 ms |
49120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
259 ms |
34916 KB |
Output is correct |
2 |
Correct |
142 ms |
48992 KB |
Output is correct |
3 |
Correct |
219 ms |
43616 KB |
Output is correct |
4 |
Correct |
180 ms |
33504 KB |
Output is correct |
5 |
Correct |
216 ms |
42676 KB |
Output is correct |
6 |
Correct |
190 ms |
34016 KB |
Output is correct |
7 |
Correct |
214 ms |
42336 KB |
Output is correct |
8 |
Correct |
238 ms |
35172 KB |
Output is correct |
9 |
Correct |
141 ms |
48992 KB |
Output is correct |
10 |
Correct |
215 ms |
41068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7532 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7620 KB |
Output is correct |
5 |
Correct |
149 ms |
32484 KB |
Output is correct |
6 |
Correct |
88 ms |
46048 KB |
Output is correct |
7 |
Correct |
138 ms |
41256 KB |
Output is correct |
8 |
Correct |
103 ms |
31076 KB |
Output is correct |
9 |
Correct |
130 ms |
38876 KB |
Output is correct |
10 |
Correct |
105 ms |
31344 KB |
Output is correct |
11 |
Correct |
7 ms |
7680 KB |
Output is correct |
12 |
Correct |
7 ms |
7788 KB |
Output is correct |
13 |
Correct |
6 ms |
7788 KB |
Output is correct |
14 |
Correct |
6 ms |
7660 KB |
Output is correct |
15 |
Correct |
7 ms |
7660 KB |
Output is correct |
16 |
Correct |
6 ms |
7660 KB |
Output is correct |
17 |
Correct |
6 ms |
7660 KB |
Output is correct |
18 |
Correct |
7 ms |
7788 KB |
Output is correct |
19 |
Correct |
6 ms |
7660 KB |
Output is correct |
20 |
Correct |
7 ms |
7788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7404 KB |
Output is correct |
2 |
Correct |
5 ms |
7532 KB |
Output is correct |
3 |
Correct |
6 ms |
7404 KB |
Output is correct |
4 |
Correct |
6 ms |
7620 KB |
Output is correct |
5 |
Correct |
149 ms |
32484 KB |
Output is correct |
6 |
Correct |
88 ms |
46048 KB |
Output is correct |
7 |
Correct |
138 ms |
41256 KB |
Output is correct |
8 |
Correct |
103 ms |
31076 KB |
Output is correct |
9 |
Correct |
130 ms |
38876 KB |
Output is correct |
10 |
Correct |
105 ms |
31344 KB |
Output is correct |
11 |
Correct |
5 ms |
7404 KB |
Output is correct |
12 |
Correct |
5 ms |
7404 KB |
Output is correct |
13 |
Correct |
6 ms |
7788 KB |
Output is correct |
14 |
Correct |
139 ms |
48864 KB |
Output is correct |
15 |
Correct |
148 ms |
48864 KB |
Output is correct |
16 |
Correct |
141 ms |
48864 KB |
Output is correct |
17 |
Correct |
144 ms |
48864 KB |
Output is correct |
18 |
Correct |
139 ms |
48864 KB |
Output is correct |
19 |
Correct |
135 ms |
48864 KB |
Output is correct |
20 |
Correct |
158 ms |
48864 KB |
Output is correct |
21 |
Correct |
13 ms |
8556 KB |
Output is correct |
22 |
Correct |
144 ms |
49120 KB |
Output is correct |
23 |
Correct |
147 ms |
49248 KB |
Output is correct |
24 |
Correct |
143 ms |
49120 KB |
Output is correct |
25 |
Correct |
145 ms |
49248 KB |
Output is correct |
26 |
Correct |
141 ms |
49120 KB |
Output is correct |
27 |
Correct |
147 ms |
49248 KB |
Output is correct |
28 |
Correct |
143 ms |
49120 KB |
Output is correct |
29 |
Correct |
139 ms |
49120 KB |
Output is correct |
30 |
Correct |
141 ms |
49120 KB |
Output is correct |
31 |
Correct |
259 ms |
34916 KB |
Output is correct |
32 |
Correct |
142 ms |
48992 KB |
Output is correct |
33 |
Correct |
219 ms |
43616 KB |
Output is correct |
34 |
Correct |
180 ms |
33504 KB |
Output is correct |
35 |
Correct |
216 ms |
42676 KB |
Output is correct |
36 |
Correct |
190 ms |
34016 KB |
Output is correct |
37 |
Correct |
214 ms |
42336 KB |
Output is correct |
38 |
Correct |
238 ms |
35172 KB |
Output is correct |
39 |
Correct |
141 ms |
48992 KB |
Output is correct |
40 |
Correct |
215 ms |
41068 KB |
Output is correct |
41 |
Correct |
7 ms |
7680 KB |
Output is correct |
42 |
Correct |
7 ms |
7788 KB |
Output is correct |
43 |
Correct |
6 ms |
7788 KB |
Output is correct |
44 |
Correct |
6 ms |
7660 KB |
Output is correct |
45 |
Correct |
7 ms |
7660 KB |
Output is correct |
46 |
Correct |
6 ms |
7660 KB |
Output is correct |
47 |
Correct |
6 ms |
7660 KB |
Output is correct |
48 |
Correct |
7 ms |
7788 KB |
Output is correct |
49 |
Correct |
6 ms |
7660 KB |
Output is correct |
50 |
Correct |
7 ms |
7788 KB |
Output is correct |
51 |
Correct |
243 ms |
35680 KB |
Output is correct |
52 |
Correct |
148 ms |
49120 KB |
Output is correct |
53 |
Correct |
215 ms |
41504 KB |
Output is correct |
54 |
Correct |
173 ms |
34020 KB |
Output is correct |
55 |
Correct |
264 ms |
35388 KB |
Output is correct |
56 |
Correct |
146 ms |
49120 KB |
Output is correct |
57 |
Correct |
212 ms |
42336 KB |
Output is correct |
58 |
Correct |
186 ms |
33892 KB |
Output is correct |
59 |
Correct |
249 ms |
35492 KB |
Output is correct |
60 |
Correct |
145 ms |
49120 KB |
Output is correct |
61 |
Correct |
213 ms |
42464 KB |
Output is correct |
62 |
Correct |
190 ms |
34528 KB |
Output is correct |
63 |
Correct |
261 ms |
35748 KB |
Output is correct |
64 |
Correct |
142 ms |
49120 KB |
Output is correct |
65 |
Correct |
223 ms |
42912 KB |
Output is correct |
66 |
Correct |
174 ms |
34020 KB |
Output is correct |
67 |
Correct |
265 ms |
35620 KB |
Output is correct |
68 |
Correct |
147 ms |
49120 KB |
Output is correct |
69 |
Correct |
206 ms |
40480 KB |
Output is correct |
70 |
Correct |
188 ms |
34276 KB |
Output is correct |