#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define int long long
using ar = array<int, 2>;
const int64_t INF = 1ll << 60;
const int N = 1e5 + 5;
vector<bool> del(N);
int sub[N], ptr, M[N], h[N], k, n, bit[N], ans;
vector<ar> adj[N], VV, V[N];
void add(int r, int d) {
r++;
for (; r < N; r += r & -r) bit[r] += d;
}
int get(int r) {
r++;
int re = 0;
for (; r > 0; r &= (r - 1)) re += bit[r];
return re;
}
void find_sub(int v, int p) {
sub[v] = 1;
for (auto P: adj[v]) {
int u = P[0];
if (del[u] || u == p) continue;
find_sub(u, v);
sub[v] += sub[u];
}
}
int find_center(int v, int p, int kol) {
for (auto P: adj[v]) {
int u = P[0];
if (del[u] || u == p || sub[u] <= (kol / 2)) continue;
return find_center(u, v, kol);
}
return v;
}
void dfs(int v, int p) {
for (auto P: adj[v]) {
int u = P[0], w = P[1];
if (u == p || del[u]) continue;
M[u] = max(M[v], w);
h[u] = h[v] + 1;
dfs(u, v);
}
V[ptr].push_back({M[v], h[v]});
VV.push_back({M[v], h[v]});
}
int solve(vector<ar> &a) {
sort(a.begin(), a.end());
int ans = 0;
for (auto P: a) {
int m = P[0], h = P[1];
ans += get(m - h - k);
add(h, +1);
}
for (auto P: a)
add(P[1], -1);
return ans;
}
void rec(int x) {
find_sub(x, x);
int c = find_center(x, x, sub[x]);
del[c] = true;
for (auto P: adj[c]) {
int u = P[0], w = P[1];
if (del[u]) continue;
h[u] = 1, M[u] = w;
dfs(u, c);
ptr++;
}
FOR(i, 0, ptr) {
ans -= solve(V[i]);
V[i].clear();
}
VV.push_back({-INF, 0});
ans += solve(VV);
VV.clear();
ptr = 0;
for (auto P: adj[c]) {
int u = P[0], w = P[1];
if (del[u]) continue;
rec(u);
}
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
FOR(i, 1, n) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
adj[v].push_back({u, w});
adj[u].push_back({v, w});
}
rec(0);
cout << 2ll * ans << '\n';
}
//20:55:05
Compilation message
Main.cpp: In function 'void rec(long long int)':
Main.cpp:86:31: warning: unused variable 'w' [-Wunused-variable]
86 | int u = P[0], w = P[1];
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
4 ms |
5028 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
4 ms |
5204 KB |
Output is correct |
5 |
Correct |
6 ms |
5180 KB |
Output is correct |
6 |
Correct |
5 ms |
5204 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5176 KB |
Output is correct |
11 |
Correct |
4 ms |
5204 KB |
Output is correct |
12 |
Correct |
5 ms |
5204 KB |
Output is correct |
13 |
Correct |
4 ms |
5204 KB |
Output is correct |
14 |
Correct |
4 ms |
5176 KB |
Output is correct |
15 |
Correct |
4 ms |
5176 KB |
Output is correct |
16 |
Correct |
4 ms |
5176 KB |
Output is correct |
17 |
Correct |
4 ms |
5204 KB |
Output is correct |
18 |
Correct |
4 ms |
5204 KB |
Output is correct |
19 |
Correct |
5 ms |
5176 KB |
Output is correct |
20 |
Correct |
4 ms |
5204 KB |
Output is correct |
21 |
Correct |
4 ms |
5140 KB |
Output is correct |
22 |
Correct |
4 ms |
5176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
4988 KB |
Output is correct |
4 |
Correct |
6 ms |
5176 KB |
Output is correct |
5 |
Correct |
23 ms |
7088 KB |
Output is correct |
6 |
Correct |
155 ms |
14852 KB |
Output is correct |
7 |
Correct |
305 ms |
24260 KB |
Output is correct |
8 |
Correct |
356 ms |
24756 KB |
Output is correct |
9 |
Correct |
290 ms |
24344 KB |
Output is correct |
10 |
Correct |
349 ms |
24648 KB |
Output is correct |
11 |
Correct |
297 ms |
24256 KB |
Output is correct |
12 |
Correct |
328 ms |
24756 KB |
Output is correct |
13 |
Correct |
292 ms |
24352 KB |
Output is correct |
14 |
Correct |
328 ms |
24648 KB |
Output is correct |
15 |
Correct |
344 ms |
24500 KB |
Output is correct |
16 |
Correct |
304 ms |
24636 KB |
Output is correct |
17 |
Correct |
308 ms |
24520 KB |
Output is correct |
18 |
Correct |
301 ms |
24640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
4 ms |
5028 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
4 ms |
5204 KB |
Output is correct |
5 |
Correct |
6 ms |
5180 KB |
Output is correct |
6 |
Correct |
5 ms |
5204 KB |
Output is correct |
7 |
Correct |
4 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5176 KB |
Output is correct |
11 |
Correct |
4 ms |
5204 KB |
Output is correct |
12 |
Correct |
5 ms |
5204 KB |
Output is correct |
13 |
Correct |
4 ms |
5204 KB |
Output is correct |
14 |
Correct |
4 ms |
5176 KB |
Output is correct |
15 |
Correct |
4 ms |
5176 KB |
Output is correct |
16 |
Correct |
4 ms |
5176 KB |
Output is correct |
17 |
Correct |
4 ms |
5204 KB |
Output is correct |
18 |
Correct |
4 ms |
5204 KB |
Output is correct |
19 |
Correct |
5 ms |
5176 KB |
Output is correct |
20 |
Correct |
4 ms |
5204 KB |
Output is correct |
21 |
Correct |
4 ms |
5140 KB |
Output is correct |
22 |
Correct |
4 ms |
5176 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
2 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
4988 KB |
Output is correct |
26 |
Correct |
6 ms |
5176 KB |
Output is correct |
27 |
Correct |
23 ms |
7088 KB |
Output is correct |
28 |
Correct |
155 ms |
14852 KB |
Output is correct |
29 |
Correct |
305 ms |
24260 KB |
Output is correct |
30 |
Correct |
356 ms |
24756 KB |
Output is correct |
31 |
Correct |
290 ms |
24344 KB |
Output is correct |
32 |
Correct |
349 ms |
24648 KB |
Output is correct |
33 |
Correct |
297 ms |
24256 KB |
Output is correct |
34 |
Correct |
328 ms |
24756 KB |
Output is correct |
35 |
Correct |
292 ms |
24352 KB |
Output is correct |
36 |
Correct |
328 ms |
24648 KB |
Output is correct |
37 |
Correct |
344 ms |
24500 KB |
Output is correct |
38 |
Correct |
304 ms |
24636 KB |
Output is correct |
39 |
Correct |
308 ms |
24520 KB |
Output is correct |
40 |
Correct |
301 ms |
24640 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
295 ms |
24260 KB |
Output is correct |
43 |
Correct |
351 ms |
24752 KB |
Output is correct |
44 |
Correct |
289 ms |
24228 KB |
Output is correct |
45 |
Correct |
340 ms |
24600 KB |
Output is correct |
46 |
Correct |
292 ms |
24184 KB |
Output is correct |
47 |
Correct |
336 ms |
24764 KB |
Output is correct |
48 |
Correct |
294 ms |
24352 KB |
Output is correct |
49 |
Correct |
341 ms |
24648 KB |
Output is correct |
50 |
Correct |
329 ms |
24508 KB |
Output is correct |
51 |
Correct |
308 ms |
24652 KB |
Output is correct |
52 |
Correct |
108 ms |
17444 KB |
Output is correct |
53 |
Correct |
126 ms |
17716 KB |
Output is correct |
54 |
Correct |
101 ms |
17344 KB |
Output is correct |
55 |
Correct |
128 ms |
17752 KB |
Output is correct |
56 |
Correct |
125 ms |
17684 KB |
Output is correct |
57 |
Correct |
249 ms |
18720 KB |
Output is correct |
58 |
Correct |
263 ms |
18456 KB |
Output is correct |
59 |
Correct |
321 ms |
18904 KB |
Output is correct |
60 |
Correct |
288 ms |
18328 KB |
Output is correct |
61 |
Correct |
331 ms |
18812 KB |
Output is correct |
62 |
Correct |
214 ms |
17592 KB |
Output is correct |
63 |
Correct |
295 ms |
18532 KB |
Output is correct |
64 |
Correct |
245 ms |
18344 KB |
Output is correct |
65 |
Correct |
11 ms |
5692 KB |
Output is correct |
66 |
Correct |
2 ms |
5040 KB |
Output is correct |