数据结构(树,点分治):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 ...
随机推荐
- sqlserver 时间 格式化
0 或 100 (*) 默认值 mon dd yyyy hh:miAM(或 PM) 1 101 美国 mm/dd/yyyy ...
- android Services注意地方
使用service前需要在manifest声明: <manifest ... > ... <application ... > <service android:name ...
- java开发规范总结_命名规范
规范需要平时编码过程中注意,是一个慢慢养成的好习惯 1.文件 1.属性文件后缀为properties,并且符合java中i18n的规范: 2.对于各产品模块自己的配置文件必须放置在自己模块的con ...
- SGU 230. Weighings (拓扑排序)
题意: 给出质量为1~n的n个箱子的m对轻重关系,输出一种可能的箱子的质量排列. Solution: 拓扑排序,注意要处理重边. #include <iostream> #include ...
- 【BZOJ1050】【枚举+并查集】旅行comf
Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求一条路径,使得路径上最大 ...
- Excel等外部程序点击链接会带上IE信息的bug
今天碰到一个问题,在Excel内点击链接到默认浏览器Chrome打开,奇怪的是服务端收到的Session一直对不上. 查了很久发现这个Excel到Chrome的跳转竟然带上了IE的Cookie 和 U ...
- DB2JAVIT:RC=9505解决方案
DB2JAVIT:RC=9505解决方案 题记:WIN7下装DB2,启动任务中心.控制中心报DB2JAVIT:RC=9505. 解决方案:进入(计算机—>管理—>本地用户和组)把用户加入到 ...
- C#【数据库】 Excel打开到DataGridView
if (openFileDialog1.ShowDialog() == DialogResult.OK) { Filename = openFileDialog1.FileName; string s ...
- CHROME下载地址
Chrome官方独立中文安装包下载地址 一般我们安装Google Chrome浏览器都是访问 http://www.google.com/chrome/?hl=zh-CN 然后下载运行ChromeSe ...
- java学习之 垃圾回收
垃圾回收器始终以一个较低优先级的后台进程进行垃圾的回收工作,这样不会影响程序的正常工作. 通常只有当内存到达用尽的边缘而程序需要分配新的内存空间时,垃圾回收器才会执行. 垃圾回收的条件:1,垃圾回收器 ...