还是利用点的分治的办法来做,统计的办法不一样了,我的做法是排序并且标记每个点属于哪颗子树。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e4+9;
int head[maxn],lon,n,mm,m;
struct
{
int next,to,w;
}e[maxn<<1];
void edgeini()
{
memset(head,-1,sizeof(head));
lon=0;
}
void edgemake(int from,int to,int w)
{
e[++lon].to=to;
e[lon].w=w;
e[lon].next=head[from];
head[from]=lon;
} int son[maxn],maxson[maxn];
bool use[maxn];
int dp(int t,int from)
{
int ans,tmp,now=1e4+9;
son[t]=maxson[t]=0;
for(int k=head[t];k!=-1;k=e[k].next)
{
int u=e[k].to;
if(u==from||use[u]) continue;
tmp=dp(u,t);
if(maxson[tmp]<now)
{
now=maxson[tmp];
ans=tmp;
}
son[t]+=son[u];
maxson[t]=max(son[u],maxson[t]);
}
son[t]++;
maxson[t]=max(maxson[t],mm-son[t]);
if(maxson[t]<now) ans=t;
return ans;
}
int d[maxn],top;
struct D
{
int data,ss;
bool operator <(const D & xx) const
{
return data<xx.data;
}
}a[maxn];
void dfs(int t,int from)
{
a[++top].data=d[t];
son[t]=0;
for(int k=head[t];k!=-1;k=e[k].next)
{
int u=e[k].to;
if(u==from||use[u]) continue;
d[u]=d[t]+e[k].w;
dfs(u,t);
son[t]+=son[u];
}
son[t]++;
} int ss[maxn];
int cal(int l,int r)
{
int ret=0;
for(;l<r;)
{
while(r>l&&a[l].data+a[r].data>m) r--;
while(r>l&&a[l].data+a[r].data<m) l++;
if(a[l].data+a[r].data==m)
{
int ll=a[l].ss,rr=a[r].ss;
if(ll!=rr) return true;
while(l<r&&a[l+1].data==a[l].data)
{
if(a[l+1].ss!=ll) return true;
l++;
}
while(l<r&&a[r-1].data==a[r].data)
{
if(a[r-1].ss!=rr) return true;
r--;
}
l++;
r--;
}
}
return false;
} bool solve(int t)
{
int now=dp(t,0);
use[now]=1;
d[now]=top=0;
dfs(now,0);
int tmp=2,con=0;
for(int k=head[now];k!=-1;k=e[k].next)
{
int u=e[k].to;
if(use[u]) continue;
con++;
for(int i=tmp;i<tmp+son[u];i++)
a[i].ss=con;
tmp+=son[u];
}
sort(a+1,a+tmp);
a[1].ss=0;
int ret=cal(1,tmp-1);
if(ret) return true;
for(int k=head[now];k!=-1;k=e[k].next)
{
int u=e[k].to;
if(use[u]) continue;
mm=son[u];
if(solve(u)) return true;
}
return false;
} int main()
{
// freopen("in.txt","r",stdin);
while(scanf("%d", &n)!=EOF&& n)
{
edgeini();
for(int i=1,to,w;i<=n;i++)
{
while(scanf("%d",&to),to)
{
scanf("%d",&w);
edgemake(i,to,w);
edgemake(to,i,w);
}
}
int tmp;
while(scanf("%d",&tmp)!=EOF&&tmp)
{
m=tmp;
mm=n;
memset(use,0,sizeof(use));
if(solve(1))
printf("AYE\n");
else
printf("NAY\n");
}
printf(".\n");
}
return 0;
}

