题意是说给了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]&&center[ 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 树上的分治的更多相关文章

  1. poj1741 树上的点分治

    题意: 一棵10000个点的树,每条边的长不超过1000,给定一个值k,问距离不超过k的点对数有多少.(多组数据) 输入样例: 5 4 1 2 3 1 3 1 1 4 2 3 5 1 0 0输出样例: ...

  2. codeforces 161D Distance in Tree 树上点分治

    链接:https://codeforces.com/contest/161/problem/D 题意:给一个树,求距离恰好为$k$的点对是多少 题解:对于一个树,距离为$k$的点对要么经过根节点,要么 ...

  3. POJ 1741 Tree 树上点分治

    题目链接:http://poj.org/problem?id=1741 题意: 给定一棵包含$n$个点的带边权树,求距离小于等于K的点对数量 题解: 显然,枚举所有点的子树可以获得答案,但是朴素发$O ...

  4. poj2114 寻找树上存在长度为k点对,树上的分治

    寻找树上存在长度为k点对,树上的分治  代码和  这个  差不多 ,改一下判断的就好 #include <iostream> #include <algorithm> #inc ...

  5. [poj1741]Tree(点分治+容斥原理)

    题意:求树中点对距离<=k的无序点对个数. 解题关键:树上点分治,这个分治并没有传统分治的合并过程,只是分成各个小问题,并将各个小问题的答案相加即可,也就是每层的复杂度并不在合并的过程,是在每层 ...

  6. [bzoj2599][IOI2011]Race_树上点分治

    Race bzoj-2599 题目大意:询问一颗树上最短的.长度为k的链,边有边权,n个节点. 注释:$1\le n \le 2\cdot 10^5$,$1\le k \le 10^6$. 想法:树上 ...

  7. 树上点分治 poj 1741

    Give a tree with n vertices,each edge has a length(positive integer less than 1001). Define dist(u,v ...

  8. POJ-1741 树上分治--点分治(算法太奇妙了)

    给你1e5个节点的树,(⊙﹏⊙) 你能求出又几对节点的距离小于k吗??(分治NB!) 这只是一个板子题,树上分治没有简单题呀!(一个大佬说的) #include<cstdio> #incl ...

  9. [POJ1741]树上的点对 树分治

    Description 给一棵有n个节点的树,每条边都有一个长度(小于1001的正整数). 定义dist(u,v)=节点u到节点v的最短路距离. 给出一个整数k,我们称顶点对(u,v)是合法的当且仅当 ...

随机推荐

  1. shiro默认过滤器

  2. CF720A Closing ceremony 贪心

    正解:贪心 解题报告: 传送门! 先考虑如果只有一列怎么搞?那就肯定是尽量走到最远的地方 然后用点儿类似的思想,现在考虑有两列的情况QAQ 为了方便表述,这里给每个位置两个值,a表示离一号入口的距离, ...

  3. 自定义指令(v-check、v-focus)的方法有哪些?它有哪些钩子函数?还有哪些钩子函数参数?

    全局定义指令:在vue对象的directive方法里面有两个参数,一个是指令名称,另一个是函数.组件内定义指令:directives: 钩子函数:bind(绑定事件触发).inserted(节点插入的 ...

  4. dedecms清空所有文章怎么操作?sql语句如何写?

    小C新建了一个站,确切的说是复制,出于seo考虑,决定清空所有文章,那么dedecms清空所有文章怎么操作?sql语句如何写呢?特别提醒:修改之前一定要先做好备份,以防万一!下面的语句在迫不得已的情况 ...

  5. windows无法安装到这个磁盘。选中的磁盘采用GPT分区形式 Windows 检测到 EFI 系统分区格式化为 NTFS。将 EFI 系统分区个数化为 FAT32,然后重新启动安装

    win10安装问题解决 问题 1.windows无法安装到这个磁盘.选中的磁盘采用GPT分区形式 解决方法: 1.bios,更改 uefi/legacy boot 为ueei only 后面可以安装了 ...

  6. 前端ps部分

    一. 使用ps前的一些设置工作 1.首选项的设置 编剧-首选项-单位与标尺-[标尺/文字]改为像素 2.面板的设置 窗口--仅保留常用的[F7图层].[F8信息].[历史记录]. 3.视图的设置 视图 ...

  7. LigerUi自动检索输入

    var availableTags = [ "ActionScript", "AppleScript", "Asp", "BASI ...

  8. ArcGIS工具备忘

    1.Repair Geometry (Data Management) 几何图形修复,比如面图层不满足节点坐标逆时针 2.Raster Domain (3D Analyst) 获取栅格范围 3.Int ...

  9. [转]C++编译链接过程详解

    C语言的编译链接过程要把我们编写的一个c程序(源代码)转换成可以在硬件上运行的程序(可执行代码),需要进行编译和链接.编译就是把文本形式源代码翻译为机器语言形式的目标文件的过程.链接是把目标文件.操作 ...

  10. (转)区块链共识机制分析——论PoW,PoS,DPos和DAG的优缺点

    近期,随着区块链技术在社区中的声音越来越大,业界已经开始从技术角度对区块链进行全方位的解读.作为第一批区块链技术的实现,传统比特币与以太坊在共识机制.存储机制.智能合约机制.跨链通讯机制等领域并没有非 ...