Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
744 B
1
Indexable
#include <bits/stdc++.h>
using namespace std;

constexpr int maxN = 1e6;

struct edge {
    int b, cost;
};

vector<edge> graph[maxN];

int answ = 1;

void dfs(int u, int x, int p, int curdepth) {
    answ = max(answ, curdepth);
    for (edge &e : graph[u]) {
        if (e.b != p && e.cost <= x) {
            dfs(e.b, x - e.cost, u, curdepth + 1);
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n, x;
    cin >> n >> x;
    for (int i = 0; i < n - 1; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        u--; v--;
        graph[u].emplace_back(v, w);
        graph[v].emplace_back(u, w);
    }
    dfs(0, x, -1, 1);
    cout << answ;
    return 0;
}