POJ 题目2114 Boatherds(树的点分治)

本文介绍了一种算法解决方案,用于确定在特定河流网络中是否存在两个村庄之间的旅行成本等于给定值。该问题通过构建树形结构并采用深度优先搜索进行解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Boatherds
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 1712 Accepted: 556

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
.

Source

不想玩了,,无聊水一道吧,,11点熄灯,10点50过了,有种拿顽强拼搏奖的感觉。。
和那个1747有点不同而已,就是求是否有距离为k的点对。。
ac代码
14795687kxh19952114Accepted1680K594MSC++2644B2015-10-08 22:47:21
#include<stdio.h>      
#include<string.h>      
#include<algorithm>      
#include<iostream>      
#define max(a,b) (a>b?a:b)      
using namespace std;    
#define N 100100      
int head[N],cnt,pre[N],sum,son[N],vis[N],deep[N],root,dis[N];    
#define INF 0x3f3f3f3f      
int n,m,ans,k;    
struct s    
{    
    int u,v,next,w;    
}edge[N<<1];    
void add(int u,int v,int w)    
{    
    edge[cnt].u=u;    
    edge[cnt].v=v;    
    edge[cnt].w=w;    
    edge[cnt].next=head[u];    
    head[u]=cnt++;    
}    
void get_root(int u,int fa)    
{    
    son[u]=1;    
    pre[u]=0;    
    int i;    
    for(i=head[u];i!=-1;i=edge[i].next)    
    {    
        int v=edge[i].v;    
        if(v==fa||vis[v])    
            continue;    
        get_root(v,u);    
        son[u]+=son[v];    
        pre[u]=max(pre[u],son[v]);    
    }    
    pre[u]=max(pre[u],sum-son[u]);    
    if(pre[u]<pre[root])    
        root=u;    
}    
void get_deep(int u,int fa)    
{    
    deep[++deep[0]]=dis[u];    
    int i;    
    for(i=head[u];i!=-1;i=edge[i].next)    
    {    
        int v=edge[i].v;    
        if(v==fa||vis[v])    
            continue;    
        dis[v]=dis[u]+edge[i].w;    
        get_deep(v,u);    
    }    
}    
int get_sum(int x,int now)    
{    
    dis[x]=now;    
    deep[0]=0;    
    get_deep(x,0);    
    sort(deep+1,deep+1+deep[0]);    
    int res=0,l,r;    
    for(l=1,r=deep[0];l<r;)    
    {    
        if(deep[l]+deep[r]==k)    
        {    
            res+=(r-l); 
			l++;
		}
		else
			if(deep[l]+deep[r]<k)
			{
				l++;    
			}    
			else    
				r--;    
    }    
    return res;    
}    
void work(int x)    
{    
    ans+=get_sum(x,0);    
    vis[x]=1;    
    int i;    
    for(i=head[x];i!=-1;i=edge[i].next)    
    {    
        int v=edge[i].v;    
        if(vis[v])    
            continue;    
        ans-=get_sum(v,edge[i].w);    
        sum=son[v];    
        root=0;    
        get_root(v,root);    
        work(root);    
    }    
}    
int main()      
{       
    while(scanf("%d",&n)!=EOF,n)    
    {    
        int i;    
        cnt=0;     
        memset(head,-1,sizeof(head));    
        for(i=1;i<=n;i++)    
        {    
			int x;
            while(scanf("%d",&x)!=EOF,x)
			{
				int c;
				scanf("%d",&c);
				add(i,x,c);
				add(x,i,c);
			}
        }    
        while(scanf("%d",&k)!=EOF,k)
		{
			sum=n;    
			pre[0]=INF;    
			memset(vis,0,sizeof(vis));    
			root=0;    
			get_root(1,0);    
			ans=0;    
			work(root);    
			if(ans)
				printf("AYE\n");
			else
				printf("NAY\n");
		}
		printf(".\n");
    }    
}    



资源下载链接为: https://pan.quark.cn/s/9648a1f24758 这个HTML文件是一个专门设计的网页,适合在告白或纪念日这样的特殊时刻送给女朋友,给她带来惊喜。它通过HTML技术,将普通文字转化为富有情感和创意的表达方式,让数字媒体也能传递深情。HTML(HyperText Markup Language)是构建网页的基础语言,通过标签描述网页结构和内容,让浏览器正确展示页面。在这个特效网页中,开发者可能使用了HTML5的新特性,比如音频、视频、Canvas画布或WebGL图形,来提升视觉效果和交互体验。 原本这个文件可能是基于ASP.NET技术构建的,其扩展名是“.aspx”。ASP.NET是微软开发的一个服务器端Web应用程序框架,支持多种编程语言(如C#或VB.NET)来编写动态网页。但为了在本地直接运行,不依赖服务器,开发者将其转换为纯静态的HTML格式,只需浏览器即可打开查看。 在使用这个HTML特效页时,建议使用Internet Explorer(IE)浏览器,因为一些老的或特定的网页特效可能只在IE上表现正常,尤其是那些依赖ActiveX控件或IE特有功能的页面。不过,由于IE逐渐被淘汰,现代网页可能不再对其进行优化,因此在其他现代浏览器上运行可能会出现问题。 压缩包内的文件“yangyisen0713-7561403-biaobai(html版本)_1598430618”是经过压缩的HTML文件,可能包含图片、CSS样式表和JavaScript脚本等资源。用户需要先解压,然后在浏览器中打开HTML文件,就能看到预设的告白或纪念日特效。 这个目展示了HTML作为动态和互动内容载体的强大能力,也提醒我们,尽管技术在进步,但有时复古的方式(如使用IE浏览器)仍能唤起怀旧之情。在准备类似的个性化礼物时,掌握基本的HTML和网页制作技巧非常
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值