博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj2114 Boatherds
阅读量:4625 次
发布时间:2019-06-09

本文共 4213 字,大约阅读时间需要 14 分钟。

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

62 5 3 7 4 1 005 2 6 3 000018131400

Sample Output

AYEAYENAYAYE. 询问树上有没有长度为k的路径……对于询问一个一个点分治……反正n才10000m才100 输入好蛋疼……第一行n,接下来n行,每两个数ci、di,表示i和ci有长度为di的边,以0结束。接下来输入询问,以0结束。又有多组数据,以0结束
#include
#include
#include
#define LL long long#define N 40010#define mod 10207using 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]

 

转载于:https://www.cnblogs.com/zhber/p/4216260.html

你可能感兴趣的文章
ASP常用读取数据2个调用方式
查看>>
【大话UWB定位】之蓝牙定位的烦恼
查看>>
算法3-高级排序
查看>>
每天一个linux命令(17):whereis 命令
查看>>
Angular4+路由
查看>>
Codeforces-234C Weather
查看>>
面向对象编程思想及其相关内容
查看>>
Leetcode解题笔记-3sum
查看>>
Android 3.0 Hardware Acceleration
查看>>
【2011 Greater New York Regional 】Problem G: Rancher's Gift
查看>>
java常见题目总结
查看>>
(六) 牛顿切线法求根
查看>>
使用transform(平移,缩放,旋转)
查看>>
数位dp——BZOJ1026 Windy数
查看>>
oracle 查询表中重复数据
查看>>
mysql查询结果乱码
查看>>
《构建之法》读书笔记01
查看>>
不用JQuery,原生Javascript实现Ajax功能及相关知识点
查看>>
Keil MDK中的Code, RO-data , RW-data, ZI-data分别代表什么意思?(转)
查看>>
WPF listbox数据绑定
查看>>