poj1741 Tree(点分治)
题目链接:http://poj.org/problem?id=1741
题意:求树上两点之间距离小于等于k的点对的数量
思路:点分治模板题,推荐一篇讲的非常好的博客:https://blog.csdn.net/qq_39553725/article/details/77542223
实现代码:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define inf 0x7fffffff
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r) >> 1
const int M = 1e5+;
struct node{
int to,w,next;
}e[M<<];
int n,m;
int vis[M],dis[M],d[M],siz[M],f[M],cnt,head[M],sum,root,k,ans;
void init(){
cnt = ;
ans = ;
memset(head,,sizeof(head));
memset(vis,,sizeof(vis));
} void add(int u,int v,int w){
e[++cnt].to=v;e[cnt].w=w;e[cnt].next=head[u];head[u]=cnt;
} void get_root(int u,int fa){
siz[u] = ; f[u] = ;
for(int i = head[u];i;i=e[i].next){
int v = e[i].to;
if(v!=fa&&!vis[v]){
get_root(v,u);
siz[u] += siz[v];
f[u] = max(f[u],siz[v]);
}
}
// cout<<"sum: "<<sum-siz[v]<<endl
f[u] = max(f[u],sum - siz[u]);
if(f[u] < f[root]) root = u;
return ;
} void get_dis(int u,int fa){
dis[++dis[]] = d[u];
for(int i = head[u];i;i = e[i].next){
int v = e[i].to;
if(v != fa&& !vis[v]){
d[v] = d[u] + e[i].w;
get_dis(v,u);
}
}
return;
} int cal(int u,int c){
d[u] = c; dis[] = ;
get_dis(u,);
sort(dis+,dis+dis[]+);
int l = ,r = dis[],sum = ;
while(l < r){
if(dis[l] + dis[r] <= k){
sum+=r-l;
l ++;
}
else r--;
}
return sum;
} void solve(int u){
ans += cal(u,);
vis[u] = ;
for(int i = head[u];i;i = e[i].next){
int v = e[i].to;
if(!vis[v]){
ans -= cal(v,e[i].w);
sum = siz[v] ;
root = ;
get_root(v,);
solve(root);
}
}
}
int main()
{
int u,v,w;
ios::sync_with_stdio();
cin.tie(); cout.tie();
while(cin>>n>>k){
if(n==&&m==) break;
init();
for(int i = ;i <= n-;i ++){
cin>>u>>v>>w;
add(u,v,w); add(v,u,w);
}
f[] = inf;
sum = n;
root = ;
get_root(,);
solve(root);
cout<<ans<<endl;
}
return ;
}
poj1741 Tree(点分治)的更多相关文章
- [POJ1741]Tree(点分治)
树分治之点分治入门 所谓点分治,就是对于树针对点的分治处理 首先找出重心以保证时间复杂度 然后递归处理所有子树 对于这道题,对于点对(u,v)满足dis(u,v)<=k,分2种情况 路径过当前根 ...
- [poj1741]Tree(点分治+容斥原理)
题意:求树中点对距离<=k的无序点对个数. 解题关键:树上点分治,这个分治并没有传统分治的合并过程,只是分成各个小问题,并将各个小问题的答案相加即可,也就是每层的复杂度并不在合并的过程,是在每层 ...
- [bzoj1468][poj1741]Tree[点分治]
可以说是点分治第一题,之前那道的点分治只是模模糊糊,做完这道题感觉清楚了很多,点分治可以理解为每次树的重心(这样会把数分为若干棵子树,子树大小为log级别),然后统计包含重心的整个子树的值减去各个子树 ...
- POJ1741 Tree 树分治模板
http://poj.org/problem?id=1741 题意:一棵n个点的树,每条边有距离v,求该树中距离小于等于k的点的对数. dis[y]表示点y到根x的距离,v代表根到子树根的距离 ...
- POJ1741 Tree + BZOJ1468 Tree 【点分治】
POJ1741 Tree + BZOJ1468 Tree Description Give a tree with n vertices,each edge has a length(positive ...
- POJ1741 Tree(树分治——点分治)题解
题意:给一棵树,问你最多能找到几个组合(u,v),使得两点距离不超过k. 思路:点分治,复杂度O(nlogn*logn).看了半天还是有点模糊. 显然,所有满足要求的组合,连接这两个点,他们必然经过他 ...
- [poj1741][tree] (树/点分治)
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- POJ1741 tree 【点分治】
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25286 Accepted: 8421 Description ...
- POJ1741 Tree(树的点分治基础题)
Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v) ...
- POJ1741——Tree(树的点分治)
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013-11-17 1 ...
随机推荐
- python 爬虫--同花顺-使用代理
1.http://www.goubanjia.com/ 在上面获取 使用http协议的公网IP和端口 参考:https://blog.csdn.net/qq_23934063/article/det ...
- Loopback接口用途---用作管理地址。
Loopback接口的优点是永远不会down点,不管是链路断开还是网卡损坏.因而loopback接口有很高健壮性. 但是loopback并非实际网口,外部设备要与该口通信,必须经过实际口的路由实现. ...
- 在AspNetCore 中 使用Redis实现分布式缓存 (转载)
文章概念描述 分布式缓存描述:分布式缓存重点是在分布式上,相信大家接触过的分布式有很多中,像分布式开发,分布式部署,分布式锁.事物.系统 等有很多.使我们对分布式本身就有一个很明确的认识,分布式就是有 ...
- 【webstorm】免费使用
http://idea.imsxm.com/ 测试过ok 后期追加(20180316更新为) http://idea.codebeta.cn/ 后期追加(20180502更新为) http ...
- 大数据入门第十四天——Hbase详解(二)基本概念与命令、javaAPI
一.hbase数据模型 完整的官方文档的翻译,参考:https://www.cnblogs.com/simple-focus/p/6198329.html 1.rowkey 与nosql数据库们一样, ...
- WPF LinkButton
<Button Margin="5" Content="Test" Cursor="Hand"> <Button.Temp ...
- c# update check
public class UpdateChecker { public static event EventHandler completeCheck; private static bool isC ...
- 2017-2018-2 20155224『网络对抗技术』Exp7:网络欺诈防范
基础问题回答 问:通常在什么场景下容易受到DNS spoof攻击? 同一局域网下,以及各种公共网络. 问:在日常生活工作中如何防范以上两攻击方法? 答:DNS欺骗攻击是很难防御的,因为这种攻击大多数本 ...
- LeetCode 4Sum (Two pointers)
题意 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- Js_Eval方法
定义和用法eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. 语法eval(string) 其中参数string为必需.是要计算的字符串,其中含有要计算的 JavaScr ...