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
. 询问树上有没有长度为k的路径……对于询问一个一个点分治……反正n才10000m才100
输入好蛋疼……第一行n,接下来n行,每两个数ci、di,表示i和ci有长度为di的边,以0结束。接下来输入询问,以0结束。又有多组数据,以0结束
#include<cstdio>
#include<iostream>
#include<cstring>
#define LL long long
#define N 40010
#define mod 10207
using namespace std;
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct edge{int to,next,v;}e[2*N];
struct hashing{int v,next;}hash[2*N];
int he[N],head[N];
int f[N],son[N],s[N],d[N],query[N];
bool vis[N],ok[N];
int n,m,cnt,tot,root,sum,len,ans;
inline void ins(int u,int v,int w)
{
e[++cnt].to=v;
e[cnt].v=w;
e[cnt].next=head[u];
head[u]=cnt;
}
inline void insert(int u,int v,int w)
{
ins(u,v,w);
ins(v,u,w);
}
inline void inh(int u)
{
int w=u%mod+132;
hash[++tot].v=u;
hash[tot].next=he[w];
he[w]=tot;
}
inline bool fnd(int u)
{
if (u<0)return 0;
int w=u%mod+132;
for (int i=he[w];i;i=hash[i].next)
if (u==hash[i].v)return 1;
return 0;
}
inline void getroot(int x,int fa)
{
son[x]=1;f[x]=0;
for (int i=head[x];i;i=e[i].next)
if (fa!=e[i].to&&!vis[e[i].to])
{
getroot(e[i].to,x);
son[x]+=son[e[i].to];
f[x]=max(f[x],son[e[i].to]);
}
f[x]=max(f[x],sum-son[x]);
if (f[x]<f[root])root=x;
}
inline void dfs(int x,int fa)
{
s[++len]=d[x];
for (int i=head[x];i;i=e[i].next)
if (!vis[e[i].to]&&fa!=e[i].to)
{
d[e[i].to]=d[x]+e[i].v;
dfs(e[i].to,x);
}
}
inline void solve(int x)
{
vis[x]=1;tot=0;
memset(he,0,sizeof(he));inh(0);
for (int i=head[x];i;i=e[i].next)
if (!vis[e[i].to])
{
len=0;
d[e[i].to]=e[i].v;
dfs(e[i].to,0);
for (int j=1;j<=m;j++)
{
if (ok[j])continue;
for (int k=1;k<=len;k++)
if (fnd(query[j]-s[k]))
{
ok[j]=1;
break;
}
}
for (int j=1;j<=len;j++)inh(s[j]);
}
for (int i=head[x];i;i=e[i].next)
if (!vis[e[i].to])
{
sum=son[e[i].to];
root=0;
getroot(e[i].to,0);
solve(root);
}
}
inline void work()
{
memset(head,0,sizeof(head));
memset(vis,0,sizeof(vis));
memset(ok,0,sizeof(ok));
cnt=ans=0;
for(int i=1;i<=n;i++)
{
int x=read(),y;
while (x)
{
y=read();
insert(i,x,y);
x=read();
}
}
m=0;
int xx=read();
while (xx)
{
query[++m]=xx;
xx=read();
}
f[0]=n+1;sum=n;root=0;
getroot(1,0);
solve(root);
for (int i=1;i<=m;i++)
if (ok[i])printf("AYE\n");else printf("NAY\n");
printf(".\n");
}
int main()
{
while (~scanf("%d",&n)&&n)work();
}

poj2114 Boatherds的更多相关文章

  1. 【点分治】poj1741 Tree / poj2114 Boatherds / poj1987 Distance Statistics

    三道题都很类似.给出1741的代码 #include<cstdio> #include<algorithm> #include<cstring> using nam ...

  2. 树链剖分-点的分治(dis[i]+dis[j]==k的点对数量)

    poj2114 Boatherds Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1195   Accepted: 387 ...

  3. 【poj2114】 Boatherds

    http://poj.org/problem?id=2114 (题目链接) 题意 给出一棵树,问是否存在两点间的距离为K. Solution 点分治嘛,跟poj1741差不多.. 然而为什么我调了一个 ...

  4. 【POJ2114】Boatherds 树分而治之

    做广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog. ...

  5. POJ 2114 Boatherds 树分治

    Boatherds     Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...

  6. 【poj2114】点分治(离线)

    boatherds 2s 64M by czy 求一颗树上距离为K的点对是否存在 输入数据 n,m 接下来n-1条边a,b,c描述a到b有一条长度为c的路径 接下来m行每行询问一个K 输出数据 对于每 ...

  7. Poj 2114 Boatherds(点分治)

    Boatherds Time Limit: 2000MS Memory Limit: 65536K Description Boatherds Inc. is a sailing company op ...

  8. POJ 2114 - Boatherds

    原题地址:http://poj.org/problem?id=2114 题目大意: 给定一棵点数为\(n~(n \le 10000)\)的无根树,路径上有权值,给出m组询问($m \le 100$), ...

  9. POJ 2114 Boatherds【Tree,点分治】

    求一棵树上是否存在路径长度为K的点对. POJ 1714求得是路径权值<=K的路径条数,这题只需要更改一下统计路径条数的函数即可,如果最终的路径条数大于零,则说明存在这样的路径. 刚开始我以为只 ...

随机推荐

  1. Android 布局 ViewGroup

    布局 res/layout 命名规则(全部小写) activity_ fragment_ item_ 基础组件 com.android.widget包下 父类View view:屏幕上一块矩阵区域 能 ...

  2. django种表单post出现CSRF verification failed( CSRF验证失败 ) 的两种解决方式

    现象 表单界面例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc29sbG9yNTI1/font/5a6L5L2T/fontsize/400/fi ...

  3. [ES6] Object.assign (with defaults value object)

    function spinner(target, options = {}){ let defaults = { message: "Please wait", spinningS ...

  4. 《UNIX网络编程》之多客户连接服务端,可重用套接字对

    该网络编程之客户端与服务端程序模板支持: 1. 多客户端同时连接服务端,即服务程序可以同时为多个客户端服务: 2. 服务端支持套接字对重用,即即使处于TIME_WAIT状态,仍可支持服务端重启: 3. ...

  5. Activity内部Handler引起内存泄露的原因分析

    有时在Activity中使用Handler时会提示一个内存泄漏的警告,代码通常如下: public class MainActivity extends Activity { private Text ...

  6. Java基础知识强化77:正则表达式之获取功能(Pattern 和 Matcher类的使用)

    1. 获取功能: Pattern 和 Matcher类结合使用 2. 使用案例: package cn.itcast_05; import java.util.regex.Matcher; impor ...

  7. 简单dp --- HDU1248寒冰王座

    题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...

  8. nyoj 17

    // nyoj 17        代码如上,用的是dp,总的来说就是对一个字符串 从末尾开始比较,设定一个数组,存放每个单调字串的最大长度,最后比较... //要注意的就是里面if语句对于每次字符比 ...

  9. Asp.Net WebAPI 中Cookie 获取操作方式

    1. /// <summary> /// 获取上下文中的cookie /// </summary> /// <returns></returns> [H ...

  10. 如何查看linux系统下的各种日志文件 linux 系统日志的分析大全

    日志分类: 1. 连接时间的日志 连接时间日志一般由/var/log/wtmp和/var/run/utmp这两个文件记录,不过这 两个文件无法直接cat查看,并且该文件由系统自动更新,可以通过如下: ...