poj 2114 Boatherds (点分治)
来源:互联网 发布:淘宝招聘中老年女模特 编辑:程序博客网 时间:2024/06/05 11:09
Boatherds
Time Limit: 2000MS Memory Limit: 65536KTotal 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.
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):
The whole input is ended by a single line containing the number 0.
- 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.
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.
Source
CTU Open 2004
[Submit] [Go Back] [Status] [Discuss]
题目大意:树上是否存在点对(a,b)的路径长恰好=k
题解:点分治
与poj 1741 类似,那个题求的是<=k的个数。
我们在统计的时候将长度相同的点合并,然后再统计即可。需要注意因为合并了,所以a+a=k这种形式就无法用单调性计算了,所以需要特殊处理。
#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#define N 30003using namespace std;int n,m,tot,num[10000003];int point[N],next[N],v[N],len[N],cnt,root,sum,vis[N];int f[N],deep[N],mp[N],d[N],ak[N],mark[N],son[N],g[N];void init(){tot=0;memset(f,0,sizeof(f));memset(point,0,sizeof(point));memset(vis,0,sizeof(vis));memset(mark,0,sizeof(mark));}void add(int x,int y,int z){tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y; len[tot]=z;tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x; len[tot]=z;//cout<<x<<" "<<y<<" "<<z<<endl;}void getroot(int x,int fa){son[x]=1; f[x]=0;for (int i=point[x];i;i=next[i]){if (vis[v[i]]||v[i]==fa) continue;getroot(v[i],x);son[x]+=son[v[i]];f[x]=max(f[x],son[v[i]]);}f[x]=max(f[x],sum-son[x]);if (f[x]<f[root]) root=x;}void getdeep(int x,int fa){deep[++deep[0]]=d[x];for (int i=point[x];i;i=next[i]){if (vis[v[i]]||v[i]==fa) continue;d[v[i]]=d[x]+len[i];getdeep(v[i],x);}}void cal(int x,int now,int opt){d[x]=now; deep[0]=0;getdeep(x,0);sort(deep+1,deep+deep[0]+1);for (int i=1;i<=deep[0];i++) num[deep[i]]=0;for (int i=1;i<=deep[0];i++) num[deep[i]]++; g[0]=0; for (int i=1;i<=deep[0];i++) if (deep[i]!=deep[i-1]) g[++g[0]]=deep[i];for (int i=1;i<=cnt;i++) { int l=1; int r=g[0]; for (int j=1;j<=g[0];j++) if (num[g[j]]>1&&g[j]*2==ak[i]) mark[i]+=opt*num[g[j]]*(num[g[j]]-1); while (l<r) { if (g[l]+g[r]<ak[i]) l++; else { if (g[l]+g[r]==ak[i]) mark[i]+=opt*num[g[r]]*num[g[l]]; r--; } } }}void work(int x){cal(x,0,1);vis[x]=1;for (int i=point[x];i;i=next[i]){if (vis[v[i]]) continue;cal(v[i],len[i],-1);sum=son[v[i]]; root=0;getroot(v[i],x);work(root);}}int main(){freopen("a.in","r",stdin);freopen("my.out","w",stdout);while (true) {scanf("%d",&n);if (!n) break;init();for (int i=1;i<=n;i++) {int x,y; while (true) {scanf("%d",&x);if (!x) break;scanf("%d",&y);add(i,x,y);}}int x; cnt=0;while (true) {scanf("%d",&x);if (!x) break;ak[++cnt]=x;}sum=n; f[0]=1000000000; root=0;getroot(1,0);work(root);for (int i=1;i<=cnt;i++) if (mark[i]>0) printf("AYE\n"); else printf("NAY\n");printf(".\n");}}
0 0
- poj 2114 Boatherds (点分治)
- poj 2114 Boatherds 点分治
- 【POJ】2114 Boatherds 点分治
- POJ 2114 Boatherds 点分治
- POJ 2114 Boatherds 点分治
- Poj 2114 Boatherds(点分治)
- POJ 题目2114 Boatherds(树的点分治)
- POJ 2114 Boatherds【Tree,点分治】
- POJ 2114 Boatherds (树上点分治)
- poj 2114 Boatherds(树分治)
- 【树分治】 POJ 2114 Boatherds
- [POJ2114]Boatherds(点分治+二分)
- POJ2114 Boatherds 点分治
- poj 2114 Boatherds 树的分治
- POJ 2114 Boatherds 树的分治
- POJ 2114Boatherds 树的分治
- BZOJ 1460: Pku2114 Boatherds 点分治
- [树的点分治] [POJ2114] Boatherds
- 开发过程遇到的一些问题(一)
- noip2016
- IOS开发之GCD---dispatch_semaphore
- TSubclassOf<> 和 FStringClassReference 在类成员中的作用
- 路由器的中继模式Repeater和桥接模式Bridge有什么区别
- poj 2114 Boatherds (点分治)
- 在浏览器中,按f5(浏览器中的刷新按钮),与f5+ctrl 、回车的区别
- 圣墟 > 第一百二十九章 异类直播
- Array.prototype.slice() 方法
- cacti安装与配置
- Error:(1, 0) Your project path contains non-ASCII characters. This will most likely
- windows7 下安装python2.7 setuptools pip
- Gson 中文指南
- String.prototype.slice() 方法