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. 团队C++作业1

    我的分工 在本次作业中,我负责的是建筑类的完成还有调试运行. 建筑类的完成: 首先我认为建筑类的完成中,建筑类中应该有三个成员,水晶,防御塔,泉水. 水晶在这个里头是要被保护的对象,它的功能有回血还有 ...

  2. Leetcode题库——12.整数转罗马数字

    @author: ZZQ @software: PyCharm @file: intToRoman.py @time: 2018/9/28 21:59 要求: 字符 数值 I 1 V 5 X 10 L ...

  3. 第一次spring冲刺第7天

    讨论成员:王俊凯.王逸辉.罗凯杰.马志磊 讨论问题:进行UI设计的详细讨论,虽然结果各有争议,但最终确定了较为简单的布局页面,并且开始收集精美页面的案例 冲刺尚未结束,同志还需努力,致力于最后.

  4. 对象字典0x1005和0x1006的理解

    SYNC不一定由主站产生,因此,产生SYNC的节点,0x1005对象的值一般是0x40000080,第30位为1表示本节点产生 SYNC,而本节点的0x1006对象就是产生同步周期值了;而接收SYNC ...

  5. IPV6 国内进展情况

    国家下一代互联网产业技术创新战略联盟(以下简称“产业联盟”),近日在北京发布了我国首份IPv6业务用户体验监测报告(以下简称<报告>).该<报告>监测了我国固定宽带的IPv6普 ...

  6. 使用Fiddler重定向App的网络请求

    前言 开发中手机app只能访问生产环境地址,所以给开发调试带来不便,可以通过Fiddler的代理和urlreplace方式解决. 步骤 1.开启Fiddler的代理模式Tools->Teleri ...

  7. Iaas

    IaaS(Infrastructure as a Service),即基础设施即服务. 消费者通过Internet 可以从完善的计算机基础设施获得服务.这类服务称为基础设施即服务.基于 Interne ...

  8. JS选取DOM元素的方法

    摘自JavaScript权威指南(jQuery根据样式选择器查找元素的终极方式是 先用getElementsByTagName(*)获取所有DOM元素,然后根据样式选择器对所有DOM元素进行筛选) 今 ...

  9. BZOJ5297 CQOI2018社交网络(矩阵树定理)

    板子题. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...

  10. MT【117】立体几何里的一道分类讨论题

    评:最后用到了中间的截面三角形两边之和大于第三边.能不能构成三棱锥时考虑压扁的"降维"打击是常见的方式.