poj1741 树上的分治
题意是说给了n个点的树n<=10000,问有多少个点对例如(a,b)他们的之间的距离小于等于k 采用树的分治做
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn=;
int H[maxn],nx[maxn*],to[maxn*],numofE,dist[maxn*];
void add(int u, int v, int d)
{
numofE++;
dist[numofE]=d,
to[numofE]=v,
nx[numofE]=H[u],
H[u]=numofE;
}
void init(int N)
{
numofE=;
memset(H,,sizeof(H));
}
int ans;
bool center[maxn];
int subnum[maxn];
int Q[maxn+],fa[maxn];
int searchroot(int cur)
{
int rear=,root=cur;
Q[rear++]=cur;fa[cur]=;
for(int i=; i<rear; i++){
int x= Q[i];
for(int j=H[x]; j; j=nx[j])
if(to[j]!=fa[x]&& center[ to[j] ]==false)
Q[rear++]=to[j],fa[to[j]]=x;
}
int MIN=;
for(int i=rear-; i>=; i--){ int x=Q[i];
subnum[x]=;
int MA=;
for(int j=H[x]; j ; j=nx[j])
if(to[j]!=fa[x]&¢er[ to[j] ]==false)
MA=max(MA,subnum[to[j]]),subnum[x]+=subnum[to[j]];
MA=max(MA,rear-subnum[ x ]);
if(MIN>MA)MIN=MA,root=x;
}
return root;
}
int P[maxn];
int N,K;
int count_pair(int s, int t){
int ge=,R=t;
for(int i=s; i<t; i++)
{
while(R>s&&P[R-]+P[i]>K)R--;
ge+=R-s-(R>i?:);
}
return ge/;
}
void updateedg(int s, int cur, int d)
{
int rear=;
Q[rear++]=cur; fa[cur]=;P[s]=d;
for(int i=; i<rear; i++)
{
int x=Q[i];
for(int j=H[x]; j; j=nx[j])
{
int tto=to[j];
if(center[tto]||tto==fa[x])continue;
P[s+rear]=P[s+i]+dist[j],Q[rear++]=tto,fa[tto]=x;
}
}
sort(P+s,P+s+rear);
}
int dfs(int s,int cur,int d)
{
int root,tot=;
root=searchroot(cur);
center[root]=true;
for(int i=H[root]; i ; i=nx[i])
{
int tto=to[i];
if(center[tto])continue;
int n=dfs(s+tot,tto,dist[i]);
ans-=count_pair(s+tot,s+tot+n);
tot+=n;
}
P[s]=;
sort(P+s,P+s+tot);
ans+=count_pair(s,s+tot);
center[root]=false;
updateedg(s,cur,d);
return tot;
} int main()
{
memset(center,false,sizeof(center));
while(scanf("%d%d",&N,&K)==)
{
if(!N&&!K)break;
ans=;
init(N);
for(int i=; i<N; i++){
int a,b,d;
scanf("%d%d%d",&a,&b,&d);
add(a,b,d);add(b,a,d);
}
dfs(,,);
printf("%d\n",ans);
}
return ;
}
poj1741 树上的分治的更多相关文章
- poj1741 树上的点分治
题意: 一棵10000个点的树,每条边的长不超过1000,给定一个值k,问距离不超过k的点对数有多少.(多组数据) 输入样例: 5 4 1 2 3 1 3 1 1 4 2 3 5 1 0 0输出样例: ...
- codeforces 161D Distance in Tree 树上点分治
链接:https://codeforces.com/contest/161/problem/D 题意:给一个树,求距离恰好为$k$的点对是多少 题解:对于一个树,距离为$k$的点对要么经过根节点,要么 ...
- POJ 1741 Tree 树上点分治
题目链接:http://poj.org/problem?id=1741 题意: 给定一棵包含$n$个点的带边权树,求距离小于等于K的点对数量 题解: 显然,枚举所有点的子树可以获得答案,但是朴素发$O ...
- poj2114 寻找树上存在长度为k点对,树上的分治
寻找树上存在长度为k点对,树上的分治 代码和 这个 差不多 ,改一下判断的就好 #include <iostream> #include <algorithm> #inc ...
- [poj1741]Tree(点分治+容斥原理)
题意:求树中点对距离<=k的无序点对个数. 解题关键:树上点分治,这个分治并没有传统分治的合并过程,只是分成各个小问题,并将各个小问题的答案相加即可,也就是每层的复杂度并不在合并的过程,是在每层 ...
- [bzoj2599][IOI2011]Race_树上点分治
Race bzoj-2599 题目大意:询问一颗树上最短的.长度为k的链,边有边权,n个节点. 注释:$1\le n \le 2\cdot 10^5$,$1\le k \le 10^6$. 想法:树上 ...
- 树上点分治 poj 1741
Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v ...
- POJ-1741 树上分治--点分治(算法太奇妙了)
给你1e5个节点的树,(⊙﹏⊙) 你能求出又几对节点的距离小于k吗??(分治NB!) 这只是一个板子题,树上分治没有简单题呀!(一个大佬说的) #include<cstdio> #incl ...
- [POJ1741]树上的点对 树分治
Description 给一棵有n个节点的树,每条边都有一个长度(小于1001的正整数). 定义dist(u,v)=节点u到节点v的最短路距离. 给出一个整数k,我们称顶点对(u,v)是合法的当且仅当 ...
随机推荐
- 存储过程收集统计信息ORA-20000报错解决记录
存储过程如下: create or replace procedure ad.table_analyse_bill( p_BillMonth in number,--bill_month p_tail ...
- elasticsearch安装IK分词插件
一 打开网页:https://github.com/medcl/elasticsearch-analysis-ik/releases 这个是ik相关的包,找到你想下载的版本,下载对应的zip包 二 然 ...
- java JDBC (五) properties配置文件
1.在src目录下创建文件 database.properties driver = com.mysql.jdbc.Driver url = jdbc:mysql://192.168.0.207:33 ...
- redis分布式锁(转)
add by zhj: 如果不考虑键的删除,而是让他过期后自动失效,那用set就可以实现锁了 原文:http://www.cnblogs.com/yjf512/archive/2017/03/22/6 ...
- 前端 chrome查看html样式基本操作
Chrome浏览器是世界上HTML5支持最好的浏览器.提供了非常好的开发工具,非常适合我们开发人员使用.审查元素功能的快捷键是F12. 按检查或者F12 鼠标点击找标签 chorme审查元素的使用 - ...
- 前端 HTML body标签相关内容 常用标签 表单标签 form里面的 input标签介绍
input标签用于接收用户输入.可以利用input 可以做登录页面 input标签是行内块标签 <input> 元素会根据不同的 type 属性,变化为多种形态. name属性:表单点击提 ...
- 用xtrabackup2.4备份mysql5.6.30一直显示log scanned up to
用xtrabackup2.4备份mysql5.6.30一直显示log scanned up to 一直处于log scanned up to,只能ctrl+c强制终止备份 没有大事务 停止xtraba ...
- 20180820 SQL 提示Error: String or binary data would be truncated
Error: String or binary data would be truncated,错误,是因为栏位给出的长度不够,增加初始化长度就可以了. 除了创建表的增加长度情况,还有一种是,SELE ...
- 008-spring cache-缓存实现-03-springboot redis实现
1.window下redis安装 https://www.cnblogs.com/bjlhx/p/7429811.html 2.pom <!-- 缓存 --> <dependency ...
- .Net 学习过程
1.C#面向过程编程. 2.C#面向对象基础. 3.WPF教程. 4.SQL语句. 5.ADO.Net. 6.HTML.JavaScript.Dom. 7.ASP.Net.