Boatherds
Time limit1sMemory limit128 MB
Given a weighted tree and up to 100 queries, decide for each target value whether some pair of vertices has a path cost exactly equal to it.
- Level
Hard8 of 10
- Topics
- Divide and conquer, Tree, DFS, Sorting
- Solved
- No attempts yet
Problem
Boatherds Inc. is a sailing company operating in the country of Trabantustan, offering boat trips on Trabantian rivers. All rivers originate somewhere in the mountains and, on their way down to the lowlands, they gradually join until a single resulting river flows into the sea. The Trabantian villages sit exactly at the rivers' springs, at their junctions, and at the mouth of the largest river. More than two rivers may meet at a junction, but the rivers always form a tree whose vertices are the villages.
The pricing policy is simple: each river segment between two neighbouring villages has a fixed price (the same in both directions). The fare for a journey between any two villages is the sum of the prices of the segments along the unique path connecting them.
One day a peculiar tourist arrived. She leaves the country tomorrow and wants to spend all of her remaining money on a single boat trip, so she asks for a route whose fare is exactly a given amount.
You are given the river network with the cost of every segment and a sequence of integers . For each , decide whether there exists a pair of villages such that the fare of the trip between and equals exactly .
Input
The input consists of several instances. Each instance is described as follows, in order:
- A line with a single integer — the number of villages ().
- lines describing the villages. The -th of these lines describes village and contains the space-separated integers . The are the villages whose rivers flow directly into village (with no other village in between), and each is the price of the segment between villages and . Here and . Village is always the mouth of the largest river, so no is ever equal to . The list is terminated by a single .
- lines describing the queries. The -th line contains one integer ().
- The instance is terminated by a single line containing .
The whole input is terminated by a single line containing .
Output
For each instance output a sequence of lines (where is the number of queries in that instance). The -th line contains the word AYE if the network has a pair of villages joined by a path of cost exactly , or the word NAY otherwise.
The output for each instance must be followed by a single line containing just a dot character (.).