// In the name of the God
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 2e5+5;
int n, k, sz[maxn], fen[maxn];
bool mark[maxn];
vector <pii> adj[maxn], vec1, vec2;
void dfs1(int v, int p) {
sz[v] = 1;
for (pii e : adj[v]) {
int u = e.F;
if (!mark[u] and u != p) {
dfs1(u, v); sz[v] += sz[u];
}
}
}
int fcen(int v, int p, int s) {
for (pii e : adj[v]) {
int u = e.F;
if (!mark[u] and u != p and sz[u]*2 > s) return fcen(u, v, s);
}
return v;
}
void dfs2(int v, int p, int mx, int h) {
vec1.pb(mp(mx, h)), vec2.pb(mp(mx, h));
for (pii e : adj[v]) {
int u = e.F;
if (!mark[u] and u != p) dfs2(u, v, max(mx, e.S), h+1);
}
}
void upd(int i, int x) {
for (i += 1; i <= n; i += i&(-i)) fen[i] += x;
}
int get(int i) {
int ans = 0;
for (i += 1; i; i -= i&(-i)) ans += fen[i] ;
return ans;
}
int relax(vector <pii> &vec) {
int res = 0;
sort(all(vec));
for (pii p : vec) {
int j = p.F-k-p.S;
if (j >= 0) res += get(j);
upd(p.S, 1);
}
for (pii p : vec) upd(p.S, -1);
vec.clear();
return res;
}
int cen(int v) {
dfs1(v, v);
v = fcen(v, v, sz[v]);
int ans = 0;
vec2.pb(mp(0, 0));
for (pii e : adj[v]) {
int u = e.F;
if (!mark[u]) {
dfs2(u, v, e.S, 1);
ans -= relax(vec1);
}
}
ans += relax(vec2);
mark[v] = 1;
for (pii e : adj[v]) {
int u = e.F;
if (!mark[u]) ans += cen(u);
}
return ans;
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i < n; i++) {
int v, u, w; cin >> v >> u >> w;
adj[--v].pb(mp(--u, w));
adj[u].pb(mp(v, w));
}
cout << cen(0)*2 << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6744 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6840 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6744 KB |
Output is correct |
2 |
Correct |
2 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
3 ms |
6744 KB |
Output is correct |
5 |
Correct |
16 ms |
8280 KB |
Output is correct |
6 |
Correct |
120 ms |
14276 KB |
Output is correct |
7 |
Correct |
220 ms |
21360 KB |
Output is correct |
8 |
Correct |
310 ms |
21424 KB |
Output is correct |
9 |
Correct |
266 ms |
21360 KB |
Output is correct |
10 |
Correct |
314 ms |
21356 KB |
Output is correct |
11 |
Correct |
233 ms |
21372 KB |
Output is correct |
12 |
Correct |
304 ms |
21396 KB |
Output is correct |
13 |
Correct |
233 ms |
21368 KB |
Output is correct |
14 |
Correct |
295 ms |
21316 KB |
Output is correct |
15 |
Correct |
341 ms |
21356 KB |
Output is correct |
16 |
Correct |
301 ms |
21308 KB |
Output is correct |
17 |
Correct |
318 ms |
21356 KB |
Output is correct |
18 |
Correct |
330 ms |
21360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6744 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
2 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6748 KB |
Output is correct |
7 |
Correct |
2 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
2 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
2 ms |
6748 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
2 ms |
6748 KB |
Output is correct |
16 |
Correct |
2 ms |
6748 KB |
Output is correct |
17 |
Correct |
2 ms |
6748 KB |
Output is correct |
18 |
Correct |
2 ms |
6748 KB |
Output is correct |
19 |
Correct |
2 ms |
6748 KB |
Output is correct |
20 |
Correct |
2 ms |
6840 KB |
Output is correct |
21 |
Correct |
2 ms |
6748 KB |
Output is correct |
22 |
Correct |
2 ms |
6748 KB |
Output is correct |
23 |
Correct |
2 ms |
6744 KB |
Output is correct |
24 |
Correct |
2 ms |
6748 KB |
Output is correct |
25 |
Correct |
1 ms |
6748 KB |
Output is correct |
26 |
Correct |
3 ms |
6744 KB |
Output is correct |
27 |
Correct |
16 ms |
8280 KB |
Output is correct |
28 |
Correct |
120 ms |
14276 KB |
Output is correct |
29 |
Correct |
220 ms |
21360 KB |
Output is correct |
30 |
Correct |
310 ms |
21424 KB |
Output is correct |
31 |
Correct |
266 ms |
21360 KB |
Output is correct |
32 |
Correct |
314 ms |
21356 KB |
Output is correct |
33 |
Correct |
233 ms |
21372 KB |
Output is correct |
34 |
Correct |
304 ms |
21396 KB |
Output is correct |
35 |
Correct |
233 ms |
21368 KB |
Output is correct |
36 |
Correct |
295 ms |
21316 KB |
Output is correct |
37 |
Correct |
341 ms |
21356 KB |
Output is correct |
38 |
Correct |
301 ms |
21308 KB |
Output is correct |
39 |
Correct |
318 ms |
21356 KB |
Output is correct |
40 |
Correct |
330 ms |
21360 KB |
Output is correct |
41 |
Correct |
1 ms |
6744 KB |
Output is correct |
42 |
Correct |
225 ms |
21372 KB |
Output is correct |
43 |
Correct |
314 ms |
21500 KB |
Output is correct |
44 |
Correct |
233 ms |
21356 KB |
Output is correct |
45 |
Correct |
315 ms |
21372 KB |
Output is correct |
46 |
Correct |
225 ms |
21360 KB |
Output is correct |
47 |
Correct |
365 ms |
21368 KB |
Output is correct |
48 |
Correct |
232 ms |
21356 KB |
Output is correct |
49 |
Correct |
307 ms |
21356 KB |
Output is correct |
50 |
Correct |
339 ms |
21508 KB |
Output is correct |
51 |
Correct |
316 ms |
21356 KB |
Output is correct |
52 |
Correct |
89 ms |
15220 KB |
Output is correct |
53 |
Correct |
126 ms |
15228 KB |
Output is correct |
54 |
Correct |
75 ms |
15092 KB |
Output is correct |
55 |
Correct |
128 ms |
15312 KB |
Output is correct |
56 |
Correct |
121 ms |
15224 KB |
Output is correct |
57 |
Correct |
199 ms |
15548 KB |
Output is correct |
58 |
Correct |
285 ms |
15236 KB |
Output is correct |
59 |
Correct |
243 ms |
15584 KB |
Output is correct |
60 |
Correct |
261 ms |
15552 KB |
Output is correct |
61 |
Correct |
272 ms |
15804 KB |
Output is correct |
62 |
Correct |
165 ms |
15560 KB |
Output is correct |
63 |
Correct |
229 ms |
15548 KB |
Output is correct |
64 |
Correct |
242 ms |
15552 KB |
Output is correct |
65 |
Incorrect |
10 ms |
7256 KB |
Output isn't correct |
66 |
Halted |
0 ms |
0 KB |
- |