// 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 = 1e5+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 |
1 ms |
2652 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 |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
1 ms |
2908 KB |
Output is correct |
12 |
Correct |
2 ms |
3160 KB |
Output is correct |
13 |
Correct |
2 ms |
2904 KB |
Output is correct |
14 |
Correct |
2 ms |
2908 KB |
Output is correct |
15 |
Correct |
1 ms |
2904 KB |
Output is correct |
16 |
Correct |
2 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2912 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
2856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
2984 KB |
Output is correct |
5 |
Correct |
17 ms |
4516 KB |
Output is correct |
6 |
Correct |
117 ms |
11184 KB |
Output is correct |
7 |
Correct |
223 ms |
19096 KB |
Output is correct |
8 |
Correct |
273 ms |
19704 KB |
Output is correct |
9 |
Correct |
238 ms |
19312 KB |
Output is correct |
10 |
Correct |
281 ms |
19568 KB |
Output is correct |
11 |
Correct |
222 ms |
19052 KB |
Output is correct |
12 |
Correct |
271 ms |
19568 KB |
Output is correct |
13 |
Correct |
241 ms |
19152 KB |
Output is correct |
14 |
Correct |
274 ms |
19568 KB |
Output is correct |
15 |
Correct |
279 ms |
19380 KB |
Output is correct |
16 |
Correct |
263 ms |
19564 KB |
Output is correct |
17 |
Correct |
272 ms |
19568 KB |
Output is correct |
18 |
Correct |
280 ms |
19392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 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 |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
1 ms |
2908 KB |
Output is correct |
12 |
Correct |
2 ms |
3160 KB |
Output is correct |
13 |
Correct |
2 ms |
2904 KB |
Output is correct |
14 |
Correct |
2 ms |
2908 KB |
Output is correct |
15 |
Correct |
1 ms |
2904 KB |
Output is correct |
16 |
Correct |
2 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2912 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
2856 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 |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2984 KB |
Output is correct |
27 |
Correct |
17 ms |
4516 KB |
Output is correct |
28 |
Correct |
117 ms |
11184 KB |
Output is correct |
29 |
Correct |
223 ms |
19096 KB |
Output is correct |
30 |
Correct |
273 ms |
19704 KB |
Output is correct |
31 |
Correct |
238 ms |
19312 KB |
Output is correct |
32 |
Correct |
281 ms |
19568 KB |
Output is correct |
33 |
Correct |
222 ms |
19052 KB |
Output is correct |
34 |
Correct |
271 ms |
19568 KB |
Output is correct |
35 |
Correct |
241 ms |
19152 KB |
Output is correct |
36 |
Correct |
274 ms |
19568 KB |
Output is correct |
37 |
Correct |
279 ms |
19380 KB |
Output is correct |
38 |
Correct |
263 ms |
19564 KB |
Output is correct |
39 |
Correct |
272 ms |
19568 KB |
Output is correct |
40 |
Correct |
280 ms |
19392 KB |
Output is correct |
41 |
Correct |
1 ms |
2652 KB |
Output is correct |
42 |
Correct |
234 ms |
19184 KB |
Output is correct |
43 |
Correct |
274 ms |
19568 KB |
Output is correct |
44 |
Correct |
239 ms |
19312 KB |
Output is correct |
45 |
Correct |
276 ms |
19568 KB |
Output is correct |
46 |
Correct |
227 ms |
19300 KB |
Output is correct |
47 |
Correct |
282 ms |
19576 KB |
Output is correct |
48 |
Correct |
234 ms |
19320 KB |
Output is correct |
49 |
Correct |
275 ms |
19568 KB |
Output is correct |
50 |
Correct |
280 ms |
19312 KB |
Output is correct |
51 |
Correct |
281 ms |
19468 KB |
Output is correct |
52 |
Correct |
93 ms |
12760 KB |
Output is correct |
53 |
Correct |
118 ms |
12988 KB |
Output is correct |
54 |
Correct |
88 ms |
12368 KB |
Output is correct |
55 |
Correct |
116 ms |
12920 KB |
Output is correct |
56 |
Correct |
112 ms |
12920 KB |
Output is correct |
57 |
Correct |
198 ms |
13156 KB |
Output is correct |
58 |
Correct |
252 ms |
12996 KB |
Output is correct |
59 |
Correct |
210 ms |
13500 KB |
Output is correct |
60 |
Correct |
238 ms |
13244 KB |
Output is correct |
61 |
Correct |
237 ms |
13512 KB |
Output is correct |
62 |
Correct |
168 ms |
12992 KB |
Output is correct |
63 |
Correct |
198 ms |
13200 KB |
Output is correct |
64 |
Correct |
214 ms |
13328 KB |
Output is correct |
65 |
Incorrect |
7 ms |
3420 KB |
Output isn't correct |
66 |
Halted |
0 ms |
0 KB |
- |