#include <stdio.h>
#include <stdlib.h>
#define N 100000
#define M 100000
long long max(long long a, long long b) { return a > b ? a : b; }
void append(int **ej, int *eo, int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
int uu[M], vv[M], ww[M];
int *ej[N], eo[N], *eh[N], eo_[N], dd[N], pp[N], qq[N], ta[N], tb[N], n; long long dp[N];
int dfs1(int p, int i, int d) {
int o, s, k_, j_;
dd[i] = d, pp[i] = p;
s = 1, k_ = 0, j_ = -1;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
int k = dfs1(i, j, d + 1);
s += k;
if (k_ < k)
k_ = k, j_ = j;
}
}
qq[i] = j_;
return s;
}
void dfs2(int p, int i, int q) {
static int time;
int o, j_;
ta[i] = time++;
j_ = qq[i], qq[i] = q;
if (j_ != -1)
dfs2(i, j_, q);
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p && j != j_)
dfs2(i, j, j);
}
tb[i] = time;
}
int lca(int i, int j) {
while (qq[i] != qq[j])
if (dd[qq[i]] > dd[qq[j]])
i = pp[qq[i]];
else
j = pp[qq[j]];
return dd[i] < dd[j] ? i : j;
}
long long ft[N];
void update(int i, int n, long long x) {
while (i < n) {
ft[i] += x;
i |= i + 1;
}
}
long long query(int i) {
long long x = 0;
while (i >= 0) {
x += ft[i];
i &= i + 1, i--;
}
return x;
}
void dfs3(int p, int i) {
int o;
long long x, y;
x = 0;
for (o = eo[i]; o--; ) {
int j = ej[i][o];
if (j != p) {
dfs3(i, j);
x += dp[j];
}
}
y = 0;
for (o = eo_[i]; o--; ) {
int h = eh[i][o];
y = max(y, ww[h] - (uu[h] == i ? 0 : query(ta[uu[h]])) - (vv[h] == i ? 0 : query(ta[vv[h]])));
}
dp[i] = x + y;
update(ta[i], n, y), update(tb[i], n, -y);
}
int main() {
int m, h, i, j;
scanf("%d", &n);
for (i = 0; i < n; i++) {
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
}
for (h = 0; h < n - 1; h++) {
scanf("%d%d", &i, &j), i--, j--;
append(ej, eo, i, j), append(ej, eo, j, i);
}
dfs1(-1, 0, 0);
dfs2(-1, 0, 0);
scanf("%d", &m);
for (h = 0; h < m; h++) {
scanf("%d%d%d", &uu[h], &vv[h], &ww[h]), uu[h]--, vv[h]--;
append(eh, eo_, lca(uu[h], vv[h]), h);
}
dfs3(-1, 0);
printf("%lld\n", dp[0]);
return 0;
}
Compilation message
election_campaign.c: In function 'append':
election_campaign.c:12:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
12 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
election_campaign.c: In function 'main':
election_campaign.c:111:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
election_campaign.c:117:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
election_campaign.c:122:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | scanf("%d", &m);
| ^~~~~~~~~~~~~~~
election_campaign.c:124:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
124 | scanf("%d%d%d", &uu[h], &vv[h], &ww[h]), uu[h]--, vv[h]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
71 ms |
13476 KB |
Output is correct |
6 |
Correct |
48 ms |
22556 KB |
Output is correct |
7 |
Correct |
78 ms |
19300 KB |
Output is correct |
8 |
Correct |
55 ms |
13776 KB |
Output is correct |
9 |
Correct |
84 ms |
17472 KB |
Output is correct |
10 |
Correct |
76 ms |
13764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
83 ms |
25524 KB |
Output is correct |
5 |
Correct |
87 ms |
25492 KB |
Output is correct |
6 |
Correct |
83 ms |
25444 KB |
Output is correct |
7 |
Correct |
90 ms |
25508 KB |
Output is correct |
8 |
Correct |
82 ms |
25508 KB |
Output is correct |
9 |
Correct |
81 ms |
25456 KB |
Output is correct |
10 |
Correct |
82 ms |
25524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
83 ms |
25524 KB |
Output is correct |
5 |
Correct |
87 ms |
25492 KB |
Output is correct |
6 |
Correct |
83 ms |
25444 KB |
Output is correct |
7 |
Correct |
90 ms |
25508 KB |
Output is correct |
8 |
Correct |
82 ms |
25508 KB |
Output is correct |
9 |
Correct |
81 ms |
25456 KB |
Output is correct |
10 |
Correct |
82 ms |
25524 KB |
Output is correct |
11 |
Correct |
8 ms |
1292 KB |
Output is correct |
12 |
Correct |
90 ms |
25788 KB |
Output is correct |
13 |
Correct |
102 ms |
25820 KB |
Output is correct |
14 |
Correct |
82 ms |
25660 KB |
Output is correct |
15 |
Correct |
82 ms |
25828 KB |
Output is correct |
16 |
Correct |
83 ms |
25740 KB |
Output is correct |
17 |
Correct |
95 ms |
25820 KB |
Output is correct |
18 |
Correct |
81 ms |
25832 KB |
Output is correct |
19 |
Correct |
84 ms |
25768 KB |
Output is correct |
20 |
Correct |
95 ms |
25800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
16828 KB |
Output is correct |
2 |
Correct |
83 ms |
25400 KB |
Output is correct |
3 |
Correct |
130 ms |
22332 KB |
Output is correct |
4 |
Correct |
94 ms |
16776 KB |
Output is correct |
5 |
Correct |
142 ms |
21548 KB |
Output is correct |
6 |
Correct |
94 ms |
16920 KB |
Output is correct |
7 |
Correct |
124 ms |
21408 KB |
Output is correct |
8 |
Correct |
124 ms |
16552 KB |
Output is correct |
9 |
Correct |
81 ms |
25424 KB |
Output is correct |
10 |
Correct |
121 ms |
20404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
71 ms |
13476 KB |
Output is correct |
6 |
Correct |
48 ms |
22556 KB |
Output is correct |
7 |
Correct |
78 ms |
19300 KB |
Output is correct |
8 |
Correct |
55 ms |
13776 KB |
Output is correct |
9 |
Correct |
84 ms |
17472 KB |
Output is correct |
10 |
Correct |
76 ms |
13764 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
564 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
71 ms |
13476 KB |
Output is correct |
6 |
Correct |
48 ms |
22556 KB |
Output is correct |
7 |
Correct |
78 ms |
19300 KB |
Output is correct |
8 |
Correct |
55 ms |
13776 KB |
Output is correct |
9 |
Correct |
84 ms |
17472 KB |
Output is correct |
10 |
Correct |
76 ms |
13764 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
296 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
83 ms |
25524 KB |
Output is correct |
15 |
Correct |
87 ms |
25492 KB |
Output is correct |
16 |
Correct |
83 ms |
25444 KB |
Output is correct |
17 |
Correct |
90 ms |
25508 KB |
Output is correct |
18 |
Correct |
82 ms |
25508 KB |
Output is correct |
19 |
Correct |
81 ms |
25456 KB |
Output is correct |
20 |
Correct |
82 ms |
25524 KB |
Output is correct |
21 |
Correct |
8 ms |
1292 KB |
Output is correct |
22 |
Correct |
90 ms |
25788 KB |
Output is correct |
23 |
Correct |
102 ms |
25820 KB |
Output is correct |
24 |
Correct |
82 ms |
25660 KB |
Output is correct |
25 |
Correct |
82 ms |
25828 KB |
Output is correct |
26 |
Correct |
83 ms |
25740 KB |
Output is correct |
27 |
Correct |
95 ms |
25820 KB |
Output is correct |
28 |
Correct |
81 ms |
25832 KB |
Output is correct |
29 |
Correct |
84 ms |
25768 KB |
Output is correct |
30 |
Correct |
95 ms |
25800 KB |
Output is correct |
31 |
Correct |
124 ms |
16828 KB |
Output is correct |
32 |
Correct |
83 ms |
25400 KB |
Output is correct |
33 |
Correct |
130 ms |
22332 KB |
Output is correct |
34 |
Correct |
94 ms |
16776 KB |
Output is correct |
35 |
Correct |
142 ms |
21548 KB |
Output is correct |
36 |
Correct |
94 ms |
16920 KB |
Output is correct |
37 |
Correct |
124 ms |
21408 KB |
Output is correct |
38 |
Correct |
124 ms |
16552 KB |
Output is correct |
39 |
Correct |
81 ms |
25424 KB |
Output is correct |
40 |
Correct |
121 ms |
20404 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
596 KB |
Output is correct |
43 |
Correct |
1 ms |
564 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
568 KB |
Output is correct |
51 |
Correct |
128 ms |
16892 KB |
Output is correct |
52 |
Correct |
87 ms |
25848 KB |
Output is correct |
53 |
Correct |
149 ms |
20808 KB |
Output is correct |
54 |
Correct |
96 ms |
17100 KB |
Output is correct |
55 |
Correct |
119 ms |
17100 KB |
Output is correct |
56 |
Correct |
88 ms |
25820 KB |
Output is correct |
57 |
Correct |
126 ms |
21196 KB |
Output is correct |
58 |
Correct |
95 ms |
17060 KB |
Output is correct |
59 |
Correct |
121 ms |
16836 KB |
Output is correct |
60 |
Correct |
82 ms |
25844 KB |
Output is correct |
61 |
Correct |
128 ms |
21420 KB |
Output is correct |
62 |
Correct |
96 ms |
17096 KB |
Output is correct |
63 |
Correct |
121 ms |
17072 KB |
Output is correct |
64 |
Correct |
85 ms |
25780 KB |
Output is correct |
65 |
Correct |
123 ms |
21548 KB |
Output is correct |
66 |
Correct |
91 ms |
17004 KB |
Output is correct |
67 |
Correct |
118 ms |
17168 KB |
Output is correct |
68 |
Correct |
80 ms |
25832 KB |
Output is correct |
69 |
Correct |
127 ms |
19916 KB |
Output is correct |
70 |
Correct |
97 ms |
17172 KB |
Output is correct |