#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
#define fi first
#define se second
#define left BAO
#define right ANH
#define pb push_back
#define pf push_front
#define mp make_pair
#define ins insert
#define btpc __builtin_popcount
#define btclz __builtin_clz
#define sz(x) (int)(x.size());
#define all(x) x.begin(), x.end()
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y)
{
x = y;
return true;
}
return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y)
{
x = y;
return true;
}
return false;
}
const int MOD = 1e9 + 7; //998244353
template<class X, class Y>
void add(X &x, const Y &y) {
x = (x + y);
if(x >= MOD) x -= MOD;
}
template<class X, class Y>
void sub(X &x, const Y &y) {
x = (x - y);
if(x < 0) x += MOD;
}
/* Author : Le Ngoc Bao Anh, 12A5, LQD High School for Gifted Student*/
const ll INF = 1e9;
const int N = 2e5 + 10;
vector<pii> g[N];
ll t[N];
bool used[N], ok[N];
int cnt = 0;
void dfs(int u, int par, ll wpar, ll lim) {
ll r = 0, c = 0;
t[u] = ok[u] = 0;
for(auto [v, w] : g[u]) {
if(v != par) {
dfs(v, u, w, lim);
if(t[v] - w >= 0) ok[u] = true;
if(ok[v]) maximize(r, t[v] - w);
else maximize(c, (-t[v]) + w);
}
}
if(r >= c) t[u] = r;
else {
t[u] = -c;
ok[u] = false;
}
if(u == 1 && !ok[u]) {
used[u] = 1;
++cnt;
return;
}
if(!ok[u] && -(t[u] - wpar) > lim) {
t[u] = lim;
ok[u] = true;
used[u] = true;
++cnt;
}
}
void BaoJiaoPisu() {
int n, k; cin >> n >> k;
for(int i = 1; i < n; i++) {
int u, v, w; cin >> u >> v >> w;
g[u].pb(mp(v, w));
g[v].pb(mp(u, w));
}
if(k == n) {
cout << 0 << '\n';
for(int i = 1; i <= n; i++) cout << i << " ";
return;
}
vector<int> ans;
ll l = 1, r = INF * n, res = -1;
while(l <= r) {
ll mid = (l + r) >> 1;
for(int i = 1; i <= n; i++) used[i] = false;
cnt = 0;
dfs(1, 0, 0, mid);
if(cnt <= k) {
ans.clear();
res = mid;
for(int i = 1; i <= n; i++) {
if(used[i]) ans.pb(i);
}
for(int i = 1; i <= n; i++) {
if(!used[i] && ans.size() < k) ans.pb(i);
}
r = mid - 1;
} else l = mid + 1;
}
cout << res << '\n';
for(auto x : ans) cout << x << " ";
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tc = 1, ddd = 0;
// cin >> tc;
while(tc--) {
//ddd++;
//cout << "Case #" << ddd << ": ";
BaoJiaoPisu();
}
}
Compilation message
Main.cpp: In function 'void BaoJiaoPisu()':
Main.cpp:140:31: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
140 | if(!used[i] && ans.size() < k) ans.pb(i);
| ~~~~~~~~~~~^~~
Main.cpp: In function 'int main()':
Main.cpp:154:17: warning: unused variable 'ddd' [-Wunused-variable]
154 | int tc = 1, ddd = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4960 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5040 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5036 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
5036 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
430 ms |
33264 KB |
Output is correct |
2 |
Correct |
402 ms |
34036 KB |
Output is correct |
3 |
Correct |
374 ms |
14784 KB |
Output is correct |
4 |
Correct |
1080 ms |
13840 KB |
Output is correct |
5 |
Correct |
1059 ms |
13640 KB |
Output is correct |
6 |
Correct |
1020 ms |
13576 KB |
Output is correct |
7 |
Correct |
1046 ms |
13648 KB |
Output is correct |
8 |
Correct |
1185 ms |
14112 KB |
Output is correct |
9 |
Correct |
1117 ms |
13872 KB |
Output is correct |
10 |
Correct |
1016 ms |
14088 KB |
Output is correct |
11 |
Correct |
703 ms |
15544 KB |
Output is correct |
12 |
Correct |
713 ms |
15884 KB |
Output is correct |
13 |
Correct |
826 ms |
16916 KB |
Output is correct |
14 |
Correct |
720 ms |
15712 KB |
Output is correct |
15 |
Correct |
726 ms |
15152 KB |
Output is correct |
16 |
Correct |
743 ms |
15932 KB |
Output is correct |
17 |
Correct |
745 ms |
15180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
404 ms |
34556 KB |
Output is correct |
2 |
Correct |
416 ms |
33732 KB |
Output is correct |
3 |
Correct |
380 ms |
32168 KB |
Output is correct |
4 |
Correct |
377 ms |
32204 KB |
Output is correct |
5 |
Correct |
413 ms |
35232 KB |
Output is correct |
6 |
Correct |
483 ms |
34560 KB |
Output is correct |
7 |
Correct |
501 ms |
35664 KB |
Output is correct |
8 |
Correct |
435 ms |
34732 KB |
Output is correct |
9 |
Correct |
465 ms |
34484 KB |
Output is correct |
10 |
Correct |
390 ms |
33888 KB |
Output is correct |
11 |
Correct |
408 ms |
32812 KB |
Output is correct |
12 |
Correct |
513 ms |
36368 KB |
Output is correct |
13 |
Correct |
529 ms |
36320 KB |
Output is correct |
14 |
Correct |
465 ms |
35276 KB |
Output is correct |
15 |
Correct |
409 ms |
33948 KB |
Output is correct |
16 |
Correct |
379 ms |
32672 KB |
Output is correct |
17 |
Correct |
414 ms |
32440 KB |
Output is correct |
18 |
Correct |
406 ms |
34152 KB |
Output is correct |
19 |
Correct |
418 ms |
34500 KB |
Output is correct |
20 |
Correct |
445 ms |
34888 KB |
Output is correct |
21 |
Correct |
423 ms |
34228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4960 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5040 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
5036 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
5036 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
430 ms |
33264 KB |
Output is correct |
16 |
Correct |
402 ms |
34036 KB |
Output is correct |
17 |
Correct |
374 ms |
14784 KB |
Output is correct |
18 |
Correct |
1080 ms |
13840 KB |
Output is correct |
19 |
Correct |
1059 ms |
13640 KB |
Output is correct |
20 |
Correct |
1020 ms |
13576 KB |
Output is correct |
21 |
Correct |
1046 ms |
13648 KB |
Output is correct |
22 |
Correct |
1185 ms |
14112 KB |
Output is correct |
23 |
Correct |
1117 ms |
13872 KB |
Output is correct |
24 |
Correct |
1016 ms |
14088 KB |
Output is correct |
25 |
Correct |
703 ms |
15544 KB |
Output is correct |
26 |
Correct |
713 ms |
15884 KB |
Output is correct |
27 |
Correct |
826 ms |
16916 KB |
Output is correct |
28 |
Correct |
720 ms |
15712 KB |
Output is correct |
29 |
Correct |
726 ms |
15152 KB |
Output is correct |
30 |
Correct |
743 ms |
15932 KB |
Output is correct |
31 |
Correct |
745 ms |
15180 KB |
Output is correct |
32 |
Correct |
404 ms |
34556 KB |
Output is correct |
33 |
Correct |
416 ms |
33732 KB |
Output is correct |
34 |
Correct |
380 ms |
32168 KB |
Output is correct |
35 |
Correct |
377 ms |
32204 KB |
Output is correct |
36 |
Correct |
413 ms |
35232 KB |
Output is correct |
37 |
Correct |
483 ms |
34560 KB |
Output is correct |
38 |
Correct |
501 ms |
35664 KB |
Output is correct |
39 |
Correct |
435 ms |
34732 KB |
Output is correct |
40 |
Correct |
465 ms |
34484 KB |
Output is correct |
41 |
Correct |
390 ms |
33888 KB |
Output is correct |
42 |
Correct |
408 ms |
32812 KB |
Output is correct |
43 |
Correct |
513 ms |
36368 KB |
Output is correct |
44 |
Correct |
529 ms |
36320 KB |
Output is correct |
45 |
Correct |
465 ms |
35276 KB |
Output is correct |
46 |
Correct |
409 ms |
33948 KB |
Output is correct |
47 |
Correct |
379 ms |
32672 KB |
Output is correct |
48 |
Correct |
414 ms |
32440 KB |
Output is correct |
49 |
Correct |
406 ms |
34152 KB |
Output is correct |
50 |
Correct |
418 ms |
34500 KB |
Output is correct |
51 |
Correct |
445 ms |
34888 KB |
Output is correct |
52 |
Correct |
423 ms |
34228 KB |
Output is correct |
53 |
Correct |
1117 ms |
15440 KB |
Output is correct |
54 |
Correct |
1067 ms |
15704 KB |
Output is correct |
55 |
Correct |
1237 ms |
16084 KB |
Output is correct |
56 |
Correct |
1258 ms |
15952 KB |
Output is correct |
57 |
Correct |
1261 ms |
16616 KB |
Output is correct |
58 |
Correct |
1135 ms |
15520 KB |
Output is correct |
59 |
Correct |
1248 ms |
17928 KB |
Output is correct |
60 |
Correct |
1213 ms |
16068 KB |
Output is correct |
61 |
Correct |
994 ms |
15224 KB |
Output is correct |
62 |
Correct |
1036 ms |
16300 KB |
Output is correct |
63 |
Correct |
1277 ms |
16092 KB |
Output is correct |
64 |
Correct |
1155 ms |
17536 KB |
Output is correct |
65 |
Correct |
1191 ms |
15820 KB |
Output is correct |
66 |
Correct |
1087 ms |
16460 KB |
Output is correct |
67 |
Correct |
1074 ms |
15240 KB |
Output is correct |
68 |
Correct |
1282 ms |
17976 KB |
Output is correct |
69 |
Correct |
425 ms |
35668 KB |
Output is correct |
70 |
Correct |
411 ms |
34144 KB |
Output is correct |
71 |
Correct |
499 ms |
37480 KB |
Output is correct |
72 |
Correct |
292 ms |
15928 KB |
Output is correct |
73 |
Correct |
328 ms |
16144 KB |
Output is correct |
74 |
Correct |
358 ms |
17872 KB |
Output is correct |
75 |
Correct |
724 ms |
17888 KB |
Output is correct |
76 |
Correct |
807 ms |
18320 KB |
Output is correct |
77 |
Correct |
735 ms |
17716 KB |
Output is correct |
78 |
Correct |
773 ms |
18180 KB |
Output is correct |