This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define bg begin
#define ed end
#define ft front
#define bk back
#define sz(x) (int)(x).size()
#define all(x) (x).bg(), (x).ed()
#define mtp make_tuple
#define ins insert
#define ers erase
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define str string
#define pi pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpi vector<pi>
#define vpll vector<pll>
#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define FOS(i, r, l) for (int i = (r); i >= (l); --i)
#define FRN(i, n) for (int i = 0; i < (n); ++i)
#define FSN(i, n) for (int i = (n) - 1; i >= 0; --i)
#define EACH(i, x) for (auto &i : (x))
#define WHILE while
template<typename T> T gcd(T a, T b) { WHILE(b) { a %= b; swap(a, b); } return b; }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
const ll oo = 1e18 + 5;
const int N = 3e5 + 5;
int n;
vpi adj[N];
ll k;
vi ans;
pll dfs(int u, int p) {
    int pw = 0;
    vpll a;
    EACH(j, adj[u]) {
        int v = j.st, w = j.nd;
        if (v == p) {
            pw = w;
            continue;
        }
        a.pb(dfs(v, u));
        a.bk().st += w;
        a.bk().nd += w;
    }
    pll min1 = {oo, oo}, min2 = {oo, oo};
    EACH(j, a)
        if (j < min1) {
            min2 = min1;
            min1 = j;
        }
        else min2 = min(min2, j);
    pll rm = {oo, -oo};
    EACH(j, a) {
        pll tmp = (j == min1? min2 : min1);
        if (tmp.st + j.nd <= k) continue;
        rm.st = min(rm.st, j.st);
        rm.nd = max(rm.nd, j.nd);
    }
    if (rm.nd < -1e17) {
        if (min1.st <= k) return {min1.st, rm.nd};
        if (p && pw <= k) return {min1.st, max(0LL, rm.nd)};
        ans.pb(u);
        return {0, -oo};
    }
    else {
        if (p && rm.nd + pw <= k) return {min1.st, rm.nd};
        ans.pb(u);
        return {0LL, -oo};
    }
}
signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//    freopen(file".inp", "r", stdin);
//    freopen(file".out", "w", stdout);
    cin >> n >> k;
    FRN(i, n - 1) {
        int a, b, d;
        cin >> a >> b >> d;
        adj[a].pb({b, d});
        adj[b].pb({a, d});
    }
    dfs(1, 0);
    cout << sz(ans) << '\n';
    EACH(i, ans) cout << i << (i == ans.bk()? "\n" : " ");
    return 0;
}
/*
5 1
5 3 3
3 2 3
5 1 2
5 4 1
*/
| # | 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... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |