poj 1741(树的点分治)
Tree
Define dist(u,v)=The min distance between node u and v.
Give an integer k,for every pair (u,v) of vertices is called valid if and only if dist(u,v) not exceed k.
Write a program that will count how many pairs which are valid for a given tree.
Input
test case contains two integers n, k. (n<=10000) The following n-1
lines each contains three integers u,v,l, which means there is an edge
between node u and v of length l.
The last test case is followed by two zeros.
Output
Sample Input
5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0
Sample Output
8
树的点分治 参考了漆子超的论文 以及多位大佬的博客

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<stack>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const int N=+;
int son[N];
int ans;
int num;
int p;
int head[N];
int vis[N];
int minn;
int root;
int k;
int dis[N];
void init(){
num=;
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
}
struct node{
int to,next,w;
}edge[N];
void add(int u,int v,int w){
edge[num].to=v;
edge[num].w=w;
edge[num].next=head[u];
head[u]=num++;
}
void getroot(int x,int y,int n){
son[x]=;
int maxx=;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v==y||vis[v])continue;
getroot(v,x,n);
son[x]=son[x]+son[v];
maxx=max(maxx,son[v]);
}
maxx=max(maxx,n-son[x]);
//cout<<maxx<<" "<<x<<endl;
if(minn>maxx){
minn=maxx;
root=x;
}
}
void getdeep(int x,int v0,int y){
dis[p++]=y;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v==v0||vis[v])continue;
getdeep(v,x,y+edge[i].w);
}
}
int cal(int x,int y){
int res=;
p=;
getdeep(x,,y);
sort(dis,dis+p);
int l=;
int r=p-;
while(l<r){
if(dis[l]+dis[r]<=k){
res=res+(r-l);
l++;
}
else{
r--;
}
}
return res;
}
void work(int x){
ans=ans+cal(x,);
//cout<<cal(x,0)<<" ";
// cout<<endl;
vis[x]=;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(vis[v]==){
ans=ans-cal(v,edge[i].w);
minn=INF;
getroot(v,,son[v]);
work(root);
}
}
}
int main(){
int n;
while(scanf("%d%d",&n,&k)!=EOF){
if(n==&&k==)break;
init();
for(int i=;i<n-;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
ans=;
minn=INF;
getroot(,-,n);
//for(int i=1;i<=n;i++)cout<<son[i]<<" ";
// cout<<endl;
//cout<<root<<" "<<minn<<endl;
work(root);
cout<<ans<<endl;
}
return ;
}
poj 1741(树的点分治)的更多相关文章
- poj 1741 树的点分治(入门)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 18205 Accepted: 5951 Description ...
- POJ 1741 树的点分治
题目大意: 树上找到有多少条路径的边权值和>=k 这里在树上进行点分治,需要找到重心保证自己的不会出现过于长的链来降低复杂度 #include <cstdio> #include & ...
- POJ 1741 树分治
题目链接[http://poj.org/problem?id=1741] 题意: 给出一颗树,然后寻找点对(u,v)&&dis[u][v] < k的对数. 题解: 这是一个很经典 ...
- poj 1741 树的分治
思路:这题我是看 漆子超<分治算法在树的路径问题中的应用>写的. 附代码: #include<iostream> #include<cstring> #includ ...
- POJ 1741 Tree 树上点分治
题目链接:http://poj.org/problem?id=1741 题意: 给定一棵包含$n$个点的带边权树,求距离小于等于K的点对数量 题解: 显然,枚举所有点的子树可以获得答案,但是朴素发$O ...
- POJ 1741 Tree (点分治)
Tree Time Limit: 1000MS Memory ...
- poj 1741 Tree(点分治)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15548 Accepted: 5054 Description ...
- POJ 1741 树上 点的 分治
题意就是求树上距离小于等于K的点对有多少个 n2的算法肯定不行,因为1W个点 这就需要分治.可以看09年漆子超的论文 本题用到的是关于点的分治. 一个重要的问题是,为了防止退化,所以每次都要找到树的重 ...
- POJ 1741 树上的点分治
题目大意: 找到树上点对间距离不大于K的点对数 这是一道简单的练习点分治的题,注意的是为了防止点分治时出现最后分治出来一颗子树为一条直线,所以用递归的方法求出最合适的root点 #include &l ...
- poj 1741 Tree(树的点分治)
poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...
随机推荐
- Javascript实现导航锚点滚动效果实例
本篇文章主要介绍了Javascript实现页面滚动时导航智能定位,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 常见的开发页面中可能会有这么一个需求,页面中会有多个模块,每个模块对应一个导航,当页 ...
- 移动web——bootstrap栅格系统
基本简介 1.Bootstrap 提供了一套响应式.移动设备优先的流式栅格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列 2.栅格系统用于通过一系列的行(row)与列(c ...
- html——标签选择器
交集选择器:标签+类(ID)选择器{属性:值:}.即要满足使用了某个标签,还要满足使用了类(id)选择器. <!DOCTYPE html> <html> <head> ...
- strut2 拦截器 使用
拦截器是strut2里一个很振奋人心的应用.通过配置拦截器可以在action执行之前进行一些初始化或者是其他的操作,但是在action执行之后,返回结果就已经确定,结果是很难改变了(目前我还不知道怎么 ...
- js 不能用关键字 delete 做函数名
把delete更改为mydelete正常.
- Django - ORM实现用户登陆
1.路由分发cmdb(app)下urls.py中,建立url与函数对应关系 2.login.html代码: 3.views.py中,login函数,确认是否登陆成功 备注:从前端 获取用户名,密码,在 ...
- Bullet:Python的函数中参数是引用吗?
别的语言中关于函数有传值和传引用的区分. 关于此,流传很广的一个说法是 他们在现象的区别之一就是值传递后的变化,受到影响的就是引用,未受到影响的就是传值. 在学习中,也曾碰到过这个问题,网上关于这 ...
- 1040 有几个PAT (25 分)
题目链接:1040 有几个PAT (25 分) 做这道题目,遇到了新的困难.解决之后有了新的收获,甚是欣喜! 刚开始我用三个vector数组存储P A T三个字符出现的位置,然后三层for循环,根据字 ...
- CentOS6.8 安装python2.7,pip以及yum
由于CentOS6.8里自带的yum所依赖的python是2.6.66版本,但是安装pip至少要求python是2.7版本,因而原有的2.6并不能卸载,又得安装新的2.7.之前安装的时候强制卸载了2. ...
- bupt summer training for 16 #4 ——数论
https://vjudge.net/contest/173277#overview A.平方差公式后变为 n = (x + y)(x - y) 令 t = x - y ,变成 n = (t + 2x ...