#include <stdio.h>
#include <stdlib.h>
#define N 100000
#define W 100000
int max(int a, int b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int n, k;
int ij[N - 1], ww[N - 1];
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;
}
int sz[N], c;
void dfs(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 = i ^ ij[h];
if (j != p) {
dfs(i, j, n);
if (sz[j] * 2 > n)
centroid = 0;
sz[i] += sz[j];
}
}
if ((n - sz[i]) * 2 > n)
centroid = 0;
if (centroid)
c = i;
}
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 ft[W + 1];
void update(int i, int x) {
while (i <= W) {
ft[i] += x;
i |= i + 1;
}
}
int query(int i) {
int x = 0;
while (i >= 0) {
x += ft[i];
i &= i + 1, i--;
}
return x;
}
int ww_[N], ll[N], qu[N], cnt;
void dfs2(int p, int i, int w, int l) {
int o;
ww_[i] = w, ll[i] = l, qu[cnt++] = i;
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ij[h];
if (j != p)
dfs2(i, j, max(w, ww[h]), l + 1);
}
}
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (ww_[ii[j]] == ww_[i_])
j++;
else if (ww_[ii[j]] < ww_[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
long long solve(int *ii, int n) {
int h;
long long ans;
sort(ii, 0, n);
ans = 0;
for (h = 0; h < n; h++) {
ans += query(ww_[ii[h]] - ll[ii[h]] - k);
update(ll[ii[h]], 1);
}
for (h = 0; h < n; h++)
update(ll[ii[h]], -1);
return ans;
}
long long ans;
void cd(int i, int n) {
static int qu_[N];
int o, cnt_;
dfs(-1, i, n), i = c;
cnt_ = 0;
ww_[i] = 0, ll[i] = 0, qu_[cnt_++] = i;
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ij[h];
cnt = 0, dfs2(i, j, ww[h], 1);
ans -= solve(qu, cnt);
while (cnt--)
qu_[cnt_++] = qu[cnt];
}
ans += solve(qu_, cnt_);
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ij[h];
remove_(j, h);
cd(j, sz[j] < sz[i] ? sz[j] : n - sz[i]);
}
}
int main() {
int h, i, j;
scanf("%d%d", &n, &k);
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", &i, &j, &ww[h]), i--, j--;
ij[h] = i ^ j;
append(i, h), append(j, h);
}
cd(0, n);
ans *= 2;
printf("%lld\n", ans);
return 0;
}
Compilation message
Main.c: In function 'append':
Main.c:22:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
22 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Main.c: In function 'main':
Main.c:157:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
157 | scanf("%d%d", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:161:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
161 | scanf("%d%d%d", &i, &j, &ww[h]), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
280 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
416 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
416 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
416 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
436 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
20 ms |
1868 KB |
Output is correct |
6 |
Correct |
117 ms |
8592 KB |
Output is correct |
7 |
Correct |
200 ms |
16604 KB |
Output is correct |
8 |
Correct |
230 ms |
16964 KB |
Output is correct |
9 |
Correct |
202 ms |
16568 KB |
Output is correct |
10 |
Correct |
248 ms |
17016 KB |
Output is correct |
11 |
Correct |
190 ms |
16432 KB |
Output is correct |
12 |
Correct |
226 ms |
16964 KB |
Output is correct |
13 |
Correct |
210 ms |
16580 KB |
Output is correct |
14 |
Correct |
237 ms |
16836 KB |
Output is correct |
15 |
Correct |
243 ms |
16732 KB |
Output is correct |
16 |
Correct |
252 ms |
17016 KB |
Output is correct |
17 |
Correct |
254 ms |
16792 KB |
Output is correct |
18 |
Correct |
251 ms |
16708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
280 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
416 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
416 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
416 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
436 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
0 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
27 |
Correct |
20 ms |
1868 KB |
Output is correct |
28 |
Correct |
117 ms |
8592 KB |
Output is correct |
29 |
Correct |
200 ms |
16604 KB |
Output is correct |
30 |
Correct |
230 ms |
16964 KB |
Output is correct |
31 |
Correct |
202 ms |
16568 KB |
Output is correct |
32 |
Correct |
248 ms |
17016 KB |
Output is correct |
33 |
Correct |
190 ms |
16432 KB |
Output is correct |
34 |
Correct |
226 ms |
16964 KB |
Output is correct |
35 |
Correct |
210 ms |
16580 KB |
Output is correct |
36 |
Correct |
237 ms |
16836 KB |
Output is correct |
37 |
Correct |
243 ms |
16732 KB |
Output is correct |
38 |
Correct |
252 ms |
17016 KB |
Output is correct |
39 |
Correct |
254 ms |
16792 KB |
Output is correct |
40 |
Correct |
251 ms |
16708 KB |
Output is correct |
41 |
Correct |
0 ms |
332 KB |
Output is correct |
42 |
Correct |
195 ms |
16436 KB |
Output is correct |
43 |
Correct |
230 ms |
16980 KB |
Output is correct |
44 |
Correct |
201 ms |
16580 KB |
Output is correct |
45 |
Correct |
246 ms |
16824 KB |
Output is correct |
46 |
Correct |
192 ms |
16492 KB |
Output is correct |
47 |
Correct |
233 ms |
16964 KB |
Output is correct |
48 |
Correct |
207 ms |
16780 KB |
Output is correct |
49 |
Correct |
238 ms |
16884 KB |
Output is correct |
50 |
Correct |
239 ms |
16736 KB |
Output is correct |
51 |
Correct |
255 ms |
16964 KB |
Output is correct |
52 |
Correct |
113 ms |
9020 KB |
Output is correct |
53 |
Correct |
129 ms |
9248 KB |
Output is correct |
54 |
Correct |
105 ms |
8916 KB |
Output is correct |
55 |
Correct |
142 ms |
9436 KB |
Output is correct |
56 |
Correct |
134 ms |
9284 KB |
Output is correct |
57 |
Correct |
235 ms |
8712 KB |
Output is correct |
58 |
Correct |
258 ms |
9028 KB |
Output is correct |
59 |
Correct |
252 ms |
9212 KB |
Output is correct |
60 |
Correct |
280 ms |
9028 KB |
Output is correct |
61 |
Correct |
281 ms |
9028 KB |
Output is correct |
62 |
Correct |
212 ms |
8644 KB |
Output is correct |
63 |
Correct |
234 ms |
9028 KB |
Output is correct |
64 |
Correct |
258 ms |
8976 KB |
Output is correct |
65 |
Correct |
10 ms |
760 KB |
Output is correct |
66 |
Correct |
1 ms |
332 KB |
Output is correct |