Mines
Time limit9sMemory limit1024 MB
For each vertex of a weighted tree, count how many mines explode when that mine is detonated, given that each blast reaches all mines within its radius along tree distance.
- Level
Hard9 of 10
- Topics
- Tree, Divide and conquer, Graph, Sorting
- Solved
- No attempts yet
Problem
"Maybe we could dig up some old problem with a sequence and have them solve it on a tree?"
Problem author
You are given a tree (an undirected acyclic graph) where every edge has a length. Each vertex of the tree holds a mine with a blast radius. When a mine explodes, every mine at a vertex within distance at most its blast radius also explodes, and the chain continues. The distance between two vertices is the sum of the edge lengths on the simple path between them. For each mine, determine how many mines explode when you detonate that one mine by hand. Each manual detonation is considered independently of the manual detonations of the other mines.
Input
The first line contains an integer (), the number of vertices in the tree, which is also the number of mines. The vertices are numbered from 1 to .
The second line contains integers (), where is the blast radius of the mine at vertex .
The next lines each contain three integers , , (; ), meaning there is an edge of length connecting vertices and . The input is guaranteed to describe a valid tree.
Output
Print one line with integers. The -th integer is the number of mines that explode when you detonate the mine at vertex by hand.
Hint
Explanation of the example: the example tree is shown below.

When the mine at the fourth vertex is detonated by hand, its blast sets off the mines at the first and second vertices. The mine at the first vertex then sets off the mine at the fifth vertex. In total, four mines explode.