poj2114 Boatherds(好久没翻译的说)

译文:

时间限制: 2000MS 空间限制: 65536K

问题描述

Boatherds inc是一家在Trabantustan的帆船公司,在Trabantian rivers提供河上旅行。所有的河流都发源于山中的某个地方,在向低地流的途中,逐渐汇入,最终形成一条大河流向大海。然而,Trabantian村正好位于河流的入海口。注意:两条河可以在一个路口汇聚。河流形成的是一棵树(村庄是顶点)。 

Boatherds的收费规则很简单:每一段河有一个价格(价格在两个方向上都是一样的),所以如果一个旅游请求任何两个村庄之间的旅程,售票处职员只添加的价格段沿着路径之间的村庄。 

一天,一个有怪癖的游客出现了。ta告诉导购员说,ta第二天就会回国,ta想把剩下的钱都花在河上旅行上,所以要求公司找到一条这样的路线。然而他们只是可怜的商人,他们只能向ACM求助。

你得到了一个关于河网的描述,包括河流段的成本和一列整数x1,……,xk。对于每一个xi,你应该确定在河网中是否有一对城市(a,b),使得旅行的花费正好是xi。 

输入

输入由多组数据组成。每组数据由如下描述:
 

  • 包含一个整数的单行:村庄的个数N,描述村庄的个数。 
  • N行中,第i行描述了第i的村庄,包含了用空格分隔的整数d1,c1,d2,c2,…,dki,cki,0。dj是河流的起点村庄j(直接流入村庄i),cj是村庄i和村庄j之间的旅游价格。村庄1是入海口,因此没有任何一个di可以等于1。  
  • M行描述查询。第i行对应于第i查询,包含一个整数xi  
  • 一组数据由一个数字0完成。


输入以一个数字0结束。 

输出

对于每个输入,应该输出一个M行序列(其中M是输入中的查询数)。
如果在河网中有成本为xi的路径,那么输出“AYE”,否则输出“NAY”。 

每个输出最后必须包含一个点字符。 

样例输入

6
2 5 3 7 4 1 0
0
5 2 6 3 0
0
0
0
1
8
13
14
0
0

样例输出

AYE
AYE
NAY
AYE
.

原文:

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 2564 Accepted: 829

Description

Boatherds Inc. is a sailing company operating in the country of Trabantustan and offering boat trips on Trabantian rivers. All the rivers originate somewhere in the mountains and on their way down to the lowlands they gradually join and finally the single resulting river flows to the sea. Moreover, the Trabantian villages are exactly at the rivers’ springs, junctions and at the mouth of the largest river. Please note that more than 2 rivers can join at a junction. However, the rivers always form a tree (with villages as vertices). 

The pricing policy of the Boatherds is very simple: each segment of each river between two villages is assigned a price (the price is same in both directions), so if a tourist requests a journey between any two villages, the ticket office clerks just add the prices of the segments along the only path between the villages. 

One day, a very strange tourist appeared. She told the clerks that she returns to her country on the next day and she wants to spend all the remaining money on a boat trip, so they should find a route with exactly this cost. Being just poor (ahem) businessmen, they have asked the Abacus Calculator Makers for help.

You are given a description of the river network with costs of river segments and a sequence of integers x1,…, xk. For each xi, you should determine if there is a pair of cities (a, b) in the river network such that the cost of the trip between a and b is exactly xi. 

Input

The input consists of several instances. Each instance is described by (in the following order): 
  • A single line containing a single integer: the number of villages N (1 <= N <= 10 000). 
  • N lines describing the villages. The i-th of these lines (1 <= i <= N) describes the village with number i. It contains space separated integers d1, c1, d2, c2, , dki, cki, 0. The dj’s are numbers of villages from which the rivers flow directly to the village i (with no other villages in between), each cj is the price of the journey between villages i and dj. Moreover, 2 <= dj <= N and 0 <= cj <= 1 000. Village 1 always corresponds to the mouth of the largest river, therefore no di can ever be equal to 1. 
  • M <= 100 lines describing the queries. The i-th of these lines corresponds to the i-th query and contains a single integer xi (1 <= xi <= 10 000 000). 
  • The instance is finished by a single line containing the number 0.

The whole input is ended by a single line containing the number 0. 

Output

For each instance you should produce a sequence of M lines (where M is the number of queries in the particular instance). The i-th of these lines contains the word "AYE" if there exists a pair of cities in the river network which is connected by a path of cost xi, or the word "NAY" otherwise. 

Output for each instance must be followed by a single line containing just the dot character. 

Sample Input

6
2 5 3 7 4 1 0
0
5 2 6 3 0
0
0
0
1
8
13
14
0
0

Sample Output

AYE
AYE
NAY
AYE
.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值