POJ P1741 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.
sort(dis+,dis+num+);
r=num;
for(l=;l<=num,l<r;l++){
while(dis[l]+dis[r]>k&&l<r)
r--;
ans+=r-l;
}
可以看出查找效率主要在排序上,反正很快;
但发现当两点在同一子树中且dis(i)+dis(j)≤k时,他们被计入答案,但他们的最短路径甚至不经过该重心;
于是把这些点用相似的方式查出再减去即可;
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct ss{
int next,to,w;
};
ss e[];
int first[],num;
int n,k,ans,size[],max_size[],vis[],dis[];
inline void Init();
inline void build(int ,int ,int );
inline void part_dfs(int );
inline void dfs_size(int ,int );
inline int dfs_root(int ,int ,int );
inline int chan_ans(int ,int );
inline void dfs_len(int ,int ,int );
inline void in(int &ans)
{
ans=;bool p=false;char ch=getchar();
while((ch>'' || ch<'')&&ch!='-') ch=getchar(); if(ch=='-') p=true,ch=getchar();
while(ch<=''&&ch>='') ans=ans*+ch-'',ch=getchar(); if(p) ans=-ans;
}
int main()
{
int i,j,u,v,w;
while(scanf("%d%d",&n,&k)==){
if(n==)
return ;
Init();
for(i=;i<=n-;i++){
in(u),in(v),in(w);
build(u,v,w);build(v,u,w);
}
part_dfs();
printf("%d\n",ans);
}
}
inline void Init(){
memset(vis,,sizeof(vis));
memset(first,,sizeof(first));
ans=;num=;
}
inline void build(int f,int t,int wi){
e[++num].next=first[f];first[f]=num;
e[num].to=t;e[num].w=wi;
}
inline void part_dfs(int now){
int root,i;
dfs_size(now,);
root=dfs_root(now,,now);
ans+=chan_ans(root,);
vis[root]=;
for(i=first[root];i;i=e[i].next)
if(!vis[e[i].to]){
ans-=chan_ans(e[i].to,e[i].w);
part_dfs(e[i].to);
}
}
inline void dfs_size(int now,int fa){
size[now]=;
for(int i=first[now];i;i=e[i].next)
if(!vis[e[i].to]&&e[i].to!=fa){
dfs_size(e[i].to,now);
size[now]+=size[e[i].to];
}
}
inline int dfs_root(int now,int fa,int r){
int i,root=-,wroot;
max_size[now]=size[r]-size[now];
for(i=first[now];i;i=e[i].next)
if(!vis[e[i].to]&&e[i].to!=fa){
if(size[e[i].to]>max_size[now])
max_size[now]=size[e[i].to];
wroot=dfs_root(e[i].to,now,r);
if(max_size[wroot]<max_size[root]||root==-)
root=wroot;
}
if(max_size[now]<max_size[root]||root==-)
root=now;
return root;
}
inline int chan_ans(int root,int dis1){
int l,r,ans=;
num=;
dfs_len(root,dis1,);
sort(dis+,dis+num+);
r=num;
for(l=;l<=num,l<r;l++){
while(dis[l]+dis[r]>k&&l<r)r--;
ans+=r-l;
}
return ans;
}
inline void dfs_len(int now,int d,int fa){
dis[++num]=d;
for(int i=first[now];i;i=e[i].next)
if(!vis[e[i].to]&&e[i].to!=fa)
dfs_len(e[i].to,d+e[i].w,now);
}
//点分治:
//dfs分治{
//对每个分支dfs(两遍)重心
//以重心为根dfs统计合法路径个数
//}分治重心的子树结构
//dis:一个不具有顺序的点到当前结构的重心的距离表
//vis[i]标记(染色)已到过的重心
//size[i]
//max_size[i]记录某点的最大子树大小;
//由于基于每个分支的dfs之间是跳着的(因为现在的重心未必是前重心的子节点),所以需要vis顺便截断某次dfs,防止跑出分支
祝AC
POJ P1741 Tree 解题报告的更多相关文章
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【LeetCode】623. Add One Row to Tree 解题报告(Python)
[LeetCode]623. Add One Row to Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problem ...
- POJ 2054 Color a Tree解题报告
题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a spe ...
随机推荐
- delphi 10.2 ----memo 的例子 实现基本记事本功能
unit Unit2; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- python web开发学习笔记一:javascript基础
一.认识js: 能进入到软件所实习是我的最大的收获,也是我的荣幸,我相信努力付出一定能够换回收获. 项目最先开始的是接触到web前端的一些内容,我们需要利用flask搭建应该有的框架.我有一些pyth ...
- leetcode-686-Repeated String Match(重复多少次A能够找到B)
题目描述: Given two strings A and B, find the minimum number of times A has to be repeated such that B i ...
- Hibernate查询方式&抓取策略
Hibernate的查询方式 1.OID查询 hibernate根据对象的OID(主键)进行检索 使用get方法 Customer customer=session.get(Customer.clas ...
- Java中HashMap的hash分布策略的简单解释
趴源码是看到一段不可思议的代码,网上的解释似乎不大令人满意,因此稍微花点时间解读了一下,如有错误请指正 HashMap的桶是这样搞的 // 片段1 static final int hash(Obje ...
- SVN无法读取cruuent修复方法
解决方法:在网上百度和google了一大圈之后,终于得知是断电时current和txn-current文件没有写入当前最新版本号和最新版本的路径问题 当时非常抓狂,项目刷新一直为空. 1.先把curr ...
- PowerDesigner16 生成的备注脚本,在sql server 2008 中报“对象名 'sysproperties' 无效”的错误的解决办法
主要是在建模时我们对表.列增加了些说明注释,而Sql2005之后系统表sysproperties已废弃删除而改用sys.extended_properties所致. 1.修改Table TableCo ...
- linux MD5 SHA1 等 文件校验方法
为解决官方发布的软件包被别人更改或者软件在传输过程中出现传输错误等问题,软件官方在提供软件包的同时,还提供一个保存MD5校验码的文件. Linux/unix中可以使用 md5sum 文件名 sha1s ...
- Android Studio: /dev/kvm device permission denied
https://stackoverflow.com/questions/37300811/android-studio-dev-kvm-device-permission-denied To chec ...
- 微服务Kong(三)——添加一个API
在开始前,请确保您已经安装了KONG服务,并且已经启动了KONG服务. 在本节中,您可以学习到:如何在KONG层添加一个API.这是您使用KONG来管理您的API的第一步.对于此篇教程,我们将使用 h ...