#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 200000
long long min(long long a, long long b) { return a < b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int ij[(N - 1) * 2], ww[(N - 1) * 2];
int *eh[N], eo[N];
void append(int i, int h) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
eh[i][o] = h;
}
void remove_(int i, int h) {
int o;
for (o = eo[i]; o--; )
if (eh[i][o] == h) {
eo[i]--;
while (o < eo[i])
eh[i][o] = eh[i][o + 1], o++;
return;
}
}
int sz[N], c;
void dfs1(int p, int i, int n) {
int o, centroid;
sz[i] = 1, centroid = 1;
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = ij[h ^ 1];
if (j != p) {
dfs1(i, j, n);
sz[i] += sz[j];
if (sz[j] * 2 > n)
centroid = 0;
}
}
if ((n - sz[i]) * 2 > n)
centroid = 0;
if (centroid)
c = i;
}
int cc[N], cnt; long long xx[N], xx_[N], ss[N];
void dfs2(int p, int c, int i) {
int o, j_;
j_ = -1;
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = ij[h ^ 1];
if (j != p) {
dfs2(i, c, j);
xx[j] += ww[h];
if (j_ == -1 || xx[j_] < xx[j])
j_ = j;
}
}
xx[i] = j_ == -1 ? 0 : xx[j_];
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = ij[h ^ 1];
if (j != p && j != j_)
cc[cnt] = c, xx_[cnt] = xx[j], cnt++;
}
}
void sort(int *gg, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, g = gg[l + rand_() % (r - l)], tmp;
while (j < k)
if (xx_[gg[j]] == xx_[g])
j++;
else if (xx_[gg[j]] > xx_[g]) {
tmp = gg[i], gg[i] = gg[j], gg[j] = tmp;
i++, j++;
} else {
k--;
tmp = gg[j], gg[j] = gg[k], gg[k] = tmp;
}
sort(gg, l, i);
l = k;
}
}
long long ans[N + 1];
void cd(int i, int n, long long x) {
static int gg[N];
int o, g, k;
long long sum;
if (n == 1) {
ans[1] = min(ans[1], x);
return;
}
dfs1(-1, i, n), i = c;
cnt = 0;
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = ij[h ^ 1];
dfs2(i, j, j);
cc[cnt] = j, xx_[cnt] = xx[j] + ww[h], cnt++;
}
cc[cnt] = i, xx_[cnt] = 0, cnt++;
for (g = 0; g < cnt; g++)
ss[cc[g]] = 0;
sum = 0;
for (g = 0; g < cnt; g++) {
sum += xx_[g];
ss[cc[g]] += xx_[g];
}
for (g = 0; g < cnt; g++)
gg[g] = g;
sort(gg, 0, cnt);
for (g = 1; g < cnt; g++)
if (cc[gg[g]] != cc[gg[g - 1]]) {
ans[1] = min(ans[1], x += sum);
x -= xx_[gg[g]];
for (k = 2; k <= cnt; k++) {
x -= k <= g + 1 ? xx_[gg[k - 2]] : xx_[gg[k - 1]];
ans[k] = min(ans[k], x);
}
break;
}
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = ij[h ^ 1];
remove_(j, h ^ 1);
cd(j, sz[j] < sz[i] ? sz[j] : n - sz[i], x + sum - ss[j] + ww[h ^ 1]);
}
}
int main() {
int n, q, h, i, j, k;
scanf("%d", &n);
for (i = 0; i < n; i++)
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
for (h = 0; h < n - 1; h++) {
scanf("%d%d%d%d", &i, &j, &ww[h << 1 | 0], &ww[h << 1 | 1]), i--, j--;
ij[h << 1 | 0] = i, ij[h << 1 | 1] = j;
append(i, h << 1 | 0), append(j, h << 1 | 1);
}
memset(ans, 0x3f, (n + 1) * sizeof *ans);
cd(0, n, 0);
for (k = 1; k <= n; k++)
ans[k] = min(ans[k], ans[k - 1]);
scanf("%d", &q);
while (q--) {
scanf("%d", &k);
printf("%lld\n", ans[k]);
}
return 0;
}
Compilation message
designated_cities.c: In function 'append':
designated_cities.c:21:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
21 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
designated_cities.c: In function 'main':
designated_cities.c:155:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
155 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
designated_cities.c:159:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
159 | scanf("%d%d%d%d", &i, &j, &ww[h << 1 | 0], &ww[h << 1 | 1]), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.c:167:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
167 | scanf("%d", &q);
| ^~~~~~~~~~~~~~~
designated_cities.c:169:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
169 | scanf("%d", &k);
| ^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
668 ms |
25788 KB |
Output is correct |
3 |
Correct |
1010 ms |
36636 KB |
Output is correct |
4 |
Correct |
628 ms |
24204 KB |
Output is correct |
5 |
Correct |
273 ms |
25636 KB |
Output is correct |
6 |
Correct |
802 ms |
27112 KB |
Output is correct |
7 |
Correct |
232 ms |
26552 KB |
Output is correct |
8 |
Correct |
905 ms |
37084 KB |
Output is correct |
9 |
Correct |
213 ms |
27980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
677 ms |
25480 KB |
Output is correct |
3 |
Correct |
969 ms |
38760 KB |
Output is correct |
4 |
Correct |
698 ms |
24084 KB |
Output is correct |
5 |
Correct |
283 ms |
25672 KB |
Output is correct |
6 |
Correct |
805 ms |
27428 KB |
Output is correct |
7 |
Correct |
228 ms |
27336 KB |
Output is correct |
8 |
Correct |
870 ms |
32808 KB |
Output is correct |
9 |
Correct |
180 ms |
27584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
596 KB |
Output is correct |
14 |
Correct |
3 ms |
556 KB |
Output is correct |
15 |
Correct |
3 ms |
596 KB |
Output is correct |
16 |
Correct |
3 ms |
564 KB |
Output is correct |
17 |
Correct |
3 ms |
596 KB |
Output is correct |
18 |
Correct |
3 ms |
596 KB |
Output is correct |
19 |
Correct |
3 ms |
560 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
3 ms |
560 KB |
Output is correct |
22 |
Correct |
3 ms |
596 KB |
Output is correct |
23 |
Correct |
4 ms |
680 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
3 ms |
724 KB |
Output is correct |
26 |
Correct |
2 ms |
560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
668 ms |
25788 KB |
Output is correct |
3 |
Correct |
1010 ms |
36636 KB |
Output is correct |
4 |
Correct |
628 ms |
24204 KB |
Output is correct |
5 |
Correct |
273 ms |
25636 KB |
Output is correct |
6 |
Correct |
802 ms |
27112 KB |
Output is correct |
7 |
Correct |
232 ms |
26552 KB |
Output is correct |
8 |
Correct |
905 ms |
37084 KB |
Output is correct |
9 |
Correct |
213 ms |
27980 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
677 ms |
25480 KB |
Output is correct |
12 |
Correct |
969 ms |
38760 KB |
Output is correct |
13 |
Correct |
698 ms |
24084 KB |
Output is correct |
14 |
Correct |
283 ms |
25672 KB |
Output is correct |
15 |
Correct |
805 ms |
27428 KB |
Output is correct |
16 |
Correct |
228 ms |
27336 KB |
Output is correct |
17 |
Correct |
870 ms |
32808 KB |
Output is correct |
18 |
Correct |
180 ms |
27584 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
680 ms |
25484 KB |
Output is correct |
21 |
Correct |
905 ms |
38932 KB |
Output is correct |
22 |
Correct |
613 ms |
24144 KB |
Output is correct |
23 |
Correct |
647 ms |
25484 KB |
Output is correct |
24 |
Correct |
656 ms |
24348 KB |
Output is correct |
25 |
Correct |
650 ms |
25448 KB |
Output is correct |
26 |
Correct |
609 ms |
24224 KB |
Output is correct |
27 |
Correct |
275 ms |
25640 KB |
Output is correct |
28 |
Correct |
793 ms |
27208 KB |
Output is correct |
29 |
Correct |
599 ms |
25440 KB |
Output is correct |
30 |
Correct |
569 ms |
25100 KB |
Output is correct |
31 |
Correct |
222 ms |
26752 KB |
Output is correct |
32 |
Correct |
922 ms |
33416 KB |
Output is correct |
33 |
Correct |
196 ms |
27880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
292 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
668 ms |
25788 KB |
Output is correct |
14 |
Correct |
1010 ms |
36636 KB |
Output is correct |
15 |
Correct |
628 ms |
24204 KB |
Output is correct |
16 |
Correct |
273 ms |
25636 KB |
Output is correct |
17 |
Correct |
802 ms |
27112 KB |
Output is correct |
18 |
Correct |
232 ms |
26552 KB |
Output is correct |
19 |
Correct |
905 ms |
37084 KB |
Output is correct |
20 |
Correct |
213 ms |
27980 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
677 ms |
25480 KB |
Output is correct |
23 |
Correct |
969 ms |
38760 KB |
Output is correct |
24 |
Correct |
698 ms |
24084 KB |
Output is correct |
25 |
Correct |
283 ms |
25672 KB |
Output is correct |
26 |
Correct |
805 ms |
27428 KB |
Output is correct |
27 |
Correct |
228 ms |
27336 KB |
Output is correct |
28 |
Correct |
870 ms |
32808 KB |
Output is correct |
29 |
Correct |
180 ms |
27584 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
3 ms |
596 KB |
Output is correct |
32 |
Correct |
3 ms |
556 KB |
Output is correct |
33 |
Correct |
3 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
564 KB |
Output is correct |
35 |
Correct |
3 ms |
596 KB |
Output is correct |
36 |
Correct |
3 ms |
596 KB |
Output is correct |
37 |
Correct |
3 ms |
560 KB |
Output is correct |
38 |
Correct |
2 ms |
596 KB |
Output is correct |
39 |
Correct |
3 ms |
560 KB |
Output is correct |
40 |
Correct |
3 ms |
596 KB |
Output is correct |
41 |
Correct |
4 ms |
680 KB |
Output is correct |
42 |
Correct |
2 ms |
596 KB |
Output is correct |
43 |
Correct |
3 ms |
724 KB |
Output is correct |
44 |
Correct |
2 ms |
560 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
680 ms |
25484 KB |
Output is correct |
47 |
Correct |
905 ms |
38932 KB |
Output is correct |
48 |
Correct |
613 ms |
24144 KB |
Output is correct |
49 |
Correct |
647 ms |
25484 KB |
Output is correct |
50 |
Correct |
656 ms |
24348 KB |
Output is correct |
51 |
Correct |
650 ms |
25448 KB |
Output is correct |
52 |
Correct |
609 ms |
24224 KB |
Output is correct |
53 |
Correct |
275 ms |
25640 KB |
Output is correct |
54 |
Correct |
793 ms |
27208 KB |
Output is correct |
55 |
Correct |
599 ms |
25440 KB |
Output is correct |
56 |
Correct |
569 ms |
25100 KB |
Output is correct |
57 |
Correct |
222 ms |
26752 KB |
Output is correct |
58 |
Correct |
922 ms |
33416 KB |
Output is correct |
59 |
Correct |
196 ms |
27880 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
750 ms |
28112 KB |
Output is correct |
62 |
Correct |
980 ms |
38364 KB |
Output is correct |
63 |
Correct |
655 ms |
26700 KB |
Output is correct |
64 |
Correct |
671 ms |
28016 KB |
Output is correct |
65 |
Correct |
661 ms |
26612 KB |
Output is correct |
66 |
Correct |
712 ms |
28016 KB |
Output is correct |
67 |
Correct |
704 ms |
26784 KB |
Output is correct |
68 |
Correct |
316 ms |
28484 KB |
Output is correct |
69 |
Correct |
844 ms |
29692 KB |
Output is correct |
70 |
Correct |
696 ms |
27908 KB |
Output is correct |
71 |
Correct |
659 ms |
27628 KB |
Output is correct |
72 |
Correct |
271 ms |
29776 KB |
Output is correct |
73 |
Correct |
980 ms |
34500 KB |
Output is correct |
74 |
Correct |
244 ms |
32052 KB |
Output is correct |