#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define rep(i, n) for(int i = 1; (i) <= (n); ++i)
#define forn(i, l, r) for(int i = (l); i <= (r); ++i)
#define ford(i, r, l) for(int i = (r); i >= (l); --i)
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define FORD(i, n) for(int i = ((n) - 1); i >= 0; --i)
#define fi first
#define se second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define endl "\n"
#define task "note"
#define sz(a) int(a.size())
#define C(x, y) make_pair(x, y)
#define all(a) (a).begin(), (a).end()
#define bit(i, mask) (mask >> i & 1)
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
inline int readInt() {char c;while(c=getchar(),c!='-'&&(c<'0'||c>'9'));bool sign=(c=='-');if(sign)c=getchar();int n=c-'0';while(c=getchar(),c>='0'&&c<='9')n=10*n+c-'0';return(!sign)?n:-n;}
inline ll readLong() {char c;while(c=getchar(),c!='-'&&(c<'0'||c>'9'));bool sign=(c=='-');if(sign)c=getchar();ll n=c-'0';while(c=getchar(),c>='0'&&c<='9')n=10*n+c-'0';return(!sign)?n:-n;}
inline string readString() {char c;while(c=getchar(),c==' '||c=='\n'||c=='\t');string s({c});while(c=getchar(),c!=EOF&&c!=' '&&c!='\n'&&c!='\t')s+=c;return s;}
const int N = 2e5 + 33;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, k;
int a[N];
vector<pii> g[N];
const ll INF = 1e17;
int lim;
vector<int> ans;
pll dfs(int u, int p = -1, int wpar = 0)
{
ll dist = INF, reach = 0;
for(auto &[v, w] : g[u]) if(v != p)
{
pll tmp = dfs(v, u, w);
maximize(reach, tmp.fi);
minimize(dist, tmp.se);
}
if(reach + dist <= lim) return {0, dist + wpar};
if(p != -1)
{
if(reach + wpar > lim)
{
ans.pb(u);
return {0, wpar};
}
}
else
{
ans.pb(u);
return {0, wpar};
}
return {reach + wpar, dist + wpar};
}
bool check(ll threshold)
{
ans.clear();
lim = threshold;
dfs(1);
return sz(ans) <= k;
}
bool mark[N];
void solve()
{
cin >> n >> k;
// rep(i, n) cin >> a[i];
rep(i, n - 1)
{
int u, v, w;
cin >> u >> v >> w;
g[u].pb({v, w});
g[v].pb({u, w});
}
ll l = 0, r = 1e15, res = 1e9;
while(l <= r)
{
ll mid = l + r >> 1;
if(check(mid)) res = mid, r = mid - 1;
else l = mid + 1;
}
cout << res << endl;
check(res);
for(int x : ans) mark[x] = 1, --k;
rep(i, n) if(k && !mark[i]) mark[i] = 1, --k;
rep(i, n) if(mark[i]) cout << i << " ";
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int TC = 1;
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
while(TC--)
{
solve();
cout << endl;
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:112:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |