This page is still under construction.

Parts of this page are still being built. What you see may change.

Boatherds

Time limit1sMemory limit128 MB

Summary
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 x1,…,xkx_1, \dots, x_k. For each xix_i, decide whether there exists a pair of villages (a,b)(a, b) such that the fare of the trip between aa and bb equals exactly xix_i.

Input

The input consists of several instances. Each instance is described as follows, in order:

  • A line with a single integer NN — the number of villages (1≤N≤10 0001 \le N \le 10\,000).
  • NN lines describing the villages. The ii-th of these lines describes village ii and contains the space-separated integers d1,c1,d2,c2,…,dki,cki,0d_1, c_1, d_2, c_2, \dots, d_{k_i}, c_{k_i}, 0. The djd_j are the villages whose rivers flow directly into village ii (with no other village in between), and each cjc_j is the price of the segment between villages ii and djd_j. Here 2≤dj≤N2 \le d_j \le N and 0≤cj≤1 0000 \le c_j \le 1\,000. Village 11 is always the mouth of the largest river, so no djd_j is ever equal to 11. The list is terminated by a single 00.
  • M≤100M \le 100 lines describing the queries. The ii-th line contains one integer xix_i (1≤xi≤10 000 0001 \le x_i \le 10\,000\,000).
  • The instance is terminated by a single line containing 00.

The whole input is terminated by a single line containing 00.

Output

For each instance output a sequence of MM lines (where MM is the number of queries in that instance). The ii-th line contains the word AYE if the network has a pair of villages joined by a path of cost exactly xix_i, or the word NAY otherwise.

The output for each instance must be followed by a single line containing just a dot character (.).

Examples4

  1. Example 1

    Input
    6
    2 5 3 7 4 1 0
    0
    5 2 6 3 0
    0
    0
    0
    1
    8
    13
    14
    0
    0
    
    Expected output
    AYE
    AYE
    NAY
    AYE
    .
    
  2. Example 2

    Input
    1
    0
    5
    1
    0
    0
    
    Expected output
    NAY
    NAY
    .
    
  3. Example 3

    Input
    2
    2 10 0
    0
    10
    5
    0
    0
    
    Expected output
    AYE
    NAY
    .
    
  4. Example 4

    Input
    5
    2 1 3 2 4 3 5 4 0
    0
    0
    0
    0
    1
    6
    7
    8
    10
    0
    0
    
    Expected output
    AYE
    AYE
    AYE
    NAY
    NAY
    .