poj 2114 Boatherds (树分治)
链接: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 (树分治)的更多相关文章
- POJ 2114 Boatherds 树分治
Boatherds Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...
- 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 - Boatherds
原题地址:http://poj.org/problem?id=2114 题目大意: 给定一棵点数为\(n~(n \le 10000)\)的无根树,路径上有权值,给出m组询问($m \le 100$), ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- poj 1744 tree 树分治
Tree Time Limit: 1000MS Memory Limit: 30000K Description Give a tree with n vertices,each ed ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- POJ 2114 Boatherds【Tree,点分治】
求一棵树上是否存在路径长度为K的点对. POJ 1714求得是路径权值<=K的路径条数,这题只需要更改一下统计路径条数的函数即可,如果最终的路径条数大于零,则说明存在这样的路径. 刚开始我以为只 ...
- POJ 2114 Boatherds 划分树
标题效果:鉴于一棵树,问有两点之间没有距离是k的. 数据的多组 思维:和IOI2011的Race喜欢.不是这么简单.阅读恶心,我是在主要功能的别人的在线副本. CODE: #include <c ...
随机推荐
- odoo学习之弹框显示
按条件隐藏: <xpath expr="//group[1]" position="attributes"> <attribute name= ...
- (转)对一个deb包的解压、修改、重新打包全过程方法
转自:https://blog.csdn.net/yygydjkthh/article/details/36695243 Reference: http://www.debian.org/doc/ma ...
- [转]Linux下安装Java环境配置步骤详述
1.下载jdk8 登录网址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 选择 ...
- openhtmltopdf 支持自定义字体、粗体
一.支持自定义字体 private static void renderPDF(String html, OutputStream outputStream) throws Exception { t ...
- Python从菜鸟到高手(3):声明变量
变量(variable)是Python语言中一个非常重要的概念.变量的主要作用就是为Python程序中的某个值起一个名字.类似于"张三"."李四"." ...
- Zabbix监控系统部署:前端初始化
1. 概述 在上一篇博客<Zabbix监控系统部署:源码安装.md>中,主要进行了zabbix最新版的源码编译安装. (博客园地址:https://www.cnblogs.com/liwa ...
- slurm.conf系统初始配置
#slurm集群配置 ##集群名称 ClusterName=myslurm ##主控制器的主机名 ControlMachine=node11 ##主控制器的IP地址 ControlAddr=192.1 ...
- 这里已不再更新,访问新博客请移步 http://www.douruixin.com
这里已不再更新,访问新博客请移步 http://www.douruixin.com
- Linux内核分析第四周学习总结
朱国庆+原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 扒开系统调用的三层皮 ...
- 《Linux内核分析》 期中总结
Linux内核分析 期中总结 20135307 张嘉琪 一.Linux内核分析课程总结 学习笔记汇总 第一节 计算机是如何工作的 第二节 操作系统是如何工作的 第三节 构造一个简单的Linux系统Me ...