#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAX_N = 1e5 + 5;
int sz[MAX_N], dpt[MAX_N], mx[MAX_N];
vector<pair<int, int>> adj[MAX_N];
bool rem[MAX_N];
int n, k;
int init_dfs(int x, int p) {
sz[x] = 1;
for (const auto &elem: adj[x]) {
int y; tie(y, ignore) = elem;
if (y == p || rem[y]) {
continue;
}
sz[x] += init_dfs(y, x);
}
return sz[x];
}
int centroid(int x, int p, int n) {
for (const auto &elem: adj[x]) {
int y; tie(y, ignore) = elem;
if (y == p || rem[y]) continue;
if (sz[y] * 2 > n) {
return centroid(y, x, n);
}
}
return x;
}
vector<pair<int, int>> arr;
ll ans = 0;
void init(int x, int p) {
if (mx[x]) arr.emplace_back(mx[x], dpt[x]);
if (mx[x] - dpt[x] >= k) ++ans;
for (const auto &elem: adj[x]) {
int y, w; tie(y, w) = elem;
if (y == p || rem[y]) continue;
dpt[y] = dpt[x] + 1;
mx[y] = max(mx[x], w);
init(y, x);
}
}
const int H = 1e5;
int bit[H + 5];
void update(int x, int w) {
for (; x <= H; x += x & -x) {
bit[x] += w;
}
}
int get(int x) {
int ans = 0;
for (; x > 0; x -= x & -x) {
ans += bit[x];
}
return ans;
}
ll calc() {
sort(arr.begin(), arr.end());
ll ans = 0;
for (const auto &elem: arr) {
int val, h; tie(val, h) = elem;
int to_find = val - h - k;
// val - h - to_find >= k
// => to_find + h - val <= -k
// => to_find <= val - h - k
ans += get(to_find);
update(h, +1);
}
for (const auto &elem: arr)
update(elem.second, -1);
return ans;
}
void sub(int x, int p) {
arr.emplace_back(mx[x], dpt[x]);
for (const auto &elem: adj[x]) {
int y; tie(y, ignore) = elem;
if (y == p || rem[y]) continue;
sub(y, x);
}
}
void solve(int c) {
dpt[c] = mx[c] = 0;
vector<pair<int, int>>().swap(arr);
init(c, 0);
ans += calc();
for (const auto &elem: adj[c]) {
int y; tie(y, ignore) = elem;
if (rem[y]) continue;
vector<pair<int, int>>().swap(arr);
sub(y, c);
ans -= calc();
}
}
void build(int x) {
int n = init_dfs(x, 0);
int c = centroid(x, 0, n);
rem[c] = true;
solve(c);
for (const auto &elem: adj[c]) {
int y; tie(y, ignore) = elem;
if (rem[y]) continue;
build(y);
}
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i < n; i++) {
int x, y, w;
cin >> x >> y >> w;
adj[x].emplace_back(y, w);
adj[y].emplace_back(x, w);
}
build(1);
cout << ans * 2;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
2904 KB |
Output is correct |
4 |
Correct |
2 ms |
3164 KB |
Output is correct |
5 |
Correct |
2 ms |
3164 KB |
Output is correct |
6 |
Correct |
2 ms |
3164 KB |
Output is correct |
7 |
Correct |
2 ms |
3164 KB |
Output is correct |
8 |
Correct |
2 ms |
3164 KB |
Output is correct |
9 |
Correct |
2 ms |
3164 KB |
Output is correct |
10 |
Correct |
2 ms |
3164 KB |
Output is correct |
11 |
Correct |
2 ms |
3164 KB |
Output is correct |
12 |
Correct |
2 ms |
3164 KB |
Output is correct |
13 |
Correct |
2 ms |
3164 KB |
Output is correct |
14 |
Correct |
2 ms |
3160 KB |
Output is correct |
15 |
Correct |
2 ms |
3164 KB |
Output is correct |
16 |
Correct |
2 ms |
3164 KB |
Output is correct |
17 |
Correct |
2 ms |
3164 KB |
Output is correct |
18 |
Correct |
2 ms |
3164 KB |
Output is correct |
19 |
Correct |
2 ms |
3164 KB |
Output is correct |
20 |
Correct |
2 ms |
3160 KB |
Output is correct |
21 |
Correct |
2 ms |
3164 KB |
Output is correct |
22 |
Correct |
2 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2904 KB |
Output is correct |
2 |
Correct |
1 ms |
2904 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Correct |
3 ms |
3160 KB |
Output is correct |
5 |
Correct |
18 ms |
4444 KB |
Output is correct |
6 |
Correct |
125 ms |
9808 KB |
Output is correct |
7 |
Correct |
224 ms |
16072 KB |
Output is correct |
8 |
Correct |
257 ms |
16580 KB |
Output is correct |
9 |
Correct |
234 ms |
16176 KB |
Output is correct |
10 |
Correct |
279 ms |
16484 KB |
Output is correct |
11 |
Correct |
222 ms |
15948 KB |
Output is correct |
12 |
Correct |
271 ms |
16596 KB |
Output is correct |
13 |
Correct |
241 ms |
16184 KB |
Output is correct |
14 |
Correct |
275 ms |
16544 KB |
Output is correct |
15 |
Correct |
303 ms |
16324 KB |
Output is correct |
16 |
Correct |
268 ms |
16468 KB |
Output is correct |
17 |
Correct |
283 ms |
16468 KB |
Output is correct |
18 |
Correct |
277 ms |
16420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
2904 KB |
Output is correct |
4 |
Correct |
2 ms |
3164 KB |
Output is correct |
5 |
Correct |
2 ms |
3164 KB |
Output is correct |
6 |
Correct |
2 ms |
3164 KB |
Output is correct |
7 |
Correct |
2 ms |
3164 KB |
Output is correct |
8 |
Correct |
2 ms |
3164 KB |
Output is correct |
9 |
Correct |
2 ms |
3164 KB |
Output is correct |
10 |
Correct |
2 ms |
3164 KB |
Output is correct |
11 |
Correct |
2 ms |
3164 KB |
Output is correct |
12 |
Correct |
2 ms |
3164 KB |
Output is correct |
13 |
Correct |
2 ms |
3164 KB |
Output is correct |
14 |
Correct |
2 ms |
3160 KB |
Output is correct |
15 |
Correct |
2 ms |
3164 KB |
Output is correct |
16 |
Correct |
2 ms |
3164 KB |
Output is correct |
17 |
Correct |
2 ms |
3164 KB |
Output is correct |
18 |
Correct |
2 ms |
3164 KB |
Output is correct |
19 |
Correct |
2 ms |
3164 KB |
Output is correct |
20 |
Correct |
2 ms |
3160 KB |
Output is correct |
21 |
Correct |
2 ms |
3164 KB |
Output is correct |
22 |
Correct |
2 ms |
3164 KB |
Output is correct |
23 |
Correct |
1 ms |
2904 KB |
Output is correct |
24 |
Correct |
1 ms |
2904 KB |
Output is correct |
25 |
Correct |
1 ms |
2908 KB |
Output is correct |
26 |
Correct |
3 ms |
3160 KB |
Output is correct |
27 |
Correct |
18 ms |
4444 KB |
Output is correct |
28 |
Correct |
125 ms |
9808 KB |
Output is correct |
29 |
Correct |
224 ms |
16072 KB |
Output is correct |
30 |
Correct |
257 ms |
16580 KB |
Output is correct |
31 |
Correct |
234 ms |
16176 KB |
Output is correct |
32 |
Correct |
279 ms |
16484 KB |
Output is correct |
33 |
Correct |
222 ms |
15948 KB |
Output is correct |
34 |
Correct |
271 ms |
16596 KB |
Output is correct |
35 |
Correct |
241 ms |
16184 KB |
Output is correct |
36 |
Correct |
275 ms |
16544 KB |
Output is correct |
37 |
Correct |
303 ms |
16324 KB |
Output is correct |
38 |
Correct |
268 ms |
16468 KB |
Output is correct |
39 |
Correct |
283 ms |
16468 KB |
Output is correct |
40 |
Correct |
277 ms |
16420 KB |
Output is correct |
41 |
Correct |
1 ms |
2908 KB |
Output is correct |
42 |
Correct |
223 ms |
16100 KB |
Output is correct |
43 |
Correct |
265 ms |
16680 KB |
Output is correct |
44 |
Correct |
237 ms |
16180 KB |
Output is correct |
45 |
Correct |
270 ms |
16328 KB |
Output is correct |
46 |
Correct |
229 ms |
16092 KB |
Output is correct |
47 |
Correct |
292 ms |
16580 KB |
Output is correct |
48 |
Correct |
233 ms |
16080 KB |
Output is correct |
49 |
Correct |
272 ms |
16344 KB |
Output is correct |
50 |
Correct |
293 ms |
16332 KB |
Output is correct |
51 |
Correct |
280 ms |
16552 KB |
Output is correct |
52 |
Correct |
84 ms |
10952 KB |
Output is correct |
53 |
Correct |
99 ms |
11232 KB |
Output is correct |
54 |
Correct |
71 ms |
10696 KB |
Output is correct |
55 |
Correct |
114 ms |
11212 KB |
Output is correct |
56 |
Correct |
107 ms |
11476 KB |
Output is correct |
57 |
Correct |
214 ms |
10448 KB |
Output is correct |
58 |
Correct |
249 ms |
10704 KB |
Output is correct |
59 |
Correct |
216 ms |
10932 KB |
Output is correct |
60 |
Correct |
264 ms |
10904 KB |
Output is correct |
61 |
Correct |
258 ms |
10716 KB |
Output is correct |
62 |
Correct |
181 ms |
10440 KB |
Output is correct |
63 |
Correct |
222 ms |
10696 KB |
Output is correct |
64 |
Correct |
244 ms |
10700 KB |
Output is correct |
65 |
Correct |
9 ms |
3416 KB |
Output is correct |
66 |
Correct |
1 ms |
2908 KB |
Output is correct |