#include <bits/stdc++.h>
#define inf 1e16
using namespace std;
ifstream fin("geana.in");
ofstream fout("geana.out");
long long n, k;
vector<pair<long long, long long>> g[200001];
vector<int> sol;
long long deep[200001], closest[200001];
long long ans;
void dfs(long long node, long long parent, long long lim, long long cost, bool reconst) {
closest[node] = inf;
deep[node] = -inf;
for (auto i : g[node]) {
if (i.first != parent) {
dfs(i.first, node, lim, i.second, reconst);
deep[node] = max(deep[node], deep[i.first] + i.second);
closest[node] = min(closest[node], closest[i.first] + i.second);
}
}
if (closest[node] > lim && deep[node] < 0) {
deep[node] = 0;
}
if (deep[node] + closest[node] <= lim) {
deep[node] = -inf;
return;
}
if (deep[node] + cost > lim || (node==1&&(deep[node] >= 0))) {
if (reconst) {
sol.push_back(node);
}
ans++;
closest[node] = 0;
deep[node] = -inf;
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> k;
for (long long i = 2; i <= n; ++i) {
long long a, b, w;
cin >> a >> b >> w;
g[a].push_back({ b,w });
g[b].push_back({ a,w });
}
long long st = 0, dr = inf;
long long rep = 0;
while (st <= dr) {
long long mid = (st + dr) / 2;
ans = 0;
dfs(1, 0, mid, 0, 0);
if (ans <= k) {
rep = mid;
dr = mid - 1;
}
else {
st = mid + 1;
}
}
cout << rep << '\n';
dfs(1, 0, rep, 0, true);
vector<bool> seen(n + 1);
for (auto i : sol) {
seen[i] = true;
}
for (int i = 1; i <= n; ++i) {
if ((int)sol.size() == k) {
break;
}
if (!seen[i]) {
sol.push_back(i);
}
}
for (auto i : sol) {
cout << i << ' ';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5036 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5036 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5036 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
520 ms |
37300 KB |
Output is correct |
2 |
Correct |
561 ms |
42468 KB |
Output is correct |
3 |
Correct |
460 ms |
21988 KB |
Output is correct |
4 |
Correct |
1536 ms |
21724 KB |
Output is correct |
5 |
Correct |
1365 ms |
21184 KB |
Output is correct |
6 |
Correct |
1391 ms |
21212 KB |
Output is correct |
7 |
Correct |
1367 ms |
20028 KB |
Output is correct |
8 |
Correct |
1480 ms |
20832 KB |
Output is correct |
9 |
Correct |
1465 ms |
21120 KB |
Output is correct |
10 |
Correct |
1449 ms |
21512 KB |
Output is correct |
11 |
Correct |
970 ms |
23724 KB |
Output is correct |
12 |
Correct |
965 ms |
23676 KB |
Output is correct |
13 |
Correct |
1065 ms |
25440 KB |
Output is correct |
14 |
Correct |
1053 ms |
22160 KB |
Output is correct |
15 |
Correct |
1026 ms |
21708 KB |
Output is correct |
16 |
Correct |
1073 ms |
23212 KB |
Output is correct |
17 |
Correct |
1063 ms |
22296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
556 ms |
38780 KB |
Output is correct |
2 |
Correct |
483 ms |
42120 KB |
Output is correct |
3 |
Correct |
406 ms |
40112 KB |
Output is correct |
4 |
Correct |
413 ms |
40092 KB |
Output is correct |
5 |
Correct |
441 ms |
43420 KB |
Output is correct |
6 |
Correct |
473 ms |
42856 KB |
Output is correct |
7 |
Correct |
479 ms |
44324 KB |
Output is correct |
8 |
Correct |
457 ms |
42708 KB |
Output is correct |
9 |
Correct |
448 ms |
42396 KB |
Output is correct |
10 |
Correct |
433 ms |
41760 KB |
Output is correct |
11 |
Correct |
411 ms |
40300 KB |
Output is correct |
12 |
Correct |
480 ms |
44208 KB |
Output is correct |
13 |
Correct |
475 ms |
44272 KB |
Output is correct |
14 |
Correct |
466 ms |
43312 KB |
Output is correct |
15 |
Correct |
423 ms |
40888 KB |
Output is correct |
16 |
Correct |
419 ms |
39476 KB |
Output is correct |
17 |
Correct |
412 ms |
39168 KB |
Output is correct |
18 |
Correct |
431 ms |
41272 KB |
Output is correct |
19 |
Correct |
426 ms |
41028 KB |
Output is correct |
20 |
Correct |
439 ms |
41920 KB |
Output is correct |
21 |
Correct |
431 ms |
41036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5036 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
5036 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5036 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
4 ms |
4948 KB |
Output is correct |
15 |
Correct |
520 ms |
37300 KB |
Output is correct |
16 |
Correct |
561 ms |
42468 KB |
Output is correct |
17 |
Correct |
460 ms |
21988 KB |
Output is correct |
18 |
Correct |
1536 ms |
21724 KB |
Output is correct |
19 |
Correct |
1365 ms |
21184 KB |
Output is correct |
20 |
Correct |
1391 ms |
21212 KB |
Output is correct |
21 |
Correct |
1367 ms |
20028 KB |
Output is correct |
22 |
Correct |
1480 ms |
20832 KB |
Output is correct |
23 |
Correct |
1465 ms |
21120 KB |
Output is correct |
24 |
Correct |
1449 ms |
21512 KB |
Output is correct |
25 |
Correct |
970 ms |
23724 KB |
Output is correct |
26 |
Correct |
965 ms |
23676 KB |
Output is correct |
27 |
Correct |
1065 ms |
25440 KB |
Output is correct |
28 |
Correct |
1053 ms |
22160 KB |
Output is correct |
29 |
Correct |
1026 ms |
21708 KB |
Output is correct |
30 |
Correct |
1073 ms |
23212 KB |
Output is correct |
31 |
Correct |
1063 ms |
22296 KB |
Output is correct |
32 |
Correct |
556 ms |
38780 KB |
Output is correct |
33 |
Correct |
483 ms |
42120 KB |
Output is correct |
34 |
Correct |
406 ms |
40112 KB |
Output is correct |
35 |
Correct |
413 ms |
40092 KB |
Output is correct |
36 |
Correct |
441 ms |
43420 KB |
Output is correct |
37 |
Correct |
473 ms |
42856 KB |
Output is correct |
38 |
Correct |
479 ms |
44324 KB |
Output is correct |
39 |
Correct |
457 ms |
42708 KB |
Output is correct |
40 |
Correct |
448 ms |
42396 KB |
Output is correct |
41 |
Correct |
433 ms |
41760 KB |
Output is correct |
42 |
Correct |
411 ms |
40300 KB |
Output is correct |
43 |
Correct |
480 ms |
44208 KB |
Output is correct |
44 |
Correct |
475 ms |
44272 KB |
Output is correct |
45 |
Correct |
466 ms |
43312 KB |
Output is correct |
46 |
Correct |
423 ms |
40888 KB |
Output is correct |
47 |
Correct |
419 ms |
39476 KB |
Output is correct |
48 |
Correct |
412 ms |
39168 KB |
Output is correct |
49 |
Correct |
431 ms |
41272 KB |
Output is correct |
50 |
Correct |
426 ms |
41028 KB |
Output is correct |
51 |
Correct |
439 ms |
41920 KB |
Output is correct |
52 |
Correct |
431 ms |
41036 KB |
Output is correct |
53 |
Correct |
1341 ms |
21580 KB |
Output is correct |
54 |
Correct |
1386 ms |
22076 KB |
Output is correct |
55 |
Correct |
1501 ms |
22852 KB |
Output is correct |
56 |
Correct |
1431 ms |
22460 KB |
Output is correct |
57 |
Correct |
1396 ms |
23188 KB |
Output is correct |
58 |
Correct |
1349 ms |
21828 KB |
Output is correct |
59 |
Correct |
1437 ms |
24452 KB |
Output is correct |
60 |
Correct |
1469 ms |
21452 KB |
Output is correct |
61 |
Correct |
1276 ms |
19868 KB |
Output is correct |
62 |
Correct |
1354 ms |
20860 KB |
Output is correct |
63 |
Correct |
1448 ms |
21408 KB |
Output is correct |
64 |
Correct |
1354 ms |
22404 KB |
Output is correct |
65 |
Correct |
1465 ms |
21648 KB |
Output is correct |
66 |
Correct |
1324 ms |
22132 KB |
Output is correct |
67 |
Correct |
1313 ms |
20700 KB |
Output is correct |
68 |
Correct |
1450 ms |
23996 KB |
Output is correct |
69 |
Correct |
439 ms |
42384 KB |
Output is correct |
70 |
Correct |
421 ms |
39824 KB |
Output is correct |
71 |
Correct |
469 ms |
43812 KB |
Output is correct |
72 |
Correct |
341 ms |
20992 KB |
Output is correct |
73 |
Correct |
339 ms |
20336 KB |
Output is correct |
74 |
Correct |
344 ms |
22064 KB |
Output is correct |
75 |
Correct |
967 ms |
24168 KB |
Output is correct |
76 |
Correct |
992 ms |
23756 KB |
Output is correct |
77 |
Correct |
946 ms |
23444 KB |
Output is correct |
78 |
Correct |
959 ms |
23288 KB |
Output is correct |