#include <bits/stdc++.h>
using namespace std;
//#define FILE_IO
typedef long long LL;
const int NMAX = 1e5;
const int LG = 17;
typedef pair<int, int> pii;
LL dp[NMAX + 5], dp0[NMAX + 5];
int N, M;
int h[NMAX + 5], f[NMAX + 5];
int I, itv[NMAX + 5][2], lg2[2 * NMAX + 5], rmq[LG + 1][2 * NMAX + 5];
int cost[NMAX + 5];
pii path[NMAX + 5];
vector<int> edg[NMAX + 5], pth[NMAX + 5];
class BinaryIndexedTree
{
public:
int N;
LL aib[2 * NMAX + 5];
void U(int pos, int val)
{
for(; pos <= N; pos += (pos & (-pos)))
aib[pos] += val;
}
LL Q(int pos)
{
LL ans = 0;
for(; pos > 0; pos -= (pos & (-pos)))
ans += aib[pos];
return ans;
}
}sumdp, sumdp0;
void DFS(int nod, int fth)
{
f[nod] = fth;
h[nod] = h[fth] + 1;
itv[nod][0] = ++I;
rmq[0][I] = nod;
for(auto nxt: edg[nod])
if(nxt != fth)
DFS(nxt, nod);
itv[nod][1] = ++I;
rmq[0][I] = nod;
}
int minh(int a, int b) { if(h[a] < h[b]) return a; return b; }
int LCA(int a, int b)
{
int pa = itv[a][0], pb = itv[b][0];
if(pa > pb) swap(a, b), swap(pa, pb);
if(itv[a][0] <= itv[b][0] && itv[b][1] <= itv[a][1]) return a;
int pw = lg2[pb - pa + 1];
int nod = minh(rmq[pw][pa], rmq[pw][pb - (1 << pw) + 1]);
return f[nod];
}
void solve(int nod)
{
for(auto nxt: edg[nod])
if(nxt != f[nod])
{
solve(nxt);
dp0[nod] += dp[nxt];
}
sumdp0.U(itv[nod][0], dp0[nod]);
sumdp0.U(itv[nod][1], -dp0[nod]);
dp[nod] = dp0[nod];
for(auto id: pth[nod])
{
int x = path[id].first, y = path[id].second;
LL val = cost[id] + dp0[nod];
val += sumdp0.Q(itv[x][0]) - sumdp0.Q(itv[nod][0]);
val += sumdp0.Q(itv[y][0]) - sumdp0.Q(itv[nod][0]);
val -= sumdp.Q(itv[x][0]) - sumdp.Q(itv[nod][0]);
val -= sumdp.Q(itv[y][0]) - sumdp.Q(itv[nod][0]);
dp[nod] = max(dp[nod], val);
}
sumdp.U(itv[nod][0], dp[nod]);
sumdp.U(itv[nod][1], -dp[nod]);
}
int main()
{
#ifdef FILE_IO
freopen("1.in", "r", stdin);
//freopen("1.out", "w", stdout);
#endif
scanf("%d", &N);
for(int i = 1; i < N; i++)
{
int x, y;
scanf("%d%d", &x, &y);
edg[x].push_back(y);
edg[y].push_back(x);
}
DFS(1, 0);
for(int i = 2; i <= I; i++) lg2[i] = lg2[i >> 1] + 1;
for(int i = 1; (1 << i) <= I; i++)
for(int j = 1; j + (1 << i) - 1 <= I; j++)
rmq[i][j] = minh(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]);
scanf("%d", &M);
for(int i = 1; i <= M; i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
path[i] = {a, b};
cost[i] = c;
pth[LCA(a, b)].push_back(i);
}
sumdp.N = I, sumdp0.N = I;
solve(1);
printf("%d\n", dp[1]);
return 0;
}
Compilation message
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:130:25: warning: format '%d' expects argument of type 'int', but argument 2 has type 'LL {aka long long int}' [-Wformat=]
printf("%d\n", dp[1]);
~~~~~^
election_campaign.cpp:103:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
election_campaign.cpp:107:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~
election_campaign.cpp:118:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &M);
~~~~~^~~~~~~~~~
election_campaign.cpp:122:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &a, &b, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5368 KB |
Output is correct |
5 |
Correct |
146 ms |
29688 KB |
Output is correct |
6 |
Correct |
69 ms |
34168 KB |
Output is correct |
7 |
Correct |
131 ms |
32492 KB |
Output is correct |
8 |
Correct |
111 ms |
29176 KB |
Output is correct |
9 |
Correct |
140 ms |
31576 KB |
Output is correct |
10 |
Correct |
130 ms |
29304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5368 KB |
Output is correct |
4 |
Correct |
126 ms |
37724 KB |
Output is correct |
5 |
Correct |
142 ms |
37676 KB |
Output is correct |
6 |
Correct |
135 ms |
37768 KB |
Output is correct |
7 |
Correct |
126 ms |
37760 KB |
Output is correct |
8 |
Correct |
130 ms |
37764 KB |
Output is correct |
9 |
Correct |
130 ms |
37752 KB |
Output is correct |
10 |
Correct |
140 ms |
37752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5368 KB |
Output is correct |
4 |
Correct |
126 ms |
37724 KB |
Output is correct |
5 |
Correct |
142 ms |
37676 KB |
Output is correct |
6 |
Correct |
135 ms |
37768 KB |
Output is correct |
7 |
Correct |
126 ms |
37760 KB |
Output is correct |
8 |
Correct |
130 ms |
37764 KB |
Output is correct |
9 |
Correct |
130 ms |
37752 KB |
Output is correct |
10 |
Correct |
140 ms |
37752 KB |
Output is correct |
11 |
Correct |
16 ms |
6136 KB |
Output is correct |
12 |
Correct |
134 ms |
38116 KB |
Output is correct |
13 |
Correct |
133 ms |
38008 KB |
Output is correct |
14 |
Correct |
121 ms |
38060 KB |
Output is correct |
15 |
Correct |
130 ms |
38008 KB |
Output is correct |
16 |
Correct |
125 ms |
38008 KB |
Output is correct |
17 |
Correct |
128 ms |
37984 KB |
Output is correct |
18 |
Correct |
131 ms |
38132 KB |
Output is correct |
19 |
Correct |
141 ms |
37988 KB |
Output is correct |
20 |
Correct |
125 ms |
38008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
217 ms |
32796 KB |
Output is correct |
2 |
Correct |
116 ms |
37824 KB |
Output is correct |
3 |
Correct |
192 ms |
35772 KB |
Output is correct |
4 |
Correct |
140 ms |
32116 KB |
Output is correct |
5 |
Correct |
235 ms |
35792 KB |
Output is correct |
6 |
Correct |
168 ms |
32280 KB |
Output is correct |
7 |
Correct |
196 ms |
35368 KB |
Output is correct |
8 |
Correct |
184 ms |
33272 KB |
Output is correct |
9 |
Correct |
121 ms |
37804 KB |
Output is correct |
10 |
Correct |
214 ms |
34832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5368 KB |
Output is correct |
5 |
Correct |
146 ms |
29688 KB |
Output is correct |
6 |
Correct |
69 ms |
34168 KB |
Output is correct |
7 |
Correct |
131 ms |
32492 KB |
Output is correct |
8 |
Correct |
111 ms |
29176 KB |
Output is correct |
9 |
Correct |
140 ms |
31576 KB |
Output is correct |
10 |
Correct |
130 ms |
29304 KB |
Output is correct |
11 |
Correct |
7 ms |
5332 KB |
Output is correct |
12 |
Correct |
6 ms |
5368 KB |
Output is correct |
13 |
Correct |
7 ms |
5368 KB |
Output is correct |
14 |
Correct |
6 ms |
5368 KB |
Output is correct |
15 |
Correct |
6 ms |
5368 KB |
Output is correct |
16 |
Correct |
6 ms |
5368 KB |
Output is correct |
17 |
Correct |
7 ms |
5368 KB |
Output is correct |
18 |
Correct |
6 ms |
5368 KB |
Output is correct |
19 |
Correct |
7 ms |
5368 KB |
Output is correct |
20 |
Correct |
6 ms |
5368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5368 KB |
Output is correct |
5 |
Correct |
146 ms |
29688 KB |
Output is correct |
6 |
Correct |
69 ms |
34168 KB |
Output is correct |
7 |
Correct |
131 ms |
32492 KB |
Output is correct |
8 |
Correct |
111 ms |
29176 KB |
Output is correct |
9 |
Correct |
140 ms |
31576 KB |
Output is correct |
10 |
Correct |
130 ms |
29304 KB |
Output is correct |
11 |
Correct |
6 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
5112 KB |
Output is correct |
13 |
Correct |
7 ms |
5368 KB |
Output is correct |
14 |
Correct |
126 ms |
37724 KB |
Output is correct |
15 |
Correct |
142 ms |
37676 KB |
Output is correct |
16 |
Correct |
135 ms |
37768 KB |
Output is correct |
17 |
Correct |
126 ms |
37760 KB |
Output is correct |
18 |
Correct |
130 ms |
37764 KB |
Output is correct |
19 |
Correct |
130 ms |
37752 KB |
Output is correct |
20 |
Correct |
140 ms |
37752 KB |
Output is correct |
21 |
Correct |
16 ms |
6136 KB |
Output is correct |
22 |
Correct |
134 ms |
38116 KB |
Output is correct |
23 |
Correct |
133 ms |
38008 KB |
Output is correct |
24 |
Correct |
121 ms |
38060 KB |
Output is correct |
25 |
Correct |
130 ms |
38008 KB |
Output is correct |
26 |
Correct |
125 ms |
38008 KB |
Output is correct |
27 |
Correct |
128 ms |
37984 KB |
Output is correct |
28 |
Correct |
131 ms |
38132 KB |
Output is correct |
29 |
Correct |
141 ms |
37988 KB |
Output is correct |
30 |
Correct |
125 ms |
38008 KB |
Output is correct |
31 |
Correct |
217 ms |
32796 KB |
Output is correct |
32 |
Correct |
116 ms |
37824 KB |
Output is correct |
33 |
Correct |
192 ms |
35772 KB |
Output is correct |
34 |
Correct |
140 ms |
32116 KB |
Output is correct |
35 |
Correct |
235 ms |
35792 KB |
Output is correct |
36 |
Correct |
168 ms |
32280 KB |
Output is correct |
37 |
Correct |
196 ms |
35368 KB |
Output is correct |
38 |
Correct |
184 ms |
33272 KB |
Output is correct |
39 |
Correct |
121 ms |
37804 KB |
Output is correct |
40 |
Correct |
214 ms |
34832 KB |
Output is correct |
41 |
Correct |
7 ms |
5332 KB |
Output is correct |
42 |
Correct |
6 ms |
5368 KB |
Output is correct |
43 |
Correct |
7 ms |
5368 KB |
Output is correct |
44 |
Correct |
6 ms |
5368 KB |
Output is correct |
45 |
Correct |
6 ms |
5368 KB |
Output is correct |
46 |
Correct |
6 ms |
5368 KB |
Output is correct |
47 |
Correct |
7 ms |
5368 KB |
Output is correct |
48 |
Correct |
6 ms |
5368 KB |
Output is correct |
49 |
Correct |
7 ms |
5368 KB |
Output is correct |
50 |
Correct |
6 ms |
5368 KB |
Output is correct |
51 |
Correct |
221 ms |
33476 KB |
Output is correct |
52 |
Correct |
129 ms |
38008 KB |
Output is correct |
53 |
Correct |
192 ms |
35188 KB |
Output is correct |
54 |
Correct |
200 ms |
32536 KB |
Output is correct |
55 |
Correct |
226 ms |
33056 KB |
Output is correct |
56 |
Correct |
128 ms |
37996 KB |
Output is correct |
57 |
Correct |
271 ms |
35712 KB |
Output is correct |
58 |
Correct |
180 ms |
32500 KB |
Output is correct |
59 |
Correct |
238 ms |
33400 KB |
Output is correct |
60 |
Correct |
129 ms |
38008 KB |
Output is correct |
61 |
Correct |
232 ms |
35864 KB |
Output is correct |
62 |
Correct |
198 ms |
32432 KB |
Output is correct |
63 |
Correct |
183 ms |
32968 KB |
Output is correct |
64 |
Correct |
131 ms |
38008 KB |
Output is correct |
65 |
Correct |
190 ms |
35572 KB |
Output is correct |
66 |
Correct |
189 ms |
32632 KB |
Output is correct |
67 |
Correct |
240 ms |
33012 KB |
Output is correct |
68 |
Correct |
132 ms |
38008 KB |
Output is correct |
69 |
Correct |
271 ms |
35016 KB |
Output is correct |
70 |
Correct |
229 ms |
32632 KB |
Output is correct |