#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define pll pair<ll, ll>
#define pii pair<ll, int>
#define fi first
#define se second
using namespace std;
const int N = 2e5+5;
const int M = 250;
const int mod = 1e9+7;
const ll base = 75;
const ll inf = 1e16;
int n, lab[N], tong, d[N], t, c[N], dp[N], k;
ll ans, m, a[N], b[N];
vector<pii> adj[N];
vector<pll> vi;
string s;
bool vis[N];
void dfs(int u, int p = 0)
{
b[u] = 0;
a[u] = -1;
for(pii v: adj[u])
{
if(v.se == p)continue;
dfs(v.se, u);
if(a[v.se] != -1)a[u] = max(a[u], a[v.se]-v.fi);
else
{
if(b[v.se]+v.fi > m)
{
++tong;
vis[v.se] = 1;
a[u] = max(a[u], m-v.fi);
}
else
b[u] = max(b[u], b[v.se]+v.fi);
}
}
if(a[u] >= b[u])b[u] = 0;
}
bool check(ll x)
{
fill_n(vis, n+1, false);
m = x;
tong = 0;
dfs(1);
if(b[1])
{
vis[1] = true;
++tong;
}
return tong <= k;
}
void sol()
{
cin >> n >> k;
for(int i = 1; i < n; i ++)
{
int x, y, w;
cin >> x >> y >> w;
adj[x].pb({w, y});
adj[y].pb({w, x});
}
ll l = 0, r = 1e15, mid;
while(l <= r)
{
mid = (l+r)>>1;
if(check(mid))r = mid-1;
else l = mid+1;
}
check(l);
cout << l << '\n';
for(int i = 1; i <= n; i ++)
{
if(vis[i])cout << i <<" ";
else if(tong < k)
{
++tong;
cout << i <<" ";
}
}
}
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
#define task "test"
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int test = 1;
//cin >> test;
while(test -- > 0)sol();
return 0;
}
/*
1234
21
*/
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
97 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Incorrect |
3 ms |
5020 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
339 ms |
31616 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
332 ms |
32788 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Incorrect |
3 ms |
5020 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |