POJ 2114 点分治
思路:
点分治
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 10005
int n,k,xx,yy,first[N],next[N*2],v[N*2],w[N*2],tot;
int f[N],size[N],vis[N],d[N],deep[N],root,sum,ans;
void add(int x,int y,int z){w[tot]=z,v[tot]=y,next[tot]=first[x],first[x]=tot++;}
void getroot(int x,int fa){
size[x]=1,f[x]=0;
for(int i=first[x];~i;i=next[i])if(!vis[v[i]]&&v[i]!=fa)
getroot(v[i],x),size[x]+=size[v[i]],f[x]=max(f[x],size[v[i]]);
f[x]=max(f[x],sum-size[x]);
if(f[x]<f[root])root=x;
}
void getdeep(int x,int fa){
deep[++deep[0]]=d[x];
for(int i=first[x];~i;i=next[i])if(v[i]!=fa&&!vis[v[i]]&&d[x]+w[i]<=k)
d[v[i]]=d[x]+w[i],getdeep(v[i],x);
}
int calc(int x,int now){
d[x]=now,deep[0]=0,getdeep(x,0);
sort(deep+1,deep+deep[0]+1);
int t=0,l=1,r=deep[0];
while(l<r)
if(deep[l]+deep[r]<k)l++;
else if(deep[l]+deep[r]>k)r--;
else{
if(deep[l]==deep[r]){t+=(r-l+1)*(r-l)/2;break;}
int st=l,ed=r;
while(deep[st]==deep[l])st++;
while(deep[ed]==deep[r])ed--;
t+=(st-l)*(r-ed);
l=st,r=ed;
}
return t;
}
void work(int x){
ans+=calc(root,0),vis[x]=1;
for(int i=first[x];~i;i=next[i])if(!vis[v[i]])
ans-=calc(v[i],w[i]),
sum=size[v[i]],root=0,
getroot(v[i],x),work(root);
}
int read(){
int x=0;char p=getchar();
while(p<'0'||p>'9')p=getchar();
while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();
return x;
}
int main(){
while(scanf("%d",&n)&&n){
memset(first,-1,sizeof(first));tot=0;
for(int i=1;i<=n;i++)
while(xx=read())
yy=read(),add(i,xx,yy),add(xx,i,yy);
while(k=read()){
memset(vis,0,sizeof(vis));
f[0]=10005,root=ans=0,sum=n,getroot(1,0);
work(root);
puts(ans?"AYE":"NAY");
}puts(".");
}
}
POJ 2114 点分治的更多相关文章
- 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(点分治)
Boatherds Time Limit: 2000MS Memory Limit: 65536K Description Boatherds Inc. is a sailing company op ...
- POJ 2114 (点分治)
题目:https://vjudge.net/contest/307753#problem/B 题意:求树中路径和=k的点对是否存在 思路:点分治,这个题其实和上一题洛谷一样,只是这个数据强,我们不能直 ...
- 树分治 点分治poj 2114
存在2点间距离==k 输出AYE 否则输出NAY #include<stdio.h> #include<string.h> #include<algorithm> ...
- POJ 2114 Boatherds 树分治
Boatherds Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...
- poj 2114 树的分治 可作模板
/* 啊啊啊啊啊啊啊本题证明一个问题,在实际应用中sort比qsort块 还有memset这类初始化能不加尽量别加,很浪费时间 原来的程序把qsort该成sort,去掉一个无用memset就a了时间不 ...
- POJ 2114 - Boatherds
原题地址:http://poj.org/problem?id=2114 题目大意: 给定一棵点数为\(n~(n \le 10000)\)的无根树,路径上有权值,给出m组询问($m \le 100$), ...
随机推荐
- python类的继承和多态,获取对象信息
继承 类的继承机制使得子类可以继承父类中定义的方法,拥有父类的财产,比如有一个Animal的类作为父类,它有一个eat方法: class Animal(object): def __init__(se ...
- NEU2016年一月月赛回顾
月赛传送门 http://acm.neu.edu.cn/hustoj/contest.php?cid=1066 月赛已经结束十天了...才把题目补完真是大失误... 茅巨巨四天前就补完了,,,总结写得 ...
- JavaScript组成部分——ECMAScript、DOM、BOM、
1.JavaScript组成部分 虽然 JavaScript 和 ECMAScript 通常被人们用来表达相同的含义,但 JavaScript 的含义却比ECMA-262标准中规定的要多得多. 一个完 ...
- AMD cpu 下 Pytorch 多卡并行卡死问题解决
dataparallel not working on nvidia gpus and amd cpus https://github.com/pytorch/pytorch/issues/130 ...
- BZOJ 4710 容斥原理+dp
//By SiriusRen #include <cstdio> using namespace std; int n,m,a[1005]; typedef long long ll; l ...
- SVN——Jenkins自动发布
最近公司项目处于开发阶段,很多功能开发完后就需要发布到测试环境等待测试去验收,这个时候如果手动更新网站的话,是很费时费力的. 于是乎,我们做成了自动发布,这样我们只管提交代码到SVN就行了,发布由软件 ...
- IP地址转化为数字,charindex ,SUBSTRING
SET NOCOUNT ON; declare @I_PCity table ( IPStart nvarchar(), Area nvarchar(), CityID int, IPID int ) ...
- Windows 安装 MySQL8
MySQL8下载地址:https://dev.mysql.com/downloads/mysql/ 解压到安装目录 新建配置文件my.ini [mysqld]# 设置mysql的安装目录basedir ...
- 移除HTML5 input在type="search"时的清除按钮
input[type="search"]::-webkit-search-cancel-button { display: none; }
- 关于优化for循环的注意的事项
for循环注意事项: 1.for循环内部尽量少做数据库查询之类的IO代价大的操作 2.尽量控制for循环的次数,不多做无用功 3.能一次加载在内存中的,就不要通过循环来多次查询数据库,除非数据量过大. ...