POJ 1741 Tree

Description

Give a tree with n vertices,each edge has a length(positive integer less than 1001). 
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

The 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

For each test case output the answer on a single line.

Sample Input

5 4
1 2 3
1 3 1
1 4 2
3 5 1
0 0

Sample Output

8
题目大意:有一颗由n个点组成的树,问树上两点间距离小于等于k的点对有多少对
输入:多组数据输入。每组数据第1行n,k,接下来n-1行,u,v,l表示点u与点v之间有一条长为l的边
输出:点对个数
基本算法:点分治
点分治,本质还是分治算法
对于一棵树,简单的递归搜索的复杂度,呵呵~~,
所以为了降低复杂度,通俗点儿说就是将一棵树拆开
一棵树的复杂度之所以高,是因为它有可能很深,
所以拆要使拆开后的几棵树最深的最小
那么选取的这个点就是树的重心
树的重心通俗点儿说就是删除重心后最大的连通块最小
找出重心后,树上的点的路径就可以分为
经过重心的 和 不过重心的
对于经过重心的,
1、统计出过重心的所有点的满足条件的数目=ans1
2、对于每棵子树,统计一遍自己内部满足条件的数目=ans2
ans=ans1-所有的ans2
对于不经过重心的,继续递归
本人点分治理解不深,对点分治更详细的解读 推荐博客:http://www.cnblogs.com/chty/p/5912360.html对于文章中出现的错误,欢迎各位指正
代码中数组含义:head[],链表  son[i]=j,以i为根的所有子树总共有j个节点(包括i)    
     f[i]=j以i为根的所有子树中,最大的一颗子树有j个节点(不包括i)
sum,当前计算的树或子树的点的个数
     d[i]=j,点i到当前所选的根节点距离为j deep[],d数组的汇总
