#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<pair<int, int>> adj[200000];
vector<int> vec;
pair<int, int> dp(int x, int v, int par) {
int mxover = -1e18, mxunder = 0, pard;
for(auto i : adj[x]) {
if(i.first == par) {
pard = i.second;
continue;
}
pair<int, int> tmp = dp(i.first, v, x);
if(tmp.first == -1) mxunder = max(mxunder, tmp.second);
else mxover = max(mxover, tmp.second);
}
pair<int, int> res;
if(mxover >= mxunder) {
res = make_pair(1, mxover-pard);
} else {
res = make_pair(-1, mxunder + pard);
}
if(res.first == -1 && res.second > v) {
vec.push_back(x);
res = make_pair(1, v-pard);
}
if(res.first == 1 && res.second < 0) {
res = make_pair(-1, 0);
}
return res;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, k, x, y, w;
cin >> n >> k;
for(int i = 0; i < n-1; i++) {
cin >> x >> y >> w;
x--; y--;
adj[x].push_back(make_pair(y, w));
adj[y].push_back(make_pair(x, w));
}
int l = 0, r = 1e18, m;
while(l < r) {
m = (l+r)/2;
vec.clear();
dp(0, m, 1e18);
if(vec.size() > k) l = m+1;
else r = m;
}
vec.clear();
dp(0, l, 1e18);
cout << l << '\n';
for(int i : vec) cout << i+1 << " ";
}
Compilation message
Main.cpp: In function 'int32_t main()':
Main.cpp:52:17: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
52 | if(vec.size() > k) l = m+1;
| ~~~~~~~~~~~^~~
Main.cpp: In function 'std::pair<long long int, long long int> dp(long long int, long long int, long long int)':
Main.cpp:26:21: warning: 'pard' may be used uninitialized in this function [-Wmaybe-uninitialized]
26 | if(res.first == -1 && res.second > v) {
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5016 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4940 KB |
Unexpected end of file - int32 expected |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
313 ms |
29020 KB |
Output is correct |
2 |
Incorrect |
340 ms |
29636 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
337 ms |
30184 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5016 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4940 KB |
Unexpected end of file - int32 expected |
3 |
Halted |
0 ms |
0 KB |
- |