#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ll long long
#define X first
#define Y second
using namespace std;
const int MAXN = 201000;
vector <pair <int, int> > ve[MAXN];
ll mid;
int uk;
bool flag[MAXN];
int marked[MAXN];
ll dfs(int x, int par) {
if (ve[x].size() == 1 && x != 0) return 0;
ll mi = (1LL << 60), ma = -(1LL << 60);
for (auto tr : ve[x]) {
int y = tr.X;
ll d = tr.Y;
if (y == par) continue;
ll a = dfs(y, x);
if (a == 0 && flag[y] == 1) {
mi = min(mi, 0LL);
ma = max(ma, 0LL);
continue;
}
if (a < 0 && a + d <= 0) flag[x] = 1;
if (a < 0 && a + d > 0) a = 0;
else a += d;
if (a > mid) {
uk++;
marked[y] = 1;
//cout << y << " ";
a = min(-mid + d, 0LL);
if (-mid + d <= 0) flag[x] = 1;
flag[y] = 1;
//cout << y + 1 << endl;
}
//cout << x + 1 << " " << y + 1 << " " << a << endl;
ma = max(ma, a);
mi = min(mi, a);
}
//cout << x + 1 << ": " << mi << " " << ma << endl;
if (-mi >= ma/* && mi != 0*/) {
//flag[x] = 1;
return mi;
}
return ma;
}
int main() {
ios_base::sync_with_stdio(false);
int n, k;
cin >> n >> k;
REP(i, n - 1) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
ve[a].push_back({b, c});
ve[b].push_back({a, c});
}
ll lo = 0, hi = (1LL << 60);
while (lo < hi) {
mid = (lo + hi) / 2;
uk = 0;
memset(flag, 0, sizeof flag);
memset(marked, 0, sizeof marked);
if (dfs(0, 0) > 0) marked[0] = 1, uk++;
else if (flag[0] == 0) marked[0] = 1, uk++;
//cout << endl << uk << endl;
//cout << mid << " " << uk << endl;
if (uk <= k) hi = mid;
else lo = mid + 1;
}
mid = lo;
uk = 0;
memset(flag, 0, sizeof flag);
memset(marked, 0, sizeof marked);
if (dfs(0, 0) > 0) marked[0] = 1, uk++;
else if (flag[0] == 0) marked[0] = 1, uk++;
cout << lo << "\n";
vector <int> out;
REP(i, n) if (marked[i] == 1) out.push_back(i + 1);
REP(i, n) if (out.size() < k && marked[i] == 0) out.push_back(i + 1);
for (int x : out) cout << x << " "; cout << "\n";
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:102:30: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
102 | REP(i, n) if (out.size() < k && marked[i] == 0) out.push_back(i + 1);
| ~~~~~~~~~~~^~~
Main.cpp:103:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
103 | for (int x : out) cout << x << " "; cout << "\n";
| ^~~
Main.cpp:103:41: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
103 | for (int x : out) cout << x << " "; cout << "\n";
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5964 KB |
Output is correct |
2 |
Correct |
5 ms |
5964 KB |
Output is correct |
3 |
Correct |
7 ms |
6092 KB |
Output is correct |
4 |
Correct |
5 ms |
5964 KB |
Output is correct |
5 |
Correct |
5 ms |
5964 KB |
Output is correct |
6 |
Correct |
5 ms |
5964 KB |
Output is correct |
7 |
Correct |
6 ms |
5964 KB |
Output is correct |
8 |
Correct |
5 ms |
5984 KB |
Output is correct |
9 |
Correct |
4 ms |
5964 KB |
Output is correct |
10 |
Correct |
4 ms |
5964 KB |
Output is correct |
11 |
Correct |
5 ms |
6012 KB |
Output is correct |
12 |
Correct |
6 ms |
5964 KB |
Output is correct |
13 |
Correct |
4 ms |
5964 KB |
Output is correct |
14 |
Correct |
6 ms |
6012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
459 ms |
29476 KB |
Output is correct |
2 |
Correct |
534 ms |
30116 KB |
Output is correct |
3 |
Correct |
284 ms |
13676 KB |
Output is correct |
4 |
Correct |
1002 ms |
13000 KB |
Output is correct |
5 |
Correct |
1080 ms |
12776 KB |
Output is correct |
6 |
Correct |
1115 ms |
12780 KB |
Output is correct |
7 |
Correct |
1305 ms |
12832 KB |
Output is correct |
8 |
Correct |
1318 ms |
13208 KB |
Output is correct |
9 |
Correct |
1052 ms |
13012 KB |
Output is correct |
10 |
Correct |
1031 ms |
13252 KB |
Output is correct |
11 |
Correct |
736 ms |
14400 KB |
Output is correct |
12 |
Correct |
708 ms |
14716 KB |
Output is correct |
13 |
Correct |
765 ms |
15540 KB |
Output is correct |
14 |
Correct |
670 ms |
14568 KB |
Output is correct |
15 |
Correct |
677 ms |
14092 KB |
Output is correct |
16 |
Correct |
706 ms |
14628 KB |
Output is correct |
17 |
Correct |
668 ms |
14020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
455 ms |
30552 KB |
Output is correct |
2 |
Correct |
417 ms |
29976 KB |
Output is correct |
3 |
Correct |
390 ms |
28648 KB |
Output is correct |
4 |
Correct |
377 ms |
28588 KB |
Output is correct |
5 |
Correct |
412 ms |
31568 KB |
Output is correct |
6 |
Correct |
423 ms |
30752 KB |
Output is correct |
7 |
Correct |
465 ms |
31740 KB |
Output is correct |
8 |
Correct |
424 ms |
30700 KB |
Output is correct |
9 |
Correct |
417 ms |
30484 KB |
Output is correct |
10 |
Correct |
413 ms |
29956 KB |
Output is correct |
11 |
Correct |
377 ms |
29104 KB |
Output is correct |
12 |
Correct |
454 ms |
32316 KB |
Output is correct |
13 |
Correct |
439 ms |
32184 KB |
Output is correct |
14 |
Correct |
439 ms |
31360 KB |
Output is correct |
15 |
Correct |
388 ms |
29972 KB |
Output is correct |
16 |
Correct |
406 ms |
29056 KB |
Output is correct |
17 |
Correct |
369 ms |
28880 KB |
Output is correct |
18 |
Correct |
415 ms |
30148 KB |
Output is correct |
19 |
Correct |
404 ms |
30784 KB |
Output is correct |
20 |
Correct |
398 ms |
31156 KB |
Output is correct |
21 |
Correct |
407 ms |
30508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5964 KB |
Output is correct |
2 |
Correct |
5 ms |
5964 KB |
Output is correct |
3 |
Correct |
7 ms |
6092 KB |
Output is correct |
4 |
Correct |
5 ms |
5964 KB |
Output is correct |
5 |
Correct |
5 ms |
5964 KB |
Output is correct |
6 |
Correct |
5 ms |
5964 KB |
Output is correct |
7 |
Correct |
6 ms |
5964 KB |
Output is correct |
8 |
Correct |
5 ms |
5984 KB |
Output is correct |
9 |
Correct |
4 ms |
5964 KB |
Output is correct |
10 |
Correct |
4 ms |
5964 KB |
Output is correct |
11 |
Correct |
5 ms |
6012 KB |
Output is correct |
12 |
Correct |
6 ms |
5964 KB |
Output is correct |
13 |
Correct |
4 ms |
5964 KB |
Output is correct |
14 |
Correct |
6 ms |
6012 KB |
Output is correct |
15 |
Correct |
459 ms |
29476 KB |
Output is correct |
16 |
Correct |
534 ms |
30116 KB |
Output is correct |
17 |
Correct |
284 ms |
13676 KB |
Output is correct |
18 |
Correct |
1002 ms |
13000 KB |
Output is correct |
19 |
Correct |
1080 ms |
12776 KB |
Output is correct |
20 |
Correct |
1115 ms |
12780 KB |
Output is correct |
21 |
Correct |
1305 ms |
12832 KB |
Output is correct |
22 |
Correct |
1318 ms |
13208 KB |
Output is correct |
23 |
Correct |
1052 ms |
13012 KB |
Output is correct |
24 |
Correct |
1031 ms |
13252 KB |
Output is correct |
25 |
Correct |
736 ms |
14400 KB |
Output is correct |
26 |
Correct |
708 ms |
14716 KB |
Output is correct |
27 |
Correct |
765 ms |
15540 KB |
Output is correct |
28 |
Correct |
670 ms |
14568 KB |
Output is correct |
29 |
Correct |
677 ms |
14092 KB |
Output is correct |
30 |
Correct |
706 ms |
14628 KB |
Output is correct |
31 |
Correct |
668 ms |
14020 KB |
Output is correct |
32 |
Correct |
455 ms |
30552 KB |
Output is correct |
33 |
Correct |
417 ms |
29976 KB |
Output is correct |
34 |
Correct |
390 ms |
28648 KB |
Output is correct |
35 |
Correct |
377 ms |
28588 KB |
Output is correct |
36 |
Correct |
412 ms |
31568 KB |
Output is correct |
37 |
Correct |
423 ms |
30752 KB |
Output is correct |
38 |
Correct |
465 ms |
31740 KB |
Output is correct |
39 |
Correct |
424 ms |
30700 KB |
Output is correct |
40 |
Correct |
417 ms |
30484 KB |
Output is correct |
41 |
Correct |
413 ms |
29956 KB |
Output is correct |
42 |
Correct |
377 ms |
29104 KB |
Output is correct |
43 |
Correct |
454 ms |
32316 KB |
Output is correct |
44 |
Correct |
439 ms |
32184 KB |
Output is correct |
45 |
Correct |
439 ms |
31360 KB |
Output is correct |
46 |
Correct |
388 ms |
29972 KB |
Output is correct |
47 |
Correct |
406 ms |
29056 KB |
Output is correct |
48 |
Correct |
369 ms |
28880 KB |
Output is correct |
49 |
Correct |
415 ms |
30148 KB |
Output is correct |
50 |
Correct |
404 ms |
30784 KB |
Output is correct |
51 |
Correct |
398 ms |
31156 KB |
Output is correct |
52 |
Correct |
407 ms |
30508 KB |
Output is correct |
53 |
Correct |
1178 ms |
12940 KB |
Output is correct |
54 |
Correct |
1000 ms |
13168 KB |
Output is correct |
55 |
Correct |
1052 ms |
13508 KB |
Output is correct |
56 |
Correct |
988 ms |
13380 KB |
Output is correct |
57 |
Correct |
1095 ms |
14096 KB |
Output is correct |
58 |
Correct |
984 ms |
13008 KB |
Output is correct |
59 |
Correct |
1081 ms |
15320 KB |
Output is correct |
60 |
Correct |
1029 ms |
13436 KB |
Output is correct |
61 |
Correct |
842 ms |
12772 KB |
Output is correct |
62 |
Correct |
960 ms |
13828 KB |
Output is correct |
63 |
Correct |
973 ms |
13472 KB |
Output is correct |
64 |
Correct |
965 ms |
14988 KB |
Output is correct |
65 |
Correct |
927 ms |
13236 KB |
Output is correct |
66 |
Correct |
977 ms |
13984 KB |
Output is correct |
67 |
Correct |
862 ms |
12728 KB |
Output is correct |
68 |
Correct |
1069 ms |
15480 KB |
Output is correct |
69 |
Correct |
404 ms |
30000 KB |
Output is correct |
70 |
Correct |
371 ms |
28712 KB |
Output is correct |
71 |
Correct |
447 ms |
31784 KB |
Output is correct |
72 |
Correct |
190 ms |
13200 KB |
Output is correct |
73 |
Correct |
184 ms |
13620 KB |
Output is correct |
74 |
Correct |
203 ms |
15232 KB |
Output is correct |
75 |
Correct |
675 ms |
14988 KB |
Output is correct |
76 |
Correct |
747 ms |
15172 KB |
Output is correct |
77 |
Correct |
693 ms |
14788 KB |
Output is correct |
78 |
Correct |
783 ms |
15300 KB |
Output is correct |