数据结构(树,点分治):POJ 1741 Tree
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
点分治模板……
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int cnt,n,k,N;
bool vis[maxn];
int fir[maxn],to[maxn<<],nxt[maxn<<],val[maxn<<];
void addedge(int a,int b,int v){
nxt[++cnt]=fir[a];fir[a]=cnt;val[cnt]=v;to[cnt]=b;
} int rt,sz[maxn],son[maxn];
int st[maxn],tot,dis[maxn];
void Get_RT(int x,int fa){
sz[x]=;son[x]=;
for(int i=fir[x];i;i=nxt[i])
if(to[i]!=fa&&!vis[to[i]]){
Get_RT(to[i],x);
sz[x]+=sz[to[i]];
son[x]=max(sz[to[i]],son[x]);
}
son[x]=max(son[x],N-sz[x]);
if(!rt||son[rt]>son[x])rt=x;
} void DFS(int x,int fa){
st[++tot]=dis[x];
for(int i=fir[x];i;i=nxt[i])
if(to[i]!=fa&&!vis[to[i]]){
dis[to[i]]=dis[x]+val[i];
DFS(to[i],x);
}
} int Calc(int x,int d){
int ret=;tot=;
dis[x]=d;DFS(x,);
sort(st+,st+tot+);
int l=,r=tot;
while(l<r){
if(st[l]+st[r]>k)r-=;
else {ret+=r-l;l+=;}
}
return ret;
} int Solve(int x){
vis[x]=true;
int ret=Calc(x,);
for(int i=fir[x];i;i=nxt[i])
if(!vis[to[i]]){
ret-=Calc(to[i],val[i]);
N=sz[to[i]];rt=;
Get_RT(to[i],);
ret+=Solve(rt);
}
return ret;
} int main(){
while(true){
scanf("%d%d",&n,&k);
if(!n&&!k)break;cnt=;N=n;
memset(vis,,sizeof(vis));
memset(fir,,sizeof(fir));
for(int i=,a,b,v;i<n;i++){
scanf("%d%d%d",&a,&b,&v);
addedge(a,b,v);addedge(b,a,v);
}
Get_RT(,);
printf("%d\n",Solve(rt));
}
return ;
}
数据结构(树,点分治):POJ 1741 Tree的更多相关文章
- 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 ...
- 点分治——POJ 1741
写的第一道点分治的题目,权当认识点分治了. 点分治,就是对每条过某个点的路径进行考虑,若路径不经过此点,则可以对其子树进行考虑. 具体可以看menci的blog:点分治 来看一道例题:POJ 1741 ...
- POJ 1741 Tree (树分治入门)
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8554 Accepted: 2545 Description ...
- POJ 1741 Tree 树的分治
原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...
- POJ 1741 Tree 树形DP(分治)
链接:id=1741">http://poj.org/problem?id=1741 题意:给出一棵树,节点数为N(N<=10000),给出N-1条边的两点和权值,给出数值k,问 ...
- POJ - 1741 - Tree - 点分治 模板
POJ-1741 题意: 对于带权的一棵树,求树中距离不超过k的点的对数. 思路: 点分治的裸题. 将这棵树分成很多小的树,分治求解. #include <algorithm> #incl ...
- POJ 1741.Tree 树分治 树形dp 树上点对
Tree Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 24258 Accepted: 8062 Description ...
随机推荐
- iOS UIKit:view
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...
- cocos2dx Hello world 创建
环境搭建好后,就要开始创建自己的第一个hello world项目了 因为没有安装其他的插件,所以最开始只能手动创建 首先通过cmd 进入你的cocos2dx的路径下: D:\soft\cocos2d- ...
- 怎样在thinkphp里面执行原生的sql语句
$Model = new Model(); $sql = "select * from `order`"; $voList = $Model->query($sql); 只是 ...
- Junit4_单元测试
不多说,直接练习学习. 1.将Junit4单元测试包引入项目:项目右键——“属性”,选择“Java Build Path”,然后到右上选择“Libraries”标签,之后在最右边点击“Add Libr ...
- mac 下maven的安装
最近在学习mahout,这些安装相关软件的步骤先记下来,避免以后忘记. 1.首先在mac上查看本机的java版本,如果没有需要自己去安装: 我的电脑上安装的java是1.7.0_79 2.在http: ...
- C++ Reference 的“三位一体”诠释
C++ 是介于汇编语言与高级语言之间的一种“全能”语言.它的能力是其他任何基于VMA(冯-诺曼架构)计算机的高级程序设计语言无法望其项背的,而性能也只有C语言可与之伯仲. 然而长期以来,喜欢C++和憎 ...
- printf 格式化最常用用法
printf 操作符的参数包括”格式字符串“及”要输出的数据列表". 格式字符串好像用来填空的模版,代表你想要的输出格式: printf "Hello,%s;your passwo ...
- Linux常用命令大全(2)
系统信息arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS / ...
- 如何将eclipse里的项目发布到github
首先,给eclipse安装上EGit 在“Help > Install new software”中添加 http://download.eclipse.org/egit/updates 两个都 ...
- 除法(Division ,UVA 725)-ACM集训
参考:http://www.cnblogs.com/xiaobaibuhei/p/3301110.html 算法学到很弱,连这么简单个问题都难到我了.但我偏不信这个邪,终于做出来了.不过,是参照别人的 ...