#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e6+2;
const int mod = 1e9 + 7;
const ll inf = 1e17;
ll lim;
ll dist1[200002], dist2[200002], cnt, k, n;
bool viz[200002], ok[200002];
vector<pair<int, int>>G[200002];
void dfs (int nod, int par, ll last) {
dist1[nod] = 0;
dist2[nod] = inf;
for (auto [u, x] : G[nod]) {
if (u == par)
continue;
dfs(u, nod, x);
if (ok[u])
dist2[nod] = min(dist2[nod], (x + dist2[u]));
else
dist1[nod] = max(dist1[nod], x + dist1[u]);
}
if (dist1[nod] <= lim - dist2[nod]) {
ok[nod] = 1;
return;
}
if (dist1[nod] + last > lim) {
ok[nod] = 1;
viz[nod] = 1;
dist2[nod] = 0;
cnt++;
return;
}
ok[nod] = 0;
return;
}
bool solve () {
cnt = 0;
memset(viz, 0, sizeof(viz));
dfs(1, 0, 2*inf);
return (cnt <= k);
}
void cb () {
ll st = 0, dr = inf, ans;
while (st <= dr) {
lim = (st + dr) / 2;
if (solve()) {
ans = lim;
dr = lim - 1;
}
else
st = lim +1;
}
cout << ans << "\n";
lim = ans;
}
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i < n; i++) {
int x, y, z;
cin >> x >> y >> z;
G[x].push_back({y, z});
G[y].push_back({x, z});
}
cb();
memset(viz, 0, sizeof(viz));
cnt = 0;
dfs(1, 0, 2*inf);
for (int i = 1; i <= n; i++)
if (viz[i])
cout << i << " ";
for (int i = 1; i <= n && cnt < k; i++)
if (!viz[i])
cout << i << " ", cnt++;
return 0;
}
Compilation message
Main.cpp: In function 'void dfs(int, int, ll)':
Main.cpp:20:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
20 | for (auto [u, x] : G[nod]) {
| ^
Main.cpp: In function 'void cb()':
Main.cpp:62:7: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
62 | lim = ans;
| ~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6796 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
3 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6800 KB |
Output is correct |
7 |
Correct |
2 ms |
6796 KB |
Output is correct |
8 |
Correct |
2 ms |
7000 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 |
7004 KB |
Output is correct |
12 |
Correct |
2 ms |
6796 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
391 ms |
31944 KB |
Output is correct |
2 |
Correct |
406 ms |
36964 KB |
Output is correct |
3 |
Correct |
261 ms |
20708 KB |
Output is correct |
4 |
Correct |
1486 ms |
19632 KB |
Output is correct |
5 |
Correct |
933 ms |
19180 KB |
Output is correct |
6 |
Correct |
1040 ms |
19188 KB |
Output is correct |
7 |
Correct |
1219 ms |
17992 KB |
Output is correct |
8 |
Correct |
1171 ms |
18656 KB |
Output is correct |
9 |
Correct |
1311 ms |
19012 KB |
Output is correct |
10 |
Correct |
1252 ms |
19344 KB |
Output is correct |
11 |
Correct |
862 ms |
21104 KB |
Output is correct |
12 |
Correct |
745 ms |
21140 KB |
Output is correct |
13 |
Correct |
737 ms |
22604 KB |
Output is correct |
14 |
Correct |
643 ms |
19656 KB |
Output is correct |
15 |
Correct |
619 ms |
19176 KB |
Output is correct |
16 |
Correct |
708 ms |
20644 KB |
Output is correct |
17 |
Correct |
542 ms |
19856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
429 ms |
33360 KB |
Output is correct |
2 |
Correct |
376 ms |
36616 KB |
Output is correct |
3 |
Correct |
390 ms |
35020 KB |
Output is correct |
4 |
Correct |
421 ms |
34948 KB |
Output is correct |
5 |
Correct |
384 ms |
37204 KB |
Output is correct |
6 |
Correct |
359 ms |
36936 KB |
Output is correct |
7 |
Correct |
426 ms |
37928 KB |
Output is correct |
8 |
Correct |
382 ms |
37040 KB |
Output is correct |
9 |
Correct |
407 ms |
36772 KB |
Output is correct |
10 |
Correct |
397 ms |
36144 KB |
Output is correct |
11 |
Correct |
344 ms |
34996 KB |
Output is correct |
12 |
Correct |
415 ms |
37520 KB |
Output is correct |
13 |
Correct |
413 ms |
37736 KB |
Output is correct |
14 |
Correct |
374 ms |
37164 KB |
Output is correct |
15 |
Correct |
361 ms |
35572 KB |
Output is correct |
16 |
Correct |
360 ms |
34012 KB |
Output is correct |
17 |
Correct |
375 ms |
34036 KB |
Output is correct |
18 |
Correct |
453 ms |
35496 KB |
Output is correct |
19 |
Correct |
357 ms |
34896 KB |
Output is correct |
20 |
Correct |
369 ms |
35740 KB |
Output is correct |
21 |
Correct |
361 ms |
35008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
6796 KB |
Output is correct |
3 |
Correct |
2 ms |
6748 KB |
Output is correct |
4 |
Correct |
2 ms |
6748 KB |
Output is correct |
5 |
Correct |
3 ms |
6748 KB |
Output is correct |
6 |
Correct |
2 ms |
6800 KB |
Output is correct |
7 |
Correct |
2 ms |
6796 KB |
Output is correct |
8 |
Correct |
2 ms |
7000 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 |
7004 KB |
Output is correct |
12 |
Correct |
2 ms |
6796 KB |
Output is correct |
13 |
Correct |
2 ms |
6748 KB |
Output is correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Correct |
391 ms |
31944 KB |
Output is correct |
16 |
Correct |
406 ms |
36964 KB |
Output is correct |
17 |
Correct |
261 ms |
20708 KB |
Output is correct |
18 |
Correct |
1486 ms |
19632 KB |
Output is correct |
19 |
Correct |
933 ms |
19180 KB |
Output is correct |
20 |
Correct |
1040 ms |
19188 KB |
Output is correct |
21 |
Correct |
1219 ms |
17992 KB |
Output is correct |
22 |
Correct |
1171 ms |
18656 KB |
Output is correct |
23 |
Correct |
1311 ms |
19012 KB |
Output is correct |
24 |
Correct |
1252 ms |
19344 KB |
Output is correct |
25 |
Correct |
862 ms |
21104 KB |
Output is correct |
26 |
Correct |
745 ms |
21140 KB |
Output is correct |
27 |
Correct |
737 ms |
22604 KB |
Output is correct |
28 |
Correct |
643 ms |
19656 KB |
Output is correct |
29 |
Correct |
619 ms |
19176 KB |
Output is correct |
30 |
Correct |
708 ms |
20644 KB |
Output is correct |
31 |
Correct |
542 ms |
19856 KB |
Output is correct |
32 |
Correct |
429 ms |
33360 KB |
Output is correct |
33 |
Correct |
376 ms |
36616 KB |
Output is correct |
34 |
Correct |
390 ms |
35020 KB |
Output is correct |
35 |
Correct |
421 ms |
34948 KB |
Output is correct |
36 |
Correct |
384 ms |
37204 KB |
Output is correct |
37 |
Correct |
359 ms |
36936 KB |
Output is correct |
38 |
Correct |
426 ms |
37928 KB |
Output is correct |
39 |
Correct |
382 ms |
37040 KB |
Output is correct |
40 |
Correct |
407 ms |
36772 KB |
Output is correct |
41 |
Correct |
397 ms |
36144 KB |
Output is correct |
42 |
Correct |
344 ms |
34996 KB |
Output is correct |
43 |
Correct |
415 ms |
37520 KB |
Output is correct |
44 |
Correct |
413 ms |
37736 KB |
Output is correct |
45 |
Correct |
374 ms |
37164 KB |
Output is correct |
46 |
Correct |
361 ms |
35572 KB |
Output is correct |
47 |
Correct |
360 ms |
34012 KB |
Output is correct |
48 |
Correct |
375 ms |
34036 KB |
Output is correct |
49 |
Correct |
453 ms |
35496 KB |
Output is correct |
50 |
Correct |
357 ms |
34896 KB |
Output is correct |
51 |
Correct |
369 ms |
35740 KB |
Output is correct |
52 |
Correct |
361 ms |
35008 KB |
Output is correct |
53 |
Correct |
1132 ms |
19512 KB |
Output is correct |
54 |
Correct |
1102 ms |
19928 KB |
Output is correct |
55 |
Correct |
1312 ms |
20552 KB |
Output is correct |
56 |
Correct |
1084 ms |
20140 KB |
Output is correct |
57 |
Correct |
928 ms |
20380 KB |
Output is correct |
58 |
Correct |
842 ms |
19636 KB |
Output is correct |
59 |
Correct |
1048 ms |
21212 KB |
Output is correct |
60 |
Correct |
1377 ms |
19084 KB |
Output is correct |
61 |
Correct |
848 ms |
17868 KB |
Output is correct |
62 |
Correct |
878 ms |
18312 KB |
Output is correct |
63 |
Correct |
1270 ms |
19144 KB |
Output is correct |
64 |
Correct |
1043 ms |
19296 KB |
Output is correct |
65 |
Correct |
1180 ms |
19684 KB |
Output is correct |
66 |
Correct |
942 ms |
19440 KB |
Output is correct |
67 |
Correct |
1106 ms |
18640 KB |
Output is correct |
68 |
Correct |
975 ms |
20636 KB |
Output is correct |
69 |
Correct |
425 ms |
36868 KB |
Output is correct |
70 |
Correct |
366 ms |
34632 KB |
Output is correct |
71 |
Correct |
467 ms |
37632 KB |
Output is correct |
72 |
Correct |
263 ms |
19952 KB |
Output is correct |
73 |
Correct |
415 ms |
19120 KB |
Output is correct |
74 |
Correct |
289 ms |
20028 KB |
Output is correct |
75 |
Correct |
801 ms |
21564 KB |
Output is correct |
76 |
Correct |
782 ms |
20992 KB |
Output is correct |
77 |
Correct |
652 ms |
21056 KB |
Output is correct |
78 |
Correct |
760 ms |
20196 KB |
Output is correct |