#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 2e5 + 25;
vector <pair <int, ll>> adj2[MAXN];
vector <pair <int, ll>> adj[MAXN];
ll mid;
int n, k;
bool vis[MAXN];
int cnt = 0;
vector <int> u, ans_vec;
void add (int pos) {
//cout << "LOL: " << pos << '\n';
u.push_back(pos);
}
pair <ll, ll> dfs (int pos, int par = -1) {
vis[pos] = 1;
if (par != -1 && adj[pos].size() == 1) return {0, 0};
if (par == -1 && adj[pos].empty()) {
cnt++; add(pos); return {0, 0};
}
vector <pair <ll, ll>> good, bad;
int flag = 0;
for (auto j : adj[pos]) {
if (j.first == par) continue;
auto k = dfs(j.first, pos);
if (k.second == 0) {
if (k.first + j.second > mid) {
flag++; add(j.first); good.push_back({j.second, 1});
} else bad.push_back({k.first + j.second, 0});
} else {
if (k.first + j.second > mid) continue;
good.push_back({k.first + j.second, 1});
}
}
cnt += flag;
sort(good.begin(), good.end());
sort(bad.begin(), bad.end());
/*
cout << pos << " : \n";
for (auto j : good) cout << j.first << " " << j.second << " | ";
cout << '\n';
for (auto j : bad) cout << j.first << " " << j.second << " | ";
cout << '\n' << '\n';
*/
if (par == -1 && good.empty() && bad.empty()) {
cnt++; add(pos); return {0, 1};
}
if (good.empty() && bad.empty()) {
return {0, 0};
}
if (par == -1) {
if (bad.empty()) return {0, 0};
if (good.empty()) {
cnt++; add(pos); return {0, 0};
}
if (bad.back().first + good.front().first > mid) {
cnt++; add(pos); return bad.back();
}
return good.front();
}
if (bad.empty()) {
return good.front();
}
if (good.empty()) return bad.back();
if (bad.back().first + good.front().first > mid) return bad.back();
return good.front();
}
bool check () {
cnt = 0; u.clear();
for (int i = 1; i <= n; i++) {
vis[i] = 0; adj[i].clear();
for (auto j : adj2[i]) {
if (j.second <= mid) {
adj[i].push_back(j);
}
}
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
dfs(i);
//cout << i << " L L L L " << cnt << '\n';
}
}
//cout << mid << " " << cnt << '\n';
return cnt <= k;
}
set <int> pp;
int main () {
cin >> n >> k;
for (int i = 1; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
adj2[a].push_back({b, c});
adj2[b].push_back({a, c});
}
//mid = 47; cout << check() << '\n'; return 0;
ll l = 0, r = 1e16; ll ans = 1;
while (l <= r) {
mid = (l + r) >> 1;
if (check()) {
ans_vec = u;
r = mid - 1; ans = mid;
} else {
l = mid + 1;
}
}
cout << ans << '\n'; //return 0;
for (int i = 1; i <= n; i++) pp.insert(i);
for (auto i : ans_vec) {
cout << i << " "; pp.erase(i); k--;
}
for (int i = 1; i <= n && k; i++) {
if (pp.count(i)) {
k--; cout << i << " ";
}
}
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
2 ms |
9820 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9816 KB |
Output is correct |
6 |
Correct |
2 ms |
9820 KB |
Output is correct |
7 |
Correct |
2 ms |
9820 KB |
Output is correct |
8 |
Correct |
2 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9820 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
11 |
Correct |
2 ms |
9820 KB |
Output is correct |
12 |
Correct |
2 ms |
9820 KB |
Output is correct |
13 |
Correct |
2 ms |
9820 KB |
Output is correct |
14 |
Correct |
3 ms |
9816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1024 ms |
71544 KB |
Output is correct |
2 |
Correct |
1120 ms |
73300 KB |
Output is correct |
3 |
Correct |
1485 ms |
40244 KB |
Output is correct |
4 |
Correct |
1700 ms |
37928 KB |
Output is correct |
5 |
Correct |
1803 ms |
37064 KB |
Output is correct |
6 |
Correct |
1771 ms |
37080 KB |
Output is correct |
7 |
Correct |
1708 ms |
37376 KB |
Output is correct |
8 |
Correct |
1781 ms |
38748 KB |
Output is correct |
9 |
Correct |
1450 ms |
37968 KB |
Output is correct |
10 |
Correct |
1431 ms |
38740 KB |
Output is correct |
11 |
Correct |
1586 ms |
41056 KB |
Output is correct |
12 |
Correct |
1592 ms |
41556 KB |
Output is correct |
13 |
Correct |
1741 ms |
44232 KB |
Output is correct |
14 |
Correct |
1481 ms |
40960 KB |
Output is correct |
15 |
Correct |
1491 ms |
40016 KB |
Output is correct |
16 |
Correct |
1587 ms |
41424 KB |
Output is correct |
17 |
Correct |
1476 ms |
40020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1102 ms |
74304 KB |
Output is correct |
2 |
Correct |
1054 ms |
72496 KB |
Output is correct |
3 |
Correct |
1008 ms |
69460 KB |
Output is correct |
4 |
Correct |
995 ms |
69248 KB |
Output is correct |
5 |
Correct |
635 ms |
74028 KB |
Output is correct |
6 |
Correct |
772 ms |
73744 KB |
Output is correct |
7 |
Correct |
796 ms |
75832 KB |
Output is correct |
8 |
Correct |
1083 ms |
74832 KB |
Output is correct |
9 |
Correct |
1049 ms |
74064 KB |
Output is correct |
10 |
Correct |
1006 ms |
73188 KB |
Output is correct |
11 |
Correct |
1020 ms |
70416 KB |
Output is correct |
12 |
Correct |
760 ms |
76372 KB |
Output is correct |
13 |
Correct |
793 ms |
80656 KB |
Output is correct |
14 |
Correct |
793 ms |
78968 KB |
Output is correct |
15 |
Correct |
1002 ms |
75856 KB |
Output is correct |
16 |
Correct |
1020 ms |
72912 KB |
Output is correct |
17 |
Correct |
948 ms |
72804 KB |
Output is correct |
18 |
Correct |
1053 ms |
76436 KB |
Output is correct |
19 |
Correct |
930 ms |
74980 KB |
Output is correct |
20 |
Correct |
1034 ms |
76852 KB |
Output is correct |
21 |
Correct |
985 ms |
75380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9820 KB |
Output is correct |
2 |
Correct |
2 ms |
9820 KB |
Output is correct |
3 |
Correct |
2 ms |
9820 KB |
Output is correct |
4 |
Correct |
2 ms |
9820 KB |
Output is correct |
5 |
Correct |
2 ms |
9816 KB |
Output is correct |
6 |
Correct |
2 ms |
9820 KB |
Output is correct |
7 |
Correct |
2 ms |
9820 KB |
Output is correct |
8 |
Correct |
2 ms |
9820 KB |
Output is correct |
9 |
Correct |
2 ms |
9820 KB |
Output is correct |
10 |
Correct |
2 ms |
9820 KB |
Output is correct |
11 |
Correct |
2 ms |
9820 KB |
Output is correct |
12 |
Correct |
2 ms |
9820 KB |
Output is correct |
13 |
Correct |
2 ms |
9820 KB |
Output is correct |
14 |
Correct |
3 ms |
9816 KB |
Output is correct |
15 |
Correct |
1024 ms |
71544 KB |
Output is correct |
16 |
Correct |
1120 ms |
73300 KB |
Output is correct |
17 |
Correct |
1485 ms |
40244 KB |
Output is correct |
18 |
Correct |
1700 ms |
37928 KB |
Output is correct |
19 |
Correct |
1803 ms |
37064 KB |
Output is correct |
20 |
Correct |
1771 ms |
37080 KB |
Output is correct |
21 |
Correct |
1708 ms |
37376 KB |
Output is correct |
22 |
Correct |
1781 ms |
38748 KB |
Output is correct |
23 |
Correct |
1450 ms |
37968 KB |
Output is correct |
24 |
Correct |
1431 ms |
38740 KB |
Output is correct |
25 |
Correct |
1586 ms |
41056 KB |
Output is correct |
26 |
Correct |
1592 ms |
41556 KB |
Output is correct |
27 |
Correct |
1741 ms |
44232 KB |
Output is correct |
28 |
Correct |
1481 ms |
40960 KB |
Output is correct |
29 |
Correct |
1491 ms |
40016 KB |
Output is correct |
30 |
Correct |
1587 ms |
41424 KB |
Output is correct |
31 |
Correct |
1476 ms |
40020 KB |
Output is correct |
32 |
Correct |
1102 ms |
74304 KB |
Output is correct |
33 |
Correct |
1054 ms |
72496 KB |
Output is correct |
34 |
Correct |
1008 ms |
69460 KB |
Output is correct |
35 |
Correct |
995 ms |
69248 KB |
Output is correct |
36 |
Correct |
635 ms |
74028 KB |
Output is correct |
37 |
Correct |
772 ms |
73744 KB |
Output is correct |
38 |
Correct |
796 ms |
75832 KB |
Output is correct |
39 |
Correct |
1083 ms |
74832 KB |
Output is correct |
40 |
Correct |
1049 ms |
74064 KB |
Output is correct |
41 |
Correct |
1006 ms |
73188 KB |
Output is correct |
42 |
Correct |
1020 ms |
70416 KB |
Output is correct |
43 |
Correct |
760 ms |
76372 KB |
Output is correct |
44 |
Correct |
793 ms |
80656 KB |
Output is correct |
45 |
Correct |
793 ms |
78968 KB |
Output is correct |
46 |
Correct |
1002 ms |
75856 KB |
Output is correct |
47 |
Correct |
1020 ms |
72912 KB |
Output is correct |
48 |
Correct |
948 ms |
72804 KB |
Output is correct |
49 |
Correct |
1053 ms |
76436 KB |
Output is correct |
50 |
Correct |
930 ms |
74980 KB |
Output is correct |
51 |
Correct |
1034 ms |
76852 KB |
Output is correct |
52 |
Correct |
985 ms |
75380 KB |
Output is correct |
53 |
Correct |
1620 ms |
41900 KB |
Output is correct |
54 |
Correct |
1447 ms |
42860 KB |
Output is correct |
55 |
Correct |
1491 ms |
44544 KB |
Output is correct |
56 |
Correct |
1524 ms |
43580 KB |
Output is correct |
57 |
Correct |
1708 ms |
44324 KB |
Output is correct |
58 |
Correct |
1691 ms |
42112 KB |
Output is correct |
59 |
Correct |
1018 ms |
46100 KB |
Output is correct |
60 |
Correct |
1625 ms |
42648 KB |
Output is correct |
61 |
Correct |
1296 ms |
39764 KB |
Output is correct |
62 |
Correct |
1303 ms |
40924 KB |
Output is correct |
63 |
Correct |
1476 ms |
42832 KB |
Output is correct |
64 |
Correct |
1317 ms |
43352 KB |
Output is correct |
65 |
Correct |
1420 ms |
42760 KB |
Output is correct |
66 |
Correct |
1235 ms |
42844 KB |
Output is correct |
67 |
Correct |
1351 ms |
40788 KB |
Output is correct |
68 |
Correct |
1254 ms |
45884 KB |
Output is correct |
69 |
Correct |
1081 ms |
77416 KB |
Output is correct |
70 |
Correct |
974 ms |
73224 KB |
Output is correct |
71 |
Correct |
818 ms |
80184 KB |
Output is correct |
72 |
Correct |
1367 ms |
41388 KB |
Output is correct |
73 |
Correct |
1086 ms |
40576 KB |
Output is correct |
74 |
Correct |
1132 ms |
42360 KB |
Output is correct |
75 |
Correct |
1602 ms |
46100 KB |
Output is correct |
76 |
Correct |
1742 ms |
46564 KB |
Output is correct |
77 |
Correct |
1611 ms |
45648 KB |
Output is correct |
78 |
Correct |
1573 ms |
45856 KB |
Output is correct |