Poj 2114 Boatherds(点分治)
Boatherds
Time Limit: 2000MS Memory Limit: 65536K
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
CTU Open 2004
/*
点分治.
求满足两点之间距离为k的点对个数.
改改二分就好了(调了半个下午.
注意有dis重复的.
*/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#define MAXN 10001
using namespace std;
int n,m,root,k,f[MAXN],dis[MAXN],c[MAXN],ans,tot,a[MAXN],sum,cut,head[MAXN],size[MAXN],s[MAXN];
bool b[MAXN];
struct edge{int v,x,next;}e[MAXN*101];
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*f;
}
void add(int u,int v,int x)
{
e[++cut].v=v;e[cut].next=head[u];e[cut].x=x;head[u]=cut;
}
void get_root(int u,int fa)
{
size[u]=1;f[u]=0;
for(int i=head[u];i;i=e[i].next)
{
if(e[i].v==fa||b[e[i].v]) continue;
get_root(e[i].v,u);
size[u]+=size[e[i].v];
f[u]=max(f[u],size[e[i].v]);
}
f[u]=max(f[u],sum-size[u]);
if(f[u]<f[root]) root=u;
return ;
}
void get_dis(int u,int fa)
{
s[++tot]=dis[u];
for(int i=head[u];i;i=e[i].next)
{
if(e[i].v==fa||b[e[i].v]) continue;
dis[e[i].v]=dis[u]+e[i].x;
get_dis(e[i].v,u);
}
return ;
}
void erfen(int l,int r,int z)
{
int total=0;
sort(s+1,s+tot+1);s[0]=1e9;
for(int i=1;i<=tot;i++) c[i]=0;
for(int i=1;i<=tot;i++)
if(s[i]==s[i-1]) c[total]++;
else a[++total]=s[i],c[total]++;
r=total;
while(l<=r)
{
if(a[l]+a[l]==k) ans+=z*(c[l]*(c[l]-1))/2;
if(a[r]+a[r]==k) ans+=z*(c[r]*(c[r]-1))/2;
if(l==r) break;
if(a[l]+a[r]==k) ans+=z*c[l]*c[r],l++;
else if(a[l]+a[r]<k) l++;
else r--;
}
return ;
}
void Go(int u,int d,int flag)
{
dis[u]=d;tot=0;
get_dis(u,u);
erfen(1,tot,flag);
return ;
}
void slove(int u)
{
b[u]=true;Go(u,0,1);
for(int i=head[u];i;i=e[i].next)
{
if(b[e[i].v]) continue;
Go(e[i].v,e[i].x,-1);
root=0;sum=size[e[i].v];
get_root(e[i].v,root);
slove(root);
}
return ;
}
void Clear()
{
memset(size,0,sizeof size);
memset(b,0,sizeof b);
root=tot=0;
}
int main()
{
int x,y,z;
while(scanf("%d",&n),n)
{
memset(head,0,sizeof head);
memset(f,0,sizeof f);
cut=0;
for(int i=1;i<=n;i++)
{
while(scanf("%d",&x),x)
{
scanf("%d",&y);
add(i,x,y),add(x,i,y);
}
}
while(scanf("%d",&k),k)
{
Clear();ans=0;
f[0]=1e9;sum=n;
get_root(1,root);
slove(root);
if(ans) puts("AYE");
else puts("NAY");
}
printf(".\n");
}
return 0;
}
Poj 2114 Boatherds(点分治)的更多相关文章
- POJ 2114 Boatherds 树分治
Boatherds Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...
- poj 2114 Boatherds (树分治)
链接:http://poj.org/problem?id=2114 题意: 求树上距离为k的点对数量: 思路: 点分治.. 实现代码: #include<iostream> #includ ...
- POJ 2114 Boatherds【Tree,点分治】
求一棵树上是否存在路径长度为K的点对. POJ 1714求得是路径权值<=K的路径条数,这题只需要更改一下统计路径条数的函数即可,如果最终的路径条数大于零,则说明存在这样的路径. 刚开始我以为只 ...
- poj 2114 Boatherds 树的分治
还是利用点的分治的办法来做,统计的办法不一样了,我的做法是排序并且标记每个点属于哪颗子树. #include <iostream> #include <cstdio> #inc ...
- POJ 2114 - Boatherds
原题地址:http://poj.org/problem?id=2114 题目大意: 给定一棵点数为\(n~(n \le 10000)\)的无根树,路径上有权值,给出m组询问($m \le 100$), ...
- POJ 2114 (点分治)
题目:https://vjudge.net/contest/307753#problem/B 题意:求树中路径和=k的点对是否存在 思路:点分治,这个题其实和上一题洛谷一样,只是这个数据强,我们不能直 ...
- POJ 2114 Boatherds 划分树
标题效果:鉴于一棵树,问有两点之间没有距离是k的. 数据的多组 思维:和IOI2011的Race喜欢.不是这么简单.阅读恶心,我是在主要功能的别人的在线副本. CODE: #include <c ...
- poj 2114 树的分治 可作模板
/* 啊啊啊啊啊啊啊本题证明一个问题,在实际应用中sort比qsort块 还有memset这类初始化能不加尽量别加,很浪费时间 原来的程序把qsort该成sort,去掉一个无用memset就a了时间不 ...
- POJ 2114 点分治
思路: 点分治 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> u ...
随机推荐
- Mybatis配置、逆向工程自动生成代码(CRUD案例)
目的: mybatis简介 搭建mybatis环境 基于SSM逆向工程的使用 Mybatis增删改查案例 mybatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及 ...
- Linux mount/unmount 挂载和卸载指令
对于Linux用户来讲,不论有几个分区,分别分给哪一个目录使用,它总归就是一个根目录.一个独立且唯一的文件结构 Linux中每个分区都是用来组成整个文件系统的一部分,她在用一种叫做“挂载”的处理方法, ...
- Java定时任务工具详解之Timer篇
Java定时任务调度工具详解 什么是定时任务调度? ◆ 基于给定的时间点,给定的时间间隔或者给定的执行次数自动执行的任务. 在Java中的定时调度工具? ◆ Timer ◆Quartz T ...
- ESlint 格式化代码 备忘
vscode 代码格式化配置 vscode 菜单 文件->首选项->设置 --->进入扩展查找到ESlint,点击任一选项中的[在setting.json中配置],复制以下代码 { ...
- 图片上传怎么用File接受文件
xl_echo编辑整理,欢迎转载,转载请声明文章来源.欢迎添加echo微信(微信号:t2421499075)交流学习. 百战不败,依不自称常胜,百败不颓,依能奋力前行.——这才是真正的堪称强大!! - ...
- 【转载】 C#通过File类实现文件拷贝复制的功能
在Windows系统的使用过程中,一个较常使用的功能就是文件的复制拷贝操作,其实在C#开发中,也可以使用File类库中的Copy方法来实现文件的拷贝,支持设定原文件地址,以及拷贝复制后的文件存放路径. ...
- 【转】HTTP响应状态码参考簿
HTTP响应状态码参考簿 http状态返回代码 1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码. http状态返回代码 代码 说明100 (继续) 请求者应当继续提出请求. ...
- EntityFramework进阶(二)- DbContext预热
本系列原创博客代码已在EntityFramework6.0.0测试通过,转载请标明出处 在DbContext首次调用的时候,会很慢,甚至会有5,6秒的等待,通常称为冷查询.再次调用的时候,几毫秒就能请 ...
- Android NDK 学习之调用Java函数
本博客主要是在Ubuntu 下开发,且默认你已经安装了Eclipse,Android SDK, Android NDK, CDT插件. 在Eclipse中添加配置NDK,路径如下Eclipse-> ...
- pip3升级问题
输入命令sudo pip3 install --upgrade pip 升级完成之后执行pip命令会报错,错误信息如下: File "/usr/bin/pip3", line 9, ...