#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll LOGN = 18;
const ll MAXN = 1e5 + 100;
vector<vector<pair<int,int>>> graph;
int marked[MAXN], sz[MAXN], fen[MAXN], k;
ll ans = 0;
ll get(int k) {
k++;
if (k <= 0)
return 0;
ll res = 0;
while (k) {
res += fen[k];
k -= k & -k;
}
return res;
}
void update(int k, int v) {
k++;
while (k < MAXN) {
fen[k] += v;
k += k & -k;
}
}
int get_sz(int node, int parent) {
sz[node] = 1;
for (auto u : graph[node]) {
if (u.f == parent || marked[u.f])
continue;
sz[node] += get_sz(u.f, node);
}
return sz[node];
}
int find_centro(int node, int parent, int n) {
for (auto u : graph[node]) {
if (u.f != parent && !marked[u.f] && sz[u.f] * 2 >= n)
return find_centro(u.f, node, n);
}
return node;
}
vector<pair<int,int>> v;
void dfs(int node, int parent, int mx, int len) {
v.push_back({mx, len});
for (auto u : graph[node]) {
if (u.f != parent && !marked[u.f])
dfs(u.f, node, max(mx, u.s), len+1);
}
}
ll calc(int node, int parent, int mx, int len) {
v.clear();
dfs(node, parent, mx, len);
sort(v.begin(), v.end());
ll ret = 0;
for (auto u : v) {
int val = u.f - u.s;
ret += get(val - k);
update(u.s, 1);
}
for (auto u : v)
update(u.s, -1);
return ret;
}
void decompose(int node, int parent) {
int n = get_sz(node, parent);
int centro = find_centro(node, parent, n);
ans += calc(centro, centro, 0, 0);
marked[centro] = 1;
for (auto u : graph[centro]) {
if (!marked[u.f])
ans -= calc(u.f, u.f, u.s, 1);
}
for (auto u : graph[centro]) {
if (!marked[u.f])
decompose(u.f, node);
}
}
int main() {
fast
int n, x, y, w;
cin >> n >> k;
graph = vector<vector<pair<int,int>>>(n+1, vector<pair<int,int>>());
for (int i = 1; i < n; i++) {
cin >> x >> y >> w;
graph[x].push_back({y, w});
graph[y].push_back({x, w});
}
decompose(1, 1);
cout << ans*2 << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
472 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
472 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
520 KB |
Output is correct |
17 |
Correct |
1 ms |
360 KB |
Output is correct |
18 |
Correct |
1 ms |
352 KB |
Output is correct |
19 |
Correct |
2 ms |
352 KB |
Output is correct |
20 |
Correct |
1 ms |
352 KB |
Output is correct |
21 |
Correct |
2 ms |
352 KB |
Output is correct |
22 |
Correct |
1 ms |
352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
352 KB |
Output is correct |
4 |
Correct |
2 ms |
608 KB |
Output is correct |
5 |
Correct |
19 ms |
2228 KB |
Output is correct |
6 |
Correct |
125 ms |
8148 KB |
Output is correct |
7 |
Correct |
232 ms |
15332 KB |
Output is correct |
8 |
Correct |
284 ms |
15880 KB |
Output is correct |
9 |
Correct |
249 ms |
15300 KB |
Output is correct |
10 |
Correct |
279 ms |
15752 KB |
Output is correct |
11 |
Correct |
240 ms |
15312 KB |
Output is correct |
12 |
Correct |
273 ms |
15808 KB |
Output is correct |
13 |
Correct |
252 ms |
16020 KB |
Output is correct |
14 |
Correct |
282 ms |
15620 KB |
Output is correct |
15 |
Correct |
284 ms |
15636 KB |
Output is correct |
16 |
Correct |
267 ms |
15816 KB |
Output is correct |
17 |
Correct |
269 ms |
15764 KB |
Output is correct |
18 |
Correct |
291 ms |
15668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
472 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
472 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
520 KB |
Output is correct |
17 |
Correct |
1 ms |
360 KB |
Output is correct |
18 |
Correct |
1 ms |
352 KB |
Output is correct |
19 |
Correct |
2 ms |
352 KB |
Output is correct |
20 |
Correct |
1 ms |
352 KB |
Output is correct |
21 |
Correct |
2 ms |
352 KB |
Output is correct |
22 |
Correct |
1 ms |
352 KB |
Output is correct |
23 |
Correct |
1 ms |
352 KB |
Output is correct |
24 |
Correct |
0 ms |
464 KB |
Output is correct |
25 |
Correct |
0 ms |
352 KB |
Output is correct |
26 |
Correct |
2 ms |
608 KB |
Output is correct |
27 |
Correct |
19 ms |
2228 KB |
Output is correct |
28 |
Correct |
125 ms |
8148 KB |
Output is correct |
29 |
Correct |
232 ms |
15332 KB |
Output is correct |
30 |
Correct |
284 ms |
15880 KB |
Output is correct |
31 |
Correct |
249 ms |
15300 KB |
Output is correct |
32 |
Correct |
279 ms |
15752 KB |
Output is correct |
33 |
Correct |
240 ms |
15312 KB |
Output is correct |
34 |
Correct |
273 ms |
15808 KB |
Output is correct |
35 |
Correct |
252 ms |
16020 KB |
Output is correct |
36 |
Correct |
282 ms |
15620 KB |
Output is correct |
37 |
Correct |
284 ms |
15636 KB |
Output is correct |
38 |
Correct |
267 ms |
15816 KB |
Output is correct |
39 |
Correct |
269 ms |
15764 KB |
Output is correct |
40 |
Correct |
291 ms |
15668 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
236 ms |
15356 KB |
Output is correct |
43 |
Correct |
266 ms |
15812 KB |
Output is correct |
44 |
Correct |
246 ms |
15340 KB |
Output is correct |
45 |
Correct |
278 ms |
15812 KB |
Output is correct |
46 |
Correct |
234 ms |
15316 KB |
Output is correct |
47 |
Correct |
268 ms |
15812 KB |
Output is correct |
48 |
Correct |
251 ms |
15968 KB |
Output is correct |
49 |
Correct |
279 ms |
15768 KB |
Output is correct |
50 |
Correct |
283 ms |
15436 KB |
Output is correct |
51 |
Correct |
279 ms |
15636 KB |
Output is correct |
52 |
Correct |
109 ms |
9924 KB |
Output is correct |
53 |
Correct |
108 ms |
10184 KB |
Output is correct |
54 |
Correct |
86 ms |
9940 KB |
Output is correct |
55 |
Correct |
103 ms |
10196 KB |
Output is correct |
56 |
Correct |
104 ms |
10368 KB |
Output is correct |
57 |
Correct |
212 ms |
9680 KB |
Output is correct |
58 |
Correct |
237 ms |
9932 KB |
Output is correct |
59 |
Correct |
223 ms |
9884 KB |
Output is correct |
60 |
Correct |
247 ms |
9932 KB |
Output is correct |
61 |
Correct |
242 ms |
9936 KB |
Output is correct |
62 |
Correct |
180 ms |
9648 KB |
Output is correct |
63 |
Correct |
207 ms |
9928 KB |
Output is correct |
64 |
Correct |
229 ms |
9928 KB |
Output is correct |
65 |
Correct |
9 ms |
860 KB |
Output is correct |
66 |
Correct |
0 ms |
468 KB |
Output is correct |