#LCA0000049. Chạy khỏi chuồng bò (Running Away From the Barn)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Chạy khỏi chuồng bò (Running Away From the Barn)
Running Away From the Barn
Source: USACO
Version: Phuoc Hung OJ Extended
Problem Statement
A farm consists of pastures connected as a weighted tree rooted at the barn, pasture . From its starting pasture, a cow may only take roads that lead strictly farther from the barn, i.e. down the rooted tree.
The total traveled distance cannot exceed , and the cow may also stay where it started. For each starting pasture , count how many different pastures can be reached under these rules.
Input
- The first line contains .
- For each , line contains . Here is pasture 's parent and is the length of edge .
Output
Print exactly lines. Line gives the number of possible final pastures from , including itself.
Subtasks
General constraints: ; ; ; .
-
Subtask 1 (20 points): .
-
Subtask 2 (30 points): .
-
Subtask 3 (50 points): .
Examples
Input
4 5
1 4
2 3
1 5
Output
3
2
1
1
Explanation
From pasture , a cow can reach with travel lengths . It cannot reach because the route has length . From pasture , it can reach or ; pastures and can only reach themselves.