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 0Sample 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;}