#P4149. [IOI 2011] Race

[IOI 2011] Race

Description

Given a tree with a weight on each edge, find a simple path whose total weight equals kk, with the minimum possible number of edges.

Input Format

The first line contains two integers n,kn, k, representing the number of vertices in the tree and the target path sum.

Each of the next n1n-1 lines contains three integers ui,vi,wiu_i, v_i, w_i, representing an undirected edge between uiu_i and viv_i with weight wiw_i.

Note: vertices are numbered starting from 00.

Output Format

Output a single integer, the minimal number of edges.

If no such path exists, output 1-1.

4 3
0 1 1
1 2 2
1 3 4
2

Hint

For 100%100\% of the testdata, it is guaranteed that 1n2×1051 \leq n \leq 2 \times 10^5, 0k,wi1060 \leq k, w_i \leq 10^6, 0ui,vi<n0 \leq u_i, v_i < n.

Translated by ChatGPT 5