#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 1e6 + 10;
const int MOD = 998244353;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) {
return bm(b, MOD-2);
}
int fastlog(int x) {
return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
vector<pair<int,int>> adj[MAXN];
int n, k, d[MAXN];
pair<int, vector<int> > find(int radius) {
int used=0;
vector<int> moments;
for(int i=1;i<=n;i++){
int j=i;
int bruh = 0;
while(j+1 <= n && bruh + d[j] <= radius) {
bruh += d[j];
j++;
}
bruh = 0;
moments.push_back(j);
while(j+1 <= n && bruh + d[j] <= radius) {
bruh += d[j];
j++;
}
used++;
i = j;
}
if(moments.size() < k) {
set<int> st;
for(int X: moments) st.insert(X);
for(int i=1; i<=n; i++) {
if(!st.count(i)) moments.push_back(i);
if(moments.size() == k) break;
}
}
return {used, moments};
}
void solve(int tc){
cin>>n>>k;
for(int i=1;i<n;i++){
int a,b,w;
cin>>a>>b>>w;
d[i]=w;
adj[a].push_back({b,w});
adj[b].push_back({a,w});
}
int lb=1, rb=1e18;
while(lb<rb){
int mid=(lb+rb)>>1;
if(find(mid).first<=k) rb=mid;
else lb=mid+1;
}
pair<int,vector<int>>res=find(lb);
cout<<lb<<"\n";
for(int x:res.second)cout<<x<<" ";
cout<<"\n";
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
// 勿忘初衷
Compilation message
Main.cpp: In function 'std::pair<long long int, std::vector<long long int> > find(long long int)':
Main.cpp:44:21: 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]
44 | if(moments.size() < k) {
| ~~~~~~~~~~~~~~~^~~
Main.cpp:49:25: 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]
49 | if(moments.size() == k) break;
| ~~~~~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23780 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Incorrect |
12 ms |
23712 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
33988 KB |
Output is correct |
2 |
Correct |
71 ms |
34324 KB |
Output is correct |
3 |
Incorrect |
79 ms |
34656 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
34532 KB |
Output is correct |
2 |
Correct |
67 ms |
34164 KB |
Output is correct |
3 |
Correct |
66 ms |
33612 KB |
Output is correct |
4 |
Correct |
67 ms |
33672 KB |
Output is correct |
5 |
Correct |
776 ms |
46040 KB |
Output is correct |
6 |
Correct |
251 ms |
40012 KB |
Output is correct |
7 |
Correct |
195 ms |
41332 KB |
Output is correct |
8 |
Correct |
67 ms |
34584 KB |
Output is correct |
9 |
Correct |
75 ms |
34488 KB |
Output is correct |
10 |
Correct |
66 ms |
34180 KB |
Output is correct |
11 |
Correct |
63 ms |
33788 KB |
Output is correct |
12 |
Correct |
1006 ms |
46688 KB |
Output is correct |
13 |
Correct |
609 ms |
47740 KB |
Output is correct |
14 |
Correct |
222 ms |
43684 KB |
Output is correct |
15 |
Correct |
63 ms |
37068 KB |
Output is correct |
16 |
Correct |
66 ms |
36644 KB |
Output is correct |
17 |
Correct |
60 ms |
36556 KB |
Output is correct |
18 |
Correct |
81 ms |
37280 KB |
Output is correct |
19 |
Correct |
395 ms |
47896 KB |
Output is correct |
20 |
Correct |
239 ms |
46184 KB |
Output is correct |
21 |
Correct |
186 ms |
43292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23780 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Incorrect |
12 ms |
23712 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |