POJ 1741 Tree (点分治)
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 20816 | Accepted: 6820 |
Description
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
input contains several test cases. The first line of each 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
Source
给一棵边带权树,问两点之间的距离小于等于K的点对有多少个。
将无根树转化成有根树进行观察。满足条件的点对有两种情况:两个点的路径横跨树根,两个点位于同一颗子树中。
如果我们已经知道了此时所有点到根的距离a[i],a[x] + a[y] <= k的(x, y)对数就是结果,这个可以通过排序之后O(n)的复杂度求出。然后根据分治的思想,分别对所有的儿子求一遍即可,但是这会出现重复的——当前情况下两个点位于一颗子树中,那么应该将其减掉(显然这两个点是满足题意的,为什么减掉呢?因为在对子树进行求解的时候,会重新计算)。
在进行分治时,为了避免树退化成一条链而导致时间复杂度变为O(N^2),每次都找树的重心,这样,所有的子树规模就会变的很小了。时间复杂度O(Nlog^2N)。
树的重心的算法可以线性求解。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)
using namespace std;
typedef long long ll;
const int N=1e5+;
const int M=1e6+;
int n,m,k,tot,size,root,ans;
int head[N],s[N],f[N],d[N];
bool done[N];
vector<int>dep;
struct man{
int to,next,l;
}edg[N*];
void add(int u,int v,int l){
edg[tot].to=v;edg[tot].l=l;edg[tot].next=head[u];head[u]=tot++;
}
void getroot(int u,int fa){
s[u]=;f[u]=;
for(int i=head[u];i!=-;i=edg[i].next){
int v=edg[i].to;
if(v!=fa&&!done[v]){
getroot(v,u);
s[u]+=s[v];
f[u]=max(f[u],s[v]);
}
}
f[u]=max(f[u],size-s[u]);
if(f[u]<f[root])root=u;
}
void getdep(int u,int fa){
dep.push_back(d[u]);
s[u]=;
for(int i=head[u];i!=-;i=edg[i].next){
int v=edg[i].to;
if(v!=fa&&!done[v]){
d[v]=d[u]+edg[i].l;
getdep(v,u);
s[u]+=s[v];
}
}
}
int calc(int u,int init){
dep.clear();
d[u]=init;
getdep(u,);
sort(dep.begin(),dep.end());
int ret=;
for(int l=,r=dep.size()-;l<r; ){
if(dep[l]+dep[r]<=k)ret+=r-l++;
else r--;
}
return ret;
}
void work(int u){
ans+=calc(u,);
done[u]=true;
for(int i=head[u];i!=-;i=edg[i].next){
int v=edg[i].to;
if(!done[v]){
ans-=calc(v,edg[i].l);
f[]=size=s[v];
getroot(v,root=);
work(root);
}
}
}
int main(){
while(~scanf("%d%d",&n,&k)&&n&&k){
tot=ans=;f[]=size=n;
met(head,-);met(done,false);
int u,v,l;
for(int i=;i<n;i++){
scanf("%d%d%d",&u,&v,&l);
add(u,v,l);add(v,u,l);
}
getroot(,root=);
work(root);
printf("%d\n",ans);
}
return ;
}
POJ 1741 Tree (点分治)的更多相关文章
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
- POJ 1741 Tree 树分治
Tree Description Give a tree with n vertices,each edge has a length(positive integer less than 1 ...
- [bzoj 1468][poj 1741]Tree [点分治]
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- POJ 1741 Tree(点分治点对<=k)
Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...
- POJ 1741 Tree ——点分治
[题目分析] 这貌似是做过第三道以Tree命名的题目了. 听说树分治的代码都很长,一直吓得不敢写,有生之年终于切掉这题. 点分治模板题目.自己YY了好久才写出来. 然后1A了,开心o(* ̄▽ ̄*)ブ ...
- [poj 1741]Tree 点分治
题意 求树上距离不超过k的点对数,边权<=1000 题解 点分治. 点分治的思想就是取一个树的重心,这种路径只有两种情况,就是经过和不经过这个重心,如果不经过重心就把树剖开递归处 ...
- POJ - 1741 - Tree - 点分治 模板
POJ-1741 题意: 对于带权的一棵树,求树中距离不超过k的点的对数. 思路: 点分治的裸题. 将这棵树分成很多小的树,分治求解. #include <algorithm> #incl ...
- poj 1741 Tree(树的点分治)
poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...
- POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量
POJ 1741. Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 34141 Accepted: 11420 ...
- POJ 1741 Tree 求树上路径小于k的点对个数)
POJ 174 ...
随机推荐
- BZOJ2818 GCD 【莫比乌斯反演】
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MB Submit: 6826 Solved: 3013 [Submit][Status][Discuss ...
- wya费用流
#include<bits/stdc++.h> using namespace std; #define M 1005 #define inf 0x7fffffff #define T 6 ...
- 手把手教你通过Eclipse工程配置调用JNI完全攻略
本文地址:http://www.cnblogs.com/wavky/p/JNI.html 当你找到并鬼使神差地打开这个博文的时候,我敢肯定你已经知道什么是JNI,基本概念就不粘贴了. 百度出来的JNI ...
- Vue根据URL传参来控制全局 console.log 的开关
如果你的项目中console.log了很多信息,但是发到生产环境上又不想打印这些信息,这时候就需要设置一个全局变量,如:debug, 用正则匹配一下参数: const getQueryStr = (n ...
- bulk_insert_buffer_size and InnoDB
Q: I read the following on this page http://dev.mysql.com/doc/mysql/en/server-system-variables.html ...
- 怎么把linux的磁盘映射到windows上
步骤如下: 右击如下的computer: 然后选择:Map network drive... 然后在下图按图中所示操作: 最后成功如下图所示:
- [04] css 选择器
1.元素选择器 常见的html标签元素 h1 { color: red; } body { background: red; } 2.分组选择器 例如body和h2标签的字体颜色都是red,使用逗号将 ...
- [洛谷P1528] 切蛋糕
洛谷题目链接:切蛋糕 题目描述 Facer今天买了n块蛋糕,不料被信息组中球球等好吃懒做的家伙发现了,没办法,只好浪费一点来填他们的嘴巴.他答应给每个人留一口,然后量了量每个人口的大小.Facer有把 ...
- 【poj3734】矩阵乘法
题解: 若当前有i个格子.2个是偶数的方案数为a[i]1个是偶数的方案数为b[i]0个是偶数的方案数为c[i] a[i+1]=2*a[i](i+1染成黄或蓝)+b[i](把奇数变为偶数)b[i+1]= ...
- 【洛谷 SP2878】Knights of the Round Table(双联通分量)
先放这吧,没时间写,明天再补 "明天到了" 题目链接 题意:求不在任何奇环内的点的数量. Tarjan求点双联通分量,然后再染色判断是不是二分图就好了. 只是不懂为什么Tarjan ...