#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
#define BIT(x, i) (((x) >> (i)) & 1)
//#define endl '\n'
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
const int MOD = 1e9 + 7;
const int N = 2e5 + 30;
int n;
vector<pair<int, int>> g[N];
ll dep[N];
void dfs0(int v, int p = -1){
for (auto [u, w] : g[v]){
if (u != p){
dep[u] = dep[v] + w;
dfs0(u, v);
}
}
}
vector<int> parks(ll d){
set<pair<ll, int>> s;
for (int i = 1; i <= n; i++) s.emplace(dep[i], i);
function<void(int, int, ll)> dfs = [&](int v, int p, ll dist){
s.erase(MP(dep[v], v));
for (auto [u, w] : g[v]){
if (u == p) continue;
if (w <= dist){
dfs(u, v, dist - w);
}
}
};
vector<int> ans;
while (!s.empty()){
int v = s.begin()->second;
ans.push_back(v);
dfs(v, -1, d);
}
return ans;
}
void solve(){
int k;
cin >> n >> k;
for (int i = 1; i < n; i++){
int u, v, w;
cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
int root = 1;
for (int i = 1; i <= n; i++){
if (g[i].size() > 1) root = i;
}
dfs0(1);
vector<int> ans;
ll l = 0, r = 2e15;
while (l <= r){
ll m = (l + r) >> 1;
vector<int> p = parks(m);
if (p.size() <= k){
ans = p;
r = m - 1;
} else {
l = m + 1;
}
}
cout << ++r << endl;
for (int x : ans) cout << x << ' ';
int x = k - (int) ans.size();
for (int i = 1; i <= n; i++){
if (x == 0) break;
int j = lower_bound(all(ans), i) - ans.begin();
if (j == ans.size() || ans[j] != i){
cout << i << ' ';
x--;
}
}
cout << endl;
}
int main(){
clock_t startTime = clock();
ios_base::sync_with_stdio(false);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int test_cases = 1;
// cin >> test_cases;
for (int test = 1; test <= test_cases; test++){
// cout << (solve() ? "YES" : "NO") << endl;
solve();
}
#ifdef LOCAL
cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl;
#endif
return 0;
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:86:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
86 | if (p.size() <= k){
| ~~~~~~~~~^~~~
Main.cpp:102:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | if (j == ans.size() || ans[j] != i){
| ~~^~~~~~~~~~~~~
Main.cpp:71:9: warning: variable 'root' set but not used [-Wunused-but-set-variable]
71 | int root = 1;
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:112:13: warning: unused variable 'startTime' [-Wunused-variable]
112 | clock_t startTime = clock();
| ^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2612 ms |
53440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3053 ms |
55540 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |