Boatherds
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):
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 . Source |
[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 30003
using 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");
}
}