poj 2114 Boatherds 树的分治的更多相关文章

  1. POJ 2114 Boatherds 树分治

    Boatherds     Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...

  2. POJ 1741 Tree 树的分治

    原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...

  3. poj 2114 Boatherds (树分治)

    链接:http://poj.org/problem?id=2114 题意: 求树上距离为k的点对数量: 思路: 点分治.. 实现代码: #include<iostream> #includ ...

  4. POJ 2114 Boatherds【Tree,点分治】

    求一棵树上是否存在路径长度为K的点对. POJ 1714求得是路径权值<=K的路径条数,这题只需要更改一下统计路径条数的函数即可,如果最终的路径条数大于零,则说明存在这样的路径. 刚开始我以为只 ...

  5. Poj 2114 Boatherds(点分治)

    Boatherds Time Limit: 2000MS Memory Limit: 65536K Description Boatherds Inc. is a sailing company op ...

  6. POJ 2114 Boatherds 划分树

    标题效果:鉴于一棵树,问有两点之间没有距离是k的. 数据的多组 思维:和IOI2011的Race喜欢.不是这么简单.阅读恶心,我是在主要功能的别人的在线副本. CODE: #include <c ...

  7. POJ 2114 - Boatherds

    原题地址:http://poj.org/problem?id=2114 题目大意: 给定一棵点数为\(n~(n \le 10000)\)的无根树,路径上有权值,给出m组询问($m \le 100$), ...

  8. POJ 1741 Tree 树的分治(点分治)

    题目大意:给出一颗无根树和每条边的权值,求出树上两个点之间距离<=k的点的对数. 思路:树的点分治.利用递归和求树的重心来解决这类问题.由于满足题意的点对一共仅仅有两种: 1.在以该节点的子树中 ...

  9. poj 1741 Tree (树的分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 30928   Accepted: 10351 Descriptio ...

随机推荐

  1. addChildViewController ipad 中Controller的嵌套和叠加

    1.addChildViewController 在 base controller中添加子的controller,会自动调用子的controller中viewDidload,viewWillAppe ...

  2. 再探java基础——break和continue的用法

    再探java基础——break和continue的用法 break break可用于循环和switch...case...语句中. 用于switch...case中: 执行完满足case条件的内容内后 ...

  3. HDU 1090 A+B for Input-Output Practice (II)

    #include <cstdio> int main() { int n,a,b; scanf("%d",&n); ; i<=n; i++) { scan ...

  4. Android 全屏方法

    我大概不想赘述什么其他方法,我就说一下我已知在用的方法QAQ requestWindowFeature(Window.FEATURE_NO_TITLE); 设置程序无标题栏 getWindow().s ...

  5. UIMenuController在label中的使用

    要想在label中使用 必须是继承于label的分类 ////  MYlabel.m//  MenuController////  Created by 张明 on 16/3/8.//  Copyri ...

  6. 开源html5_kiwijs_helloworld

    本次须要的下载文件已经共享出来 网盘地址 由于我使用的是黑苹果系统, window我就无视了. 开发工具使用 网盘里的 dmg :Sublime Text 打开开发工具后在helloworld中找到 ...

  7. openstack安装配置

    openstack:1.控制节点安装所有,计算节点只有nova-compute:2.网络选择: nova-network还是neutron: nova-network比较简单, neutron功能强大 ...

  8. OS之多线程

    os中引入进程的目的是,为了描述和实现多个程序的并发执行,以改善资源利用率及提高系统的吞吐量. 为什么要引入线程?这是为了减少程序并发执行时系统所付出的额外开销(堆栈切换的开销等),使os具有更好的并 ...

  9. 关于Ubuntu远程ssh连接失败的问题

    在做机器人项目的时候,用的是Ubuntu的linux,跟之前的CentOS的操作命令有一点差别,就比如防火墙的名字,在Ubuntu系统中叫什么ufw,真是有点不好接受. 为了能模拟环境,我又弄了一台电 ...

  10. 玩2k16

    2k是我最喜欢的游戏啦,平时无聊了都会搞两盘.现在到2k16了,玩游戏时碰到一些麻烦,记录便查. 我哥一直说他的电脑玩2K16非常卡,根本玩不了,但是他的电脑配置可比我的高啊,我玩起溜溜地,喊他把配置 ...