#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, int 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(t[v] > 0) maximize(r, t[v] - w);
else maximize(c, -t[v] + w);
}
}
if(r >= c) t[u] = r;
else t[u] = -c;
if(u == 1 && t[u] < 0) {
used[u] = 1;
++cnt;
return;
}
if(!ok[u] && -(t[u] - wpar) > lim) {
t[u] = lim;
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:136:31: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
136 | if(!used[i] && ans.size() < k) ans.pb(i);
| ~~~~~~~~~~~^~~
Main.cpp: In function 'int main()':
Main.cpp:150:17: warning: unused variable 'ddd' [-Wunused-variable]
150 | int tc = 1, ddd = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
415 ms |
34876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
429 ms |
36244 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |