数据结构(树,点分治):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 ...
 
随机推荐
- DataTable用法
			
在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结. 一.DataTable简 ...
 - sql - 以半月,每月 分组
			
按半月:完整代码: SELECT siteNumber [站点], CONVERT(VARCHAR(7),day,120)+'-'+ case when day(day) between 1 and ...
 - mysql locktables
			
SELECT r.trx_id waiting_trx_id, r.trx_mysql_thread_id waiting_thread, TIMESTAMPDIFF( ...
 - Convention插件的使用(会涉及content目录,jsp必须放入这个下面才能映射成功基于注解的配置)
			
http://blog.csdn.net/zclandzzq/article/details/7107816
 - Deep Learning  学习随记(五)深度网络--续
			
前面记到了深度网络这一章.当时觉得练习应该挺简单的,用不了多少时间,结果训练时间真够长的...途中debug的时候还手贱的clear了一下,又得从头开始运行.不过最终还是调试成功了,sigh~ 前一篇 ...
 - 对 Xcode 菜单选项的详细探索(转)
			
转自 http://www.cnblogs.com/dsxniubility/p/4983614.html 本文调研Xcode的版本是 7.1,基本是探索了菜单的每一个按钮.虽然从xcode4一直用到 ...
 - 苹果App store 2015最新审核标准公布(2015.3)
			
苹果近日更新了AppStore审核指南的相关章节,对此前版本进行了修改和完善.除了增加应用截图.预览等限制外,使用ApplePay进行定期付款的应用程序必须展示每个阶段所需款额,费用归属以及如何取消. ...
 - 【C#枚举】根据EnumName获取Value
			
public static int GetEnumValue(Type enumType, string enumName) { try { if (!enumType.IsEnum) throw n ...
 - string相关
			
1.find相关 string s="abcd"; size_t pos0 = s.find_first_of("dcb"); 1 //返 ...
 - Oracle通过主键id删除记录很慢
			
问题描述: Oracle通过主键id删除2000条记录很慢,需要花费十二分钟. 解决过程: 1.首先查看SQL的执行计划,执行计划正常,cost只有4,用到了主键索引. 2.查看等待事件, selec ...