题目链接: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. CM记录-部署cdh5.3.3集群

    1.安装操作系统,保证联网环境,本文以CentOS 6.8为操作系统(略) 2.wget下载安装包(以5.3.3为例) #mkdir /usr/cdh ---新建cm安装目录 #cd /usr/cdh ...

  2. Nullable<DateTime> DateTime? 格式转换问题 tostring()

    解决方案: DateTime? dt2 = DateTime.Now; dt2.GetValueOrDefault().ToString("yyyy-MM-dd"); 控制器代码: ...

  3. Excel列名序号互转

    public static int ToIndex(string columnName) { if (!Regex.IsMatch(columnName.ToUpper(), @"[A-Z] ...

  4. 15.scrapy模拟登陆案例

    1.案例一 a.创建项目 scrapy startproject renren_login 进入项目路径 scrapy genspider renren "renren.com" ...

  5. position:fixed 失效

    如果position:fixed 失效,那应该是你设置position的元素的父元素中有设置了transform属性哦 . 把 transform删除或设置为none就可以解决啦. 但是如果trans ...

  6. 【python小练】0012题

    第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好 ...

  7. Webstorm添加新建.vue文件功能并支持高亮vue语法和es6语法

    转载:https://blog.csdn.net/qq_33008701/article/details/56486893 Webstorm 添加新建.vue文件功能并支持高亮vue语法和es6语法 ...

  8. CSS强制英文、中文换行与不换行

    .p1{ word-break:break-all; width:150px;}/*只对英文起作用,以字母作为换行依据*/ .p2{ word-wrap:break-word; width:150px ...

  9. NLTK1及NLP理论基础

    以下为Aron老师课程笔记 一.NLTK安装 1. 安装nltk https://pypi.python.org/pypi/nltk 把nltk-3.0.0解压到D:\Anacond3目录 打开cmd ...

  10. python练习题1

    1.使用while循环打印输入 1 2 3 4 5 6 8 9 10 num = 1 while num <= 10: if num == 7: num = num + 1 continue e ...