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

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

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的路径

解题报告:比较水的点分治,很久没打,细节忘光了,重心都忘了怎么求了(这哪是细节问题= =!)
解题思路和统计<k的路径方案差不多,对统计的地方稍加修改,同样是两个单调指针,如果t[l]+t[r]恰好等于k,那么就找出与l相等的数量和与r相等的数量相乘即可,注意如果t[l]=t[r],就要另加讨论.

#include 
#include
#include
#include
#include
#include
#define RG register#define il inline#define iter iterator#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))using namespace std;typedef long long ll;const int N=10005,inf=2e8,MAXZ=10000002;int head[N],num=0,to[N<<1],nxt[N<<1],dis[N<<1],n,dx,root,son[N],sum;bool vis[N];int f[N],v[N],t[N];ll ans=0;void link(int x,int y,int dist){ nxt[++num]=head[x];to[num]=y;dis[num]=dist;head[x]=num;}void getroot(int x,int last){ int u;son[x]=1;f[x]=0; for(int i=head[x];i;i=nxt[i]){ u=to[i];if(u==last || vis[u])continue; getroot(u,x); son[x]+=son[u]; f[x]=Max(f[x],son[u]); } f[x]=Max(f[x],sum-son[x]); if(f[x]
>1; break; } int i=l,j=r; while(t[i]==t[l])i++; while(t[j]==t[r])j--; tot+=(i-l)*(r-j); l=i;r=j; } else if(t[l]+t[r]>dx)r--; else l++; } return tot;}void dfs(int x){ ans+=cal(x,0); int u;vis[x]=true; for(int i=head[x];i;i=nxt[i]){ u=to[i];if(vis[u])continue; ans-=cal(u,dis[i]);sum=son[u]; root=0;getroot(u,x); dfs(root); }}bool check(){ memset(vis,0,sizeof(vis)); root=0;sum=n;getroot(1,1); dfs(root); return ans;}void Clear(){ num=0;memset(head,0,sizeof(head));}void work(){ Clear(); int x,y,flag; for(int i=1;i<=n;i++){ while(1){ scanf("%d",&x);if(!x)break; scanf("%d",&y); link(i,x,y);link(x,i,y); } } while(scanf("%d",&x)){ if(!x){ puts("."); return ; } dx=x;f[0]=inf; flag=check();ans=0; if(flag)puts("AYE"); else puts("NAY"); }}int main(){ while(~scanf("%d",&n) && n) work(); return 0;}

转载于:https://www.cnblogs.com/Yuzao/p/7446392.html

你可能感兴趣的文章
SVD综述和Mahout中实现
查看>>
Linux第七周学习总结——可执行程序的装载
查看>>
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
查看>>
细说php(二) 变量和常量
查看>>
iOS开发网络篇之Web Service和XML数据解析
查看>>
Windows 7 x64环境下JDK8安装过程
查看>>
UVA - 11077 Find the Permutations (置换)
查看>>
个人寒假作业项目《印象笔记》第一天
查看>>
table 数据少时 ,tr高度变化
查看>>
使用ASP.NET加密口令
查看>>
用js实现表单增加一行、删除一行时,序号自动改变
查看>>
conda查找安装包的版本以及安装特定版本的包
查看>>
简单方法 解决此图片来自微信公众平台,未经允许不可引用
查看>>
5-Java-C(调和级数)
查看>>
切换到新的项目,保留历史提交记录
查看>>
css一
查看>>
LeetCode-Excel Sheet Column Number
查看>>
Servlet
查看>>
MongoDB - Installing MongoDB on Windows
查看>>
Oracle “dba_tables”介绍
查看>>