题目链接:http://poj.org/problem?id=1741

题意:

给定一棵包含$n$个点的带边权树,求距离小于等于K的点对数量

题解:

显然,枚举所有点的子树可以获得答案,但是朴素发$O(n^2logn)$算法会超时,

利用树的重心进行点分治可以将$O(n^2logn)$的上界优化为近似$O(nlogn)$

足以在1000ms的测试时间内通过

具体原理参考注释

#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<algorithm>
#include<set>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<climits>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define fi first
#define se second
#define mp make_pair
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define IO ios::sync_with_stdio(false)
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
#define per(ii,a,b) for(int ii=b;ii>=a;--ii)
#define forn(x,i) for(int i=head[x];i;i=e[i].next)
#define show(x) cout<<#x<<"="<<x<<endl
#define showa(a,b) cout<<#a<<'['<<b<<"]="baidu<a[b]<<endl
#define show2(x,y) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<endl
#define show3(x,y,z) cout<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
#define show4(w,x,y,z) cout<<#w<<"="<<w<<" "<<#x<<"="<<x<<" "<<#y<<"="<<y<<" "<<#z<<"="<<z<<endl
using namespace std;
const int maxn=1e6+10,maxm=2e6+10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double PI=acos(-1.0);
//head
int casn,n,m,k;
ll val[maxn],dis[maxn],ans,maxt,dfn;
int deep[maxn],vis[maxn],size[maxn];
int dp[maxn],allnode;
struct node {int to,next;ll cost;}e[maxm];int head[maxn],nume;//静态链表存图
void add(int a,int b,ll c){e[++nume]=(node){b,head[a],c};head[a]=nume;}
int mid;
void init(){//初始化
memset(head,0,sizeof head);
memset(dis,0,sizeof dis);
memset(vis,0,sizeof vis);
nume=0;
}
void getmid(int now,int pre){//dfs求树的重心
size[now]=1;//当前点为根,其子树的节点数
for(int i=head[now];i;i=e[i].next){
int to=e[i].to;
if(to==pre||vis[to]) continue;
getmid(to,now);//递归计算子树的大小
size[now]+=size[to];
}
dp[now]=max(size[now],allnode-size[now]);//dp[i]表示以i为根建立子树的时候,最大的子树大小
if(maxt>dp[now]){//maxt为最大的子树大小
maxt=dp[now];
mid=now;
}
}
void dfs(int now,int pre){//计算深度
deep[++dfn]=dis[now];
for(int i=head[now];i;i=e[i].next){
int to=e[i].to,cost=e[i].cost;
if(to==pre||vis[to]) continue;
dis[to]=dis[now]+cost;
dfs(to,now);
}
}
int cal(int rt,int len){//计算rt为根的子树中,深度之和>=k的点对数量
dis[rt]=len,dfn=0;
dfs(rt,0);//以rt为根,dfs计算其子树中所有点的深度
sort(deep+1,deep+dfn+1);
int res=0;
for(int l=1,r=dfn;l<r;){//排序后从两端向中间逼近,总复杂度nlogn
if(deep[l]+deep[r]<=k){
res+=r-l;
l++;
}else r--;
}
return res;
}
void dc(int rt){//分治以rt为根的子树
vis[rt]=1;
ans+=cal(rt,0);//初步计算以rt为根子树答案,包含重复情况
for(int i=head[rt];i;i=e[i].next){
int to=e[i].to;
if(vis[to]) continue;
ans-=cal(to,e[i].cost);//以子节点为根的子树,设置其距离下界为len,对于其子树而言,如果距离减少子树到rt的距离,依然成立的话,必然会被重复计算
allnode=size[to];
mid=0,maxt=INF;
getmid(to,rt);//寻找以rt为根的子树的重心
dc(mid);//以子树重心为树上点分治的起点,保证总复杂度为n(logn)^2级别
} }
int main() {
//#define test
#ifdef test
auto _start = chrono::high_resolution_clock::now();
freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#endif
IO;
while(cin>>n>>k,n+k){
init();
int a,b,c;
rep(i,2,n){
cin>>a>>b>>c;
add(a,b,c);
add(b,a,c);
}
mid=ans=0;
allnode=n,maxt=INF;
getmid(1,0);
dc(mid);
cout<<ans<<endl;
}
#ifdef test
auto _end = chrono::high_resolution_clock::now();
cerr << "elapsed time: " << chrono::duration<double, milli>(_end - _start).count() << " ms\n";
fclose(stdin);fclose(stdout);system("out.txt");
#endif
return 0;
}

  