代码中函数作用:getroot,找重心 getdeep,统计点之间的距离 cal,统计满足条件的点对数目
部分代码细节:
getroot函数:son[x]=1,因为son包含自己 f[x]=0,因为f可能存有上一次的结果
f[x]=max(f[x],sum-son[x]);①解释了为什么son包含自己,sum是总点数,son[x]是除临时指定的父节点所在子树的子树节点总数,相减就是临时父节点所在子树节点总数
因为父节点是临时指定的,所以也有可能成为x的孩子节点,所以父节点所在子树也作为x的一颗子树 ②在>2个点时,保证不让叶子节点成为重心
work函数:root=0 && main函数 f[0]=inf 这两个互相照应,删除选定的根之后,让根=0,因为f[0]=inf,这样在getroot函数里才保证了f[x]<f[root],更新root
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 10010
#define inf 20001
using namespace std;
int n,k,cnt,head[N],son[N],f[N],sum,ans,root,d[N],deep[N];
bool vis[N];
struct node
{
int next,to,w;
}e[*N];
inline void add(int u,int v,int w)
{
e[++cnt].to=v;e[cnt].w=w;e[cnt].next=head[u];head[u]=cnt;
e[++cnt].to=u;e[cnt].w=w;e[cnt].next=head[v];head[v]=cnt;
}
inline void pre()
{
memset(head,,sizeof(head));
memset(vis,false,sizeof(vis));
ans=;cnt=;root=;
}
inline void getroot(int x,int fa)
{
son[x]=;f[x]=;
for(int i=head[x];i;i=e[i].next)
{
if(e[i].to==fa||vis[e[i].to]) continue;
getroot(e[i].to,x);
son[x]+=son[e[i].to];
f[x]=max(f[x],son[e[i].to]);
}
f[x]=max(f[x],sum-son[x]);
if(f[x]<f[root]) root=x;
}
inline void getdeep(int x,int fa)
{
deep[++deep[]]=d[x];
for(int i=head[x];i;i=e[i].next)
{
if(e[i].to==fa||vis[e[i].to]) continue;
d[e[i].to]=d[x]+e[i].w;
getdeep(e[i].to,x);
}
}
inline int cal(int x,int p)
{
d[x]=p;deep[]=;
getdeep(x,);
sort(deep+,deep+deep[]+);
int t=,l,r;
for(l=,r=deep[];l<r;)
{
if(deep[l]+deep[r]<=k) {t+=r-l;l++;}
else r--;
}
return t;
}
inline void work(int x)
{
ans+=cal(x,);
vis[x]=true;
for(int i=head[x];i;i=e[i].next)
{
if(vis[e[i].to]) continue;
ans-=cal(e[i].to,e[i].w);
sum=son[e[i].to];
root=;
getroot(e[i].to,);
work(root);
}
}
int main()
{
while()
{
scanf("%d%d",&n,&k);
if(!n) return ;
pre();
int u,v,w;
for(int i=;i<n;i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
sum=n;f[]=inf;
getroot(,);
work(root);
printf("%d\n",ans);
}
}
加的是无向边,链表忘了开双倍,RE。。。。。。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
int n,K,cnt,sum,ans,root;
int head[],deep[],d[],f[],son[];
bool vis[];
struct data{int to,next,v;}e[];
inline int read()
{
int x=;char c=getchar();
while(c<''||c>'') c=getchar();
while(c>=''&&c<='') {x=x*+c-'';c=getchar();}
return x;
}
inline void insert(int u,int v,int w)
{
e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;e[cnt].v=w;
e[++cnt].to=u;e[cnt].next=head[v];head[v]=cnt;e[cnt].v=w;
}
//最小递归层数 why联通的节点数量最小? 不是层数?
inline void getroot(int x,int fa)
{
son[x]=;f[x]=;//son:以x为根的子树的节点个数,包括自己
//f[x]=0 不能删 因为f[x]可能存有上一次的结果
for(int i=head[x];i;i=e[i].next)
{
if(e[i].to==fa||vis[e[i].to]) continue; //vis=true表示节点已删除
getroot(e[i].to,x);
son[x]+=son[e[i].to];
f[x]=max(f[x],son[e[i].to]);
}
f[x]=max(f[x],sum-son[x]);
//树本有根,点分治重新找根,所以以x为根的子树除了已递归到的,还有以父节点为根的子树,这也是son[x]=1的原因
if(f[x]<f[root]) root=x;//找到的根满足它的最大子树最小
}
inline void getdeep(int x,int fa)
{
deep[++deep[]]=d[x];//deep[0]总的节点数,deep 每个点到根节点的距离
for(int i=head[x];i;i=e[i].next)
{
if(e[i].to==fa||vis[e[i].to]) continue;
d[e[i].to]=d[x]+e[i].v;
getdeep(e[i].to,x);
}
}
inline int cal(int x,int now)//now初始为0
{
d[x]=now;deep[]=;//d是长度
getdeep(x,);//得到以x为根的子树中,每个点到x的距离
sort(deep+,deep+deep[]+);
int t=,l,r;
for(l=,r=deep[];l<r;)
{
if(deep[l]+deep[r]<=K) {t+=r-l;l++;}
else r--;
}
return t;
}
inline void work(int x)//x是确定的根
{
ans+=cal(x,);
vis[x]=;
for(int i=head[x];i;i=e[i].next)
{
if(vis[e[i].to]) continue;
ans-=cal(e[i].to,e[i].v);
sum=son[e[i].to];
root=;//删除原根节点后,重新找根节点,f【0】=inf
getroot(e[i].to,root);
work(root);
}
}
int main()
{
while()
{
ans=,root=,cnt=;
memset(vis,,sizeof(vis));
memset(head,,sizeof(head));
n=read();K=read();
if(!n) return ;
for(int i=;i<n;i++)
{
int u=read(),v=read(),w=read();
insert(u,v,w);
}
sum=n;f[]=inf;//sum:用sum-节点已统计的子树节点个数=以节点临时父节点为根的子树节点个数
//f 记录以x为根的最大的子树的大小,最后从f中取最小值
//f[0]=inf 不能删 因为每次getroot 更新root根据f[x]是否小于f[root],每次删除一个点root=0
getroot(,);//找第一个根 ,临时从第1个点开始找
work(root);
printf("%d\n",ans);
}
}

学习时打的注释


POJ 1741 Tree 求树上路径小于k的点对个数)的更多相关文章

  1. POJ 1741 Tree(点分治点对<=k)

    Description Give a tree with n vertices,each edge has a length(positive integer less than 1001). Def ...

  2. POJ 1741 [点分治][树上路径问题]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给一棵有n个节点的树,每条边都有一个正权值,求一共有多少个点对使得它们之间路的权值和小于给定的k. 思路: <分治算法在树的路径问题中的应用 ...

  3. [TS-A1505] [清橙2013中国国家集训队第二次作业] 树 [可持久化线段树,求树上路径第k大]

    按Dfs序逐个插入点,建立可持久化线段树,每次查询即可,具体详见代码. 不知道为什么,代码慢的要死,, #include <iostream> #include <algorithm ...

  4. POJ - 1741 Tree

    DescriptionGive a tree with n vertices,each edge has a length(positive integer less than 1001).Defin ...

  5. POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<=K的点对数量

    POJ 1741. Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 34141   Accepted: 11420 ...

  6. POJ - 3415 Common Substrings(后缀数组求长度不小于 k 的公共子串的个数+单调栈优化)

    Description A substring of a string T is defined as: T( i, k)= TiTi+1... Ti+k-1, 1≤ i≤ i+k-1≤| T|. G ...

  7. POJ1741--Tree (树的点分治) 求树上距离小于等于k的点对数

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12276   Accepted: 3886 Description ...

  8. poj 1741 Tree(树的点分治)

    poj 1741 Tree(树的点分治) 给出一个n个结点的树和一个整数k,问有多少个距离不超过k的点对. 首先对于一个树中的点对,要么经过根结点,要么不经过.所以我们可以把经过根节点的符合点对统计出 ...

  9. POJ 1741.Tree 树分治 树形dp 树上点对

    Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 24258   Accepted: 8062 Description ...

