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)是合法的当且仅当 ...
随机推荐
- 转:Java中子类是否可以继承父类的static变量和方法而呈现多态特性
原文地址:Java中子类是否可以继承父类的static变量和方法而呈现多态特性 静态方法 通常,在一个类中定义一个方法为static,那就是说,无需本类的对象即可调用此方法,关于static方法,声明 ...
- day3_字符串常用方法
s.upper()s.lower()s.capitalize()s.split(',')s.strip('abc')s.lstrip()s.rstrip()s.replace('old','new') ...
- RequireJs的理解
什么是RequireJs RequireJS 是一个JavaScript模块加载器. 在ES6出现之前,JS不像其他语言同样拥有“模块”这一概念,于是为了支持JS模块化,出现了各种各样的语言工具,如w ...
- MySQL+InnoDB semi-consitent read原理及实现分析(转)
add by zhj: 主要讲的是在MySQL在Repeatable Read和Read Committed级别下,加锁时的不同,在Read Committed隔离级别下,只对where 中满足条件的 ...
- 按键控制led驱动
内核版本:linux2.6.22.6 硬件平台:JZ2440 驱动源码 key_drv.c : #include<linux/module.h> #include<linux/ker ...
- vue获取地址栏传过来的参数VS原生js获取地址栏的参数
Vue的方式 Vue的 query方式 ①this.$route.query.companyId ( companyId 为参数的名称 是$route 不是 $router) Vue的 params方 ...
- SQL Server2008及以上 表分区操作详解
SQL Server 表分区之水平表分区 转自:https://www.cnblogs.com/Brambling/p/6766482.html 什么是表分区? 表分区分为水平表分区和垂直表分区,水 ...
- mysql实时增量备份
采用binlog日志的好处 掌控所有更改操作,必要时可用于恢复数据 数据库主从复制的必要条件 [root@localhost~]# vim /etc/my.cnf [mysqld] .. .. log ...
- Tomcat7目录结构详解
1.bin:该目录下存放的是二进制可执行文件,如果是安装版,那么这个目录下会有两个exe文件:tomcat6.exe.tomcat6w.exe,前者是在控制台下启动Tomcat,后者是弹出UGI窗口启 ...
- C++中类的前向声明
概念 可以声明一个类而不是定义它; class Screen; 这个声明被称为"前向声明".在声明之后,定义之前,类screen是一个不完全类型,即已知Screen是一个类型,但不 ...