[BZOJ2051]A Problem For Fun/[BZOJ2117]Crash的旅游计划/[BZOJ4317]Atm的树
[BZOJ2051]A Problem For Fun/[BZOJ2117]Crash的旅游计划/[BZOJ4317]Atm的树
题目大意:
给出一个\(n(n\le10^5)\)个结点的树,每条边有一个正整数权值\(w_i(w_i\le10^4)\),定义两个结点的距离为连接这两个结点路径上边权的和。对于每个结点\(i\),它到其他\(n-1\)个结点都有一个距离,将这些距离从小到大排序,输出第\(k\)个距离。
思路:
重心剖分预处理每个重心管辖范围内从重心出发能够组成的所有距离。询问时二分答案\(k\),再对于每一级重心二分出\(<k\)的距离的个数。注意去重。
时间复杂度\(\mathcal O(n\log^3n)\)。
源代码:
#include<cstdio>
#include<cctype>
#include<vector>
#include<climits>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=1e5+1;
struct Graph {
struct Edge {
int to,w;
};
std::vector<Edge> e[N];
inline void add_edge(const int &u,const int &v,const int &w) {
e[u].push_back((Edge){v,w});
}
};
Graph g1,g2;
bool mark[N];
std::vector<int> v[N],v2[N];
int n,m,size[N],max[N],root,all;
void get_root(const int &x) {
max[x]=0;
size[x]=1;
mark[x]=true;
for(unsigned i=0;i<g1.e[x].size();i++) {
const int &y=g1.e[x][i].to;
if(mark[y]) continue;
get_root(y);
size[x]+=size[y];
max[x]=std::max(max[x],size[y]);
}
max[x]=std::max(max[x],all-size[x]);
if(max[x]<max[root]) root=x;
mark[x]=false;
}
inline void get_root(const int &x,const int &size) {
root=0;
all=size;
get_root(x);
}
void dfs(const int &x,const int &par,const int &dis,std::vector<int> &v,const bool type) {
if(!type) g2.add_edge(x,root,dis);
v.push_back(dis);
for(unsigned i=0;i<g1.e[x].size();i++) {
const int &y=g1.e[x][i].to;
if(y==par||mark[y]) continue;
dfs(y,x,dis+g1.e[x][i].w,v,type);
}
}
void solve(const int x) {
mark[x]=true;
dfs(x,0,0,v[x],0);
std::sort(v[x].begin(),v[x].end());
for(unsigned i=0;i<g1.e[x].size();i++) {
const int &y=g1.e[x][i].to;
if(mark[y]) continue;
get_root(y,size[y]);
dfs(y,x,g1.e[x][i].w,v2[root],1);
std::sort(v2[root].begin(),v2[root].end());
solve(root);
}
}
inline int ord(int x,int l) {
int ret=0;
for(register unsigned i=0;i<g2.e[x].size();i++) {
const int &y=g2.e[x][i].to;
ret+=std::lower_bound(v[y].begin(),v[y].end(),l-g2.e[x][i].w)-v[y].begin();
if(i+1!=g2.e[x].size()) ret-=std::lower_bound(v2[y].begin(),v2[y].end(),l-g2.e[x][i+1].w)-v2[y].begin();
}
return ret;
}
int main() {
max[0]=INT_MAX;
n=getint(),m=getint();
for(register int i=1;i<n;i++) {
const int u=getint(),v=getint(),w=getint();
g1.add_edge(u,v,w);
g1.add_edge(v,u,w);
}
get_root(1,n);
solve(root);
for(register int i=1;i<=n;i++) {
std::reverse(g2.e[i].begin(),g2.e[i].end());
}
for(register int i=1;i<=n;i++) {
int l=1,r=(n-1)*10000;
while(l<=r) {
const int mid=(l+r)>>1;
if(ord(i,mid)<=m) {
l=mid+1;
} else {
r=mid-1;
}
}
printf("%d\n",l-1);
}
return 0;
}
[BZOJ2051]A Problem For Fun/[BZOJ2117]Crash的旅游计划/[BZOJ4317]Atm的树的更多相关文章
- [BZOJ2117]Crash的旅游计划
Description 眼看着假期就要到了,Crash由于长期切题而感到无聊了,因此他决定利用这个假期和好友陶陶一起出去旅游. Crash和陶陶所要去的城市里有N (N > 1) 个景点,Cra ...
- 【BZOJ2117】 [2010国家集训队]Crash的旅游计划
[BZOJ2117] [2010国家集训队]Crash的旅游计划 Description 眼看着假期就要到了,Crash由于长期切题而感到无聊了,因此他决定利用这个假期和好友陶陶一起出去旅游. Cra ...
- BZOJ4317Atm的树&BZOJ2051A Problem For Fun&BZOJ2117[2010国家集训队]Crash的旅游计划——二分答案+动态点分治(点分树套线段树/点分树+vector)
题目描述 Atm有一段时间在虐qtree的题目,于是,他满脑子都是tree,tree,tree…… 于是,一天晚上他梦到自己被关在了一个有根树中,每条路径都有边权,一个神秘的声音告诉他,每个点到其他的 ...
- BZOJ2117: [2010国家集训队]Crash的旅游计划
裸点分,点分树每层维护有序表,查询二分,复杂度$O(nlog^3n)$. #include<bits/stdc++.h> #define M (u+v>>1) #define ...
- [2010国家集训队]Crash的旅游计划
Description 眼看着假期就要到了,Crash由于长期切题而感到无聊了,因此他决定利用这个假期和好友陶陶一起出去旅游. Crash和陶陶所要去的城市里有N (N > 1) 个景点,Cra ...
- BZOJ 2117: [2010国家集训队]Crash的旅游计划 动态点分治+二分
感觉现在写点分治可快了~ 二分答案,就可以将求第 $k$ 大转换成一个判断问题,直接拿点分树判断一下就行了. #include <cstdio> #include <vector&g ...
- Crash的旅行计划
除草了.. Crash的旅行计划 [问题描述] 过不了多久,Crash就要迎来他朝思暮想的暑假.在这个暑假里,他计划着到火星上旅游.在火星上有N个旅游景点,Crash用1至N这N个正整数对这些景点标号 ...
- BZOJ2051——A Problem For Fun
0.题意:给出一个N个结点的树,每条边有一个正整数权值,定义两个结点的距离为连接这两个结点路径上边权的和.对于每个结点i,它到其他N-1个结点都有一个距离,将这些距离从小到大排序,输出第K个距离. 1 ...
- BZOJ2051 : A Problem For Fun
树的点分治,将点分治的过程记录下来,每一个分治结构按到分治中心的距离维护所有点. 对于一个点二分答案,然后在$O(\log n)$个分治结构中二分查找,时间复杂度$O(n\log^3n)$. #inc ...
随机推荐
- [ VB ] OrElse, AndAlso [ C# ] ||, && 运算符
条件演算子も当然のように C# と VB では記述方法が異なる.比較すると下表のようになる. VB C#OrElse ||AndAlso &&a ...
- dubbo 一个接口多个实现
<dubbo:service ref="dubboGroupService1" interface="com.yncp.dubbo.service.IDubboGr ...
- 前端web服务器数据同步方案
概述: 网站采用了web和mysql数据库分离的架构,前端有web1.web2.web3需要对他们进行上传文件同步 方案: 在web2的windows服务器上安装GoodSync软件,利用其双向同步特 ...
- rem布局加载闪烁问题
说明:以下内容来自CSDN,如有侵权,请立刻联系博主(我),我将删除该内容. 原文链接 https://blog.csdn.net/u013778905/article/details/779387 ...
- 分享一些 Java 无关基础方面的书籍
个人认为看书有两个点好处: 1. 能出版出来的书一定是经过反复思考,雕琢和审核的,因此从专业性的角度来说,一本好书的价值超其他资料 2. 对着书上的代码自己敲的时候方便 “看完书之后再次提升自我的最好 ...
- Spatial Transformer Networks
参考:http://blog.csdn.net/xbinworld/article/details/69049680 论文的关键在理解这句话: 先通过V中坐标(xtarget,ytarget)以此找到 ...
- hdu3397区间覆盖,区间翻转,区间合并,区间求和
调了很久的代码..注意区间翻转和覆盖的操作互相的影响 /* 区间替换操作怎么搞? 应该是加个tag标记 如果整个区间都是0|1,那么把若有tag的话直接set1|0即可,也不用设置tag标记 反之要设 ...
- 性能测试六:jmeter进阶之Cookie与header管理器
一.http cookie管理器 可以在浏览器中抓取到cookie信息,然后通过http cookie管理器为http请求添加cookie信息 添加cookie管理器后,Jmeter可以自动处理coo ...
- python 全栈开发,Day70(模板自定义标签和过滤器,模板继承 (extend),Django的模型层-ORM简介)
昨日内容回顾 视图函数: request对象 request.path 请求路径 request.GET GET请求数据 QueryDict {} request.POST POST请求数据 Quer ...
- WebApi的调用-1.前端调用
前端调用 html <div class="row"> <form id="queryForm"> <input name=&qu ...