随机推荐

  1. 软工 · 第十二次作业 - Beta答辩总结

    福大软工 · 第十二次作业 - Beta答辩总结 写第十二次的时候操作失误直接在Beta版本的博客里改了...第七次冲刺的作业链接补在这里 Beta(7/7) 组长本次博客作业链接 项目宣传视频链接 ...

  2. Internet History, Technology and Security (Week4)

    Week4. We are now moving into Week 4! This week, we will be covering commercialization and growth. T ...

  3. cobbler技术详解(是PXE二次详解)

    Cobbler是PXE的二次封装,使用Python语言开发, 可以用来快速建立 Linux 网络安装环境,它已将 Linux 网络安装的技术门槛,从大专以上文化水平,成功降低到初中以下,连补鞋匠都能学 ...

  4. 二级制包安装Tomcat 与 RPM包安装Tomcat

    1:下载二级制包 wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.0.47/bin/apache-tomcat-8.0.47.tar.g ...

  5. yum与rmp

    清理一切缓存[root@geust02 ~]# yum   clean  all  重建元数据[root@geust02 ~]# yum  makecache 查询vim相关的软件包[root@geu ...

  6. Excel中用REPT函数制作图表

    本文从以下七个方面,阐述在Excel中用REPT函数制作图表: 一. 图形效果展示 二. REPT语法解释 三. REPT制作条形图 四. REPT制作漏斗图 五. REPT制作蝴蝶图 六. REPT ...

  7. [Cnbeta]BAT财报对比

    https://www.cnbeta.com/articles/tech/789123.htm 随着腾讯上周公布财报,BAT三家2018年第三季度的数据均已公布,曾经与腾讯.阿里齐名的百度正被拉开越来 ...

  8. .NET4.0 加密通讯协议下TLS 的兼容支持问题.

    Study From https://stackoverflow.com/questions/28286086/default-securityprotocol-in-net-4-5 The defa ...

  9. PHP中的PEAR是什么?

    PEAR也就是为PHP扩展与应用库(PHP Extension and Application Repository),它是一个PHP扩展及应用的一个代码仓库. 补充:php中扩展pecl与pear ...

  10. 【uoj#37/bzoj3812】[清华集训2014]主旋律 状压dp+容斥原理

    题目描述 求一张有向图的强连通生成子图的数目对 $10^9+7$ 取模的结果. 题解 状压dp+容斥原理 设 $f[i]$ 表示点集 $i$ 强连通生成子图的数目,容易想到使用总方案数 $2^{sum ...