链接:http://poj.org/problem?id=2114

题意:

求树上距离为k的点对数量;

思路:

点分治。。

实现代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define inf 0x7fffffff
const int M = 1e5+; struct node{
int to,next,w;
}e[M<<]; int n,m;
int vis[M],dis[M],d[M],siz[M],f[M],cnt,head[M],sum,root,k,ans; void init(){
cnt = ;
ans = ;
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
} void add(int u,int v,int w){
e[++cnt].to = v;e[cnt].w = w;e[cnt].next = head[u];head[u] = cnt;
} void get_root(int u,int fa){
siz[u] = ; f[u] = ;
for(int i = head[u];i;i=e[i].next){
int v = e[i].to;
if(v != fa&&!vis[v]){
get_root(v,u);
siz[u] += siz[v];
f[u] = max(f[u],siz[u]);
}
}
f[u] = max(f[u],sum - siz[u]);
if(f[u] < f[root]) root = u;
return ;
} void get_dis(int u,int fa){
if(d[u] <= k) dis[++dis[]] = d[u];
for(int i = head[u];i;i=e[i].next){
int v = e[i].to;
if(v != fa&&!vis[v]){
d[v] = d[u] + e[i].w;
get_dis(v,u);
}
}
return ;
} int cal(int u,int c){
d[u] = c; dis[] = ;
get_dis(u,);
sort(dis+,dis+dis[]+);
int l = ,r = dis[],ans = ;
while(l < r){
if(dis[l] + dis[r] < k) l++;
else if(dis[l] + dis[r] > k) r--;
else{
if(dis[l] == dis[r]){
ans += (r-l+)*(r-l)/;
break;
}
else {
int i = l,j = r;
while(dis[i] == dis[l]) i++;
while(dis[j] == dis[r]) j--;
ans += (i-l)*(r-j);
l = i; r = j;
}
}
}
return ans;
} void solve(int u){
ans += cal(u,);
vis[u] = ;
for(int i = head[u];i;i=e[i].next){
int v = e[i].to;
if(vis[v]) continue;
ans -= cal(v,e[i].w);
sum = siz[v];
root = ;
get_root(v,);
solve(root);
}
} int main()
{
int u,v,w;
while(scanf("%d",&n)&&n){
init();
for(int i = ;i <= n;i ++){
int x,v;
while(scanf("%d",&x)&&x){
scanf("%d",&v);
add(i,x,v); add(x,i,v);
}
}
int x;
while(scanf("%d",&x)&&x){
memset(vis,,sizeof(vis));
k = x; ans = root = ;
sum = n; f[] = inf;
get_root(,);
solve(root);
if(ans) printf("AYE\n");
else printf("NAY\n");
}
printf(".\n");
}
}

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

  1. POJ 2114 Boatherds 树分治

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

  2. poj 2114 Boatherds 树的分治

    还是利用点的分治的办法来做,统计的办法不一样了,我的做法是排序并且标记每个点属于哪颗子树. #include <iostream> #include <cstdio> #inc ...

  3. Poj 2114 Boatherds(点分治)

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

  4. POJ 2114 - Boatherds

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

  5. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

  6. poj 1744 tree 树分治

    Tree Time Limit: 1000MS   Memory Limit: 30000K       Description Give a tree with n vertices,each ed ...

  7. POJ 1741 Tree 树分治

    Tree     Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...

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

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

  9. POJ 2114 Boatherds 划分树

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

随机推荐

  1. php计算utf8字符串长度

    strlen()函数计算中文字符不太友好.扩展的mb_strlen()函数可以补充这个.如果没有这个扩展,也可以利用正则匹配分解. 函数如下: // 对utf-8字符的长度 function utf8 ...

  2. Linux shell ftp命令下载文件 根据文件日期

    需求:ftp获取远程数据的文件,根据文件的创建时间点下载文件. 可以自行扩展根据文件的大小等其他需求. 知识点总结: 1.获取文件的时间: ls -lrt|awk '{print $6" & ...

  3. 使用Quartz实现定时任务

    一:Quertz的用途 Quertz是一个开源的作业任务调度框架,他可以完成像JavaScript定时器类式的功能,其实Java中Timer也可实现部分功能,但相比Quertz还是略逊一筹,本人这次需 ...

  4. WPF中TreeView.BringIntoView方法的替代方案

    原文:WPF中TreeView.BringIntoView方法的替代方案 WPF中TreeView.BringIntoView方法的替代方案 周银辉 WPF中TreeView.BringIntoVie ...

  5. MySQL调优基础, 与hikari数据库连接池配合

    1.根据硬件配置系统参数 wait_timeout  非交互连接的最大存活时间, 10-30min max_connections   全局最大连接数 默认100 根据情况调整 back_log   ...

  6. 批量实现多台服务器之间ssh无密码登录的相互信任关系

    最近IDC上架了一批hadoop大数据业务服务器,由于集群环境需要在这些服务器之间实现ssh无密码登录的相互信任关系.具体的实现思路:在其中的任一台服务器上通过"ssh-keygen -t ...

  7. Linux运维笔记-日常操作命令总结(1)

    在linux日常运维中,我们平时会用到很多常规的操作命令. 查看服务器的外网ip [root@redis-new01 ~]# curl ifconfig.me [root@redis-new01 ~] ...

  8. “耐撕团队”部署并测试onezero团队记帐本项目

    耐撕团队 对onezero团队记帐本项目的部署并测试 测试指标参见下面给出的博客: http://www.ltesting.net/ceshi/ceshijishu/xncs/2014/1030/20 ...

  9. VS2015安装及单元测试

    今天跟大家分享一下我的VS2015的安装过程以及对单元测试的操作步骤.VS2015是一款非常好用的编程软件,内容很多很广泛,是深受欢迎的一款软件,较之于VC++6.0有着一些好处,对VC6.0++来说 ...

  10. 20135337——实践一:Linux基础配置

    一.配置系统,权限中简单梳理遇到的问题 1.Ubuntu中root和普通用户相互切换 1.从user用户切换到root用户 执行:sudo su 2.从root用户切回user用户 执行:su use ...