题意是说给了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. MonkeyRunner_模拟机_运行脚本

    1.打开创建好的Android模拟机  (使用AVD Manager.exe打开,或者使用cmd窗口 emulator -avd test2打开) 2.打开cmd窗口,输入monkeyrunner,然 ...

  2. Android SDK下载和更新失败的如何解决

    一.scenario: 在部署Appium环境搭建时,安装SDK Manager.exe,遇到Android SDK下载更新失败. 报错信息如下图: Failed to fetch URL https ...

  3. 一步步搭建 Spring Boot maven 框架的工程

    摘要:让Spring应用从配置到运行更加快速,演示DIY Spring Boot 框架时,如何配置端口号,如何添加日志. Spring Boot 框架帮助开发者更容易地创建基于Spring的应用程序和 ...

  4. Phone list(Trie树模板)

    Phone List 共t组数据,给定n个长度不超过10的字符串,问其中是否存在两个数S,T,使得S是T的前缀. 存在则输出NO,不存在输出YES 输入样例#1: 2 3 911 97625999 9 ...

  5. FPGA设计中的异步复位、同步释放思想

    1.一个简单的异步复位例子: module test( input clk, input rst_n, input data_in, output reg out ); always@(posedge ...

  6. import Vue form 'vue’的意思

    1.import Vue form ‘vue’ 写全的话是import Vue from ‘…/nodemouls/vue/list/vue.js’: 此时在webpack.base.conf.js中 ...

  7. wget 下载命令

    //下载单个文件 wget http://116.199.2.206:81/2Q2WADC9FB8D469200FF61C643601888142263926D08_unknown_65A3E1A23 ...

  8. MongoDB 新建数据库和集合 查询集合

    MongoDB语法是原生ORM,根本不存在sql语句 创建数据库:这里和一般的关系型数据库一样,都要先建立一个自己的数据库空间 新建数据库db1 > use db1 switched to db ...

  9. 006-优化web请求二-应用缓存、异步调用【Future、ListenableFuture、CompletableFuture】、ETag、WebSocket【SockJS、Stomp】

    四.应用缓存 使用spring应用缓存.使用方式:使用@EnableCache注解激活Spring的缓存功能,需要创建一个CacheManager来处理缓存.如使用一个内存缓存示例 package c ...

  10. 版本控制之GitHub亲手实验总结

        使用Git的基本方式是:第一种,在remote端创建一个repository,clone到本地,然后在本地把已有文件添加进来/在本地clone的目录下创建新文件,再add 提交等纳入git版本 ...