Bandwidth Management
Time limit2sMemory limit1024 MB
Given a tree with vertex limits and a queue of path-add reservations, find the longest prefix of reservations that keeps every vertex under its limit.
- Level
Hard8 of 10
- Topics
- Tree, Prefix sum, Binary search
- Solved
- No attempts yet
Problem
You are the network administrator of the shake! contest. Every network use is handled by registering a reservation in a queue and receiving your approval.
Surprisingly, the network is a graph with a tree structure. Each vertex of the network is numbered from to , and every edge is bidirectional.
Each reservation is written as . The initial bandwidth usage of every vertex is . When a reservation is approved, the bandwidth usage of every vertex on the shortest path between vertex and vertex increases by . The shortest path includes both endpoints and .
Each vertex has a bandwidth limit. If a vertex uses too much bandwidth, the whole server goes down, and as an administrator you cannot accept that. In other words, no vertex's bandwidth usage may exceed its limit. The queue order matters, so you cannot approve a later reservation without approving the earlier ones. For example, you may accept reservations 1, 2, and 3, or accept none of them. You cannot accept only reservations 1 and 3, or only reservations 2 and 3.
Find the maximum number of reservations that can be approved.
Input
The first line contains the number of vertices and the number of reservations , separated by a space.
The next lines each contain two vertices and connected by an edge, separated by a space.
The next line contains integers , separated by spaces. is the bandwidth limit of vertex .
The next lines each contain integers describing a reservation, in the order they were added to the queue. If the reservation is approved, the bandwidth usage of every vertex on the shortest path between vertex and vertex increases by .
The network given in the input is a tree.
Output
Print the maximum number of reservations that can be approved, as a single integer.