#include <bits/stdc++.h>
using namespace std;
#define task "HOTPOT"
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int MAX = 100010;
int sz[MAX];
int nNode, k;
int bit[MAX];
bool rem[MAX];
long long res = 0;
vector<pair<int, int>> adj[MAX];
void update(int id, int delta) {
for (++id; id < MAX; id += id & -id)
bit[id] += delta;
}
int get(int id) {
int res = 0;
id = min(id, MAX - 1);
for (++id; id > 0; id -= id & -id)
res += bit[id];
return res;
}
int dfsSZ(int u, int p) {
sz[u] = 1;
for (auto [v, w]: adj[u]) if (v != p && !rem[v]) {
sz[u] += dfsSZ(v, u);
}
return sz[u];
}
int findCentroid(int u, int p, int n) {
for (auto [v, w]: adj[u]) if (v != p && !rem[v]) {
if ((sz[v] << 1) > n)
return findCentroid(v, u, n);
}
return u;
}
vector<pair<int, int>> nodes;
void dfs(int u, int p, int mxx, int dep) {
nodes.emplace_back(mxx, dep);
for (auto [v, w]: adj[u]) if (v != p && !rem[v])
dfs(v, u, max(mxx, w), dep + 1);
}
void solve(int u, int p) {
int c = findCentroid(u, p, dfsSZ(u, p)); rem[c] = true;
nodes.clear();
dfs(c, p, 0, 0);
sort(all(nodes));
for (auto [mxx, dep]: nodes) {
int maxL = mxx - k - dep;
res += get(maxL);
update(dep, +1);
}
for (auto [mxx, dep]: nodes) {
update(dep, -1);
}
for (auto [v, w]: adj[c]) if (v != p && !rem[v]) {
nodes.clear();
dfs(v, p, w, 1);
sort(all(nodes));
for (auto [mxx, dep]: nodes) {
int maxL = mxx - k - dep;
res -= get(maxL);
update(dep, +1);
}
for (auto [mxx, dep]: nodes) {
update(dep, -1);
}
}
for (auto [v, w]: adj[c]) if (v != p && !rem[v]) {
solve(v, c);
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> nNode >> k;
for (int i = 1; i < nNode; ++i) {
int u, v, w; cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
solve(1, -1);
cout << res * 2;
return 0;
}
Compilation message
Main.cpp: In function 'int dfsSZ(int, int)':
Main.cpp:32:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
32 | for (auto [v, w]: adj[u]) if (v != p && !rem[v]) {
| ^
Main.cpp: In function 'int findCentroid(int, int, int)':
Main.cpp:39:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
39 | for (auto [v, w]: adj[u]) if (v != p && !rem[v]) {
| ^
Main.cpp: In function 'void dfs(int, int, int, int)':
Main.cpp:50:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
50 | for (auto [v, w]: adj[u]) if (v != p && !rem[v])
| ^
Main.cpp: In function 'void solve(int, int)':
Main.cpp:60:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
60 | for (auto [mxx, dep]: nodes) {
| ^
Main.cpp:65:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
65 | for (auto [mxx, dep]: nodes) {
| ^
Main.cpp:69:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
69 | for (auto [v, w]: adj[c]) if (v != p && !rem[v]) {
| ^
Main.cpp:73:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
73 | for (auto [mxx, dep]: nodes) {
| ^
Main.cpp:78:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
78 | for (auto [mxx, dep]: nodes) {
| ^
Main.cpp:83:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
83 | for (auto [v, w]: adj[c]) if (v != p && !rem[v]) {
| ^
Main.cpp: In function 'int main()':
Main.cpp:92:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:93:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2908 KB |
Output is correct |
5 |
Correct |
2 ms |
2908 KB |
Output is correct |
6 |
Correct |
2 ms |
2904 KB |
Output is correct |
7 |
Correct |
2 ms |
2908 KB |
Output is correct |
8 |
Correct |
2 ms |
2904 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
2908 KB |
Output is correct |
12 |
Correct |
2 ms |
2908 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2908 KB |
Output is correct |
15 |
Correct |
2 ms |
2908 KB |
Output is correct |
16 |
Correct |
2 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
2904 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
2 ms |
2816 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
3 ms |
2824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2812 KB |
Output is correct |
4 |
Correct |
2 ms |
2908 KB |
Output is correct |
5 |
Correct |
18 ms |
4196 KB |
Output is correct |
6 |
Correct |
123 ms |
9140 KB |
Output is correct |
7 |
Correct |
219 ms |
14784 KB |
Output is correct |
8 |
Correct |
246 ms |
15056 KB |
Output is correct |
9 |
Correct |
232 ms |
14796 KB |
Output is correct |
10 |
Correct |
277 ms |
14820 KB |
Output is correct |
11 |
Correct |
220 ms |
14788 KB |
Output is correct |
12 |
Correct |
247 ms |
15036 KB |
Output is correct |
13 |
Correct |
237 ms |
15048 KB |
Output is correct |
14 |
Correct |
267 ms |
14788 KB |
Output is correct |
15 |
Correct |
270 ms |
14792 KB |
Output is correct |
16 |
Correct |
260 ms |
15308 KB |
Output is correct |
17 |
Correct |
272 ms |
15508 KB |
Output is correct |
18 |
Correct |
283 ms |
15304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2908 KB |
Output is correct |
5 |
Correct |
2 ms |
2908 KB |
Output is correct |
6 |
Correct |
2 ms |
2904 KB |
Output is correct |
7 |
Correct |
2 ms |
2908 KB |
Output is correct |
8 |
Correct |
2 ms |
2904 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
2908 KB |
Output is correct |
12 |
Correct |
2 ms |
2908 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2908 KB |
Output is correct |
15 |
Correct |
2 ms |
2908 KB |
Output is correct |
16 |
Correct |
2 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
2904 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
2 ms |
2816 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
3 ms |
2824 KB |
Output is correct |
23 |
Correct |
1 ms |
2648 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
2812 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
18 ms |
4196 KB |
Output is correct |
28 |
Correct |
123 ms |
9140 KB |
Output is correct |
29 |
Correct |
219 ms |
14784 KB |
Output is correct |
30 |
Correct |
246 ms |
15056 KB |
Output is correct |
31 |
Correct |
232 ms |
14796 KB |
Output is correct |
32 |
Correct |
277 ms |
14820 KB |
Output is correct |
33 |
Correct |
220 ms |
14788 KB |
Output is correct |
34 |
Correct |
247 ms |
15036 KB |
Output is correct |
35 |
Correct |
237 ms |
15048 KB |
Output is correct |
36 |
Correct |
267 ms |
14788 KB |
Output is correct |
37 |
Correct |
270 ms |
14792 KB |
Output is correct |
38 |
Correct |
260 ms |
15308 KB |
Output is correct |
39 |
Correct |
272 ms |
15508 KB |
Output is correct |
40 |
Correct |
283 ms |
15304 KB |
Output is correct |
41 |
Correct |
1 ms |
2652 KB |
Output is correct |
42 |
Correct |
223 ms |
15036 KB |
Output is correct |
43 |
Correct |
254 ms |
15564 KB |
Output is correct |
44 |
Correct |
254 ms |
15160 KB |
Output is correct |
45 |
Correct |
276 ms |
15464 KB |
Output is correct |
46 |
Correct |
275 ms |
15084 KB |
Output is correct |
47 |
Correct |
269 ms |
15428 KB |
Output is correct |
48 |
Correct |
238 ms |
15236 KB |
Output is correct |
49 |
Correct |
275 ms |
15308 KB |
Output is correct |
50 |
Correct |
270 ms |
15348 KB |
Output is correct |
51 |
Correct |
264 ms |
15304 KB |
Output is correct |
52 |
Correct |
91 ms |
9940 KB |
Output is correct |
53 |
Correct |
95 ms |
10184 KB |
Output is correct |
54 |
Correct |
71 ms |
9684 KB |
Output is correct |
55 |
Correct |
105 ms |
10188 KB |
Output is correct |
56 |
Correct |
100 ms |
10188 KB |
Output is correct |
57 |
Correct |
209 ms |
9424 KB |
Output is correct |
58 |
Correct |
224 ms |
9676 KB |
Output is correct |
59 |
Correct |
207 ms |
9936 KB |
Output is correct |
60 |
Correct |
235 ms |
9684 KB |
Output is correct |
61 |
Correct |
234 ms |
9788 KB |
Output is correct |
62 |
Correct |
190 ms |
9568 KB |
Output is correct |
63 |
Correct |
195 ms |
9676 KB |
Output is correct |
64 |
Correct |
220 ms |
9676 KB |
Output is correct |
65 |
Correct |
8 ms |
3160 KB |
Output is correct |
66 |
Correct |
1 ms |
2648 KB |
Output is correct |