POJ 1741 Tree 树上点分治的更多相关文章

  1. poj 1741 Tree(点分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15548   Accepted: 5054 Description ...

  2. POJ 1741 Tree (树分治入门)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8554   Accepted: 2545 Description ...

  3. POJ 1741 Tree (点分治)

                                                                        Tree Time Limit: 1000MS   Memory ...

  4. POJ 1741 Tree 树的分治

    原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...

  5. POJ 1741 Tree【树分治】

    第一次接触树分治,看了论文又照挑战上抄的代码,也就理解到这个层次了.. 以后做题中再慢慢体会学习. 题目链接: http://poj.org/problem?id=1741 题意: 给定树和树边的权重 ...

  6. poj 1741 Tree (树的分治)

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 30928   Accepted: 10351 Descriptio ...

  7. POJ 1741 Tree 树的分治(点分治)

    题目大意:给出一颗无根树和每条边的权值,求出树上两个点之间距离<=k的点的对数. 思路:树的点分治.利用递归和求树的重心来解决这类问题.由于满足题意的点对一共仅仅有两种: 1.在以该节点的子树中 ...

  8. POJ 1741 Tree ——(树分治)

    思路参考于:http://blog.csdn.net/yang_7_46/article/details/9966455,不再赘述. 复杂度:找树的重心然后分治复杂度为logn,每次对距离数组dep排 ...

  9. POJ 1741 Tree 求树上路径小于k的点对个数)

                                                                                                 POJ 174 ...

随机推荐

  1. SpringMvc+jQuery 文件拖拽上传、选择上传

    最近做了个简易的基于boostrap的文件上传功能,jsp版本的,后续会完善更多的功能,不过现在已经能用了,需要的小伙伴,直接引用下面的文件内容直接copy到自己的项目中就ok了,效果如图: file ...

  2. js方法参数中含有单引号双引号的处理

    最近在做项目时,遇到一个问题.当在js脚本中,拼接生成一个tr,然后添加到一个表格里. //假定testval是从后台传过来的数据 var testval = "含有'半角单引号的字符串&q ...

  3. wxpython绘制音频

    #-*- coding: utf-8 -*- ############################################################################# ...

  4. MySQL的一些基本命令笔记(1)

    关系型数据库的建模构建块: 1.数据是以行和列的形式存储数据. 2.这一系列的行和列称为表(关系) 3.表中的每一行表示一条记录(元组) 4.表中的每一列表示记录的一个属性 5.一组表组成了数据库 6 ...

  5. mini2440串口使用

    1.安装驱动CH340-USB转串口驱动,安装完成最好重启一下电脑. 2.用串口线将开发板与pc项链,并打开电源,通过电脑设备管理器查看端口(下一步要用到). 3.运行SecureCRT.exe,并建 ...

  6. luogu 4047 部落划分 二分答案

    二分距离判断是否满足k个部落,注意double类型精度,可使用不开方,最终再开 #include<bits/stdc++.h> #define rep(i,x,y) for(registe ...

  7. 唯一约束(UNIQUE_KEY)

    唯一约束可以保证记录的唯一性 唯一约束的字段可以为空值(NULL) 每张数据表可以存在多个唯一约束(主键只有一个) mysql> CREATE TABLE tb7( -> id SMALL ...

  8. python 读 json 文件

    一个汽车图标的训练集:train.json [{"items": [{"label_id": "0028", "bbox" ...

  9. JS基础题

    1.三目运算符(三元条件语句)的使用方法? 条件表达式?true表达式:false表达式 2.JS数据中哪些属于引用类型? 数组.对象 引用类型变量,变量名所储存的不是变量值,而是变量所在的地址. 3 ...

  10. VGG-16详解

    VGG16输入224*224*3的图片,经过的卷积核大小为3x3x3,stride=1,padding=1,pooling为采用2x2的max pooling方式: 1.输入224x224x3的图片, ...