WZJ的旅行(五)
难度级别:E; 运行时间限制:3000ms; 运行空间限制:262144KB; 代码长度限制:2000000B
试题描述

WZJ又要去旅行了T^T=0。幻想国由N个城市组成,由于道路翻修,只有N-1条双向道路可以通行,第i条双向道路从ui连接到vi,距离为wi。但这N-1条道路竟然连接了整个国家,每两个城市之间都有一条道路!

设d(a,b)表示a城市到b城市的距离(a<b)。WZJ想知道前k大的d(a,b)的值请你告诉他结果。

输入
第一个为两个正整数N,k。
接下来N-1行每行为三个正整数ui,vi,wi。  
输出
输出前k大的d(a,b)值,每个一行。
输入示例
5 10
1 2 1
1 3 2
2 4 3
2 5 4
输出示例
7
7
6
5
4
4
3
3
2
1
其他说明
1<=N<=50000,1<=k<=Min(300000,N*(N-1)/2)
1<=ui,vi<=N
1<=wi<=1000 

题解:点分治套一个超级钢琴耶,先将一个点能够到的位置用l,r数组记录下来(意会一下“够到”。。。),然后问题变为超级钢琴。。。。

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
#define CH for(int d=0;d<2;d++)if(ch[d])
#define lson x->ch[0],L,M
#define rson x->ch[1],M+1,R
using namespace std;
const int maxn=+,maxnode=+,maxm=+,maxt=maxn*,inf=-1u>>;
struct ted{int x,y,w;ted*nxt;}adj[maxm],*fch[maxn],*ms=adj;
void add(int x,int y,int w){
*ms=(ted){x,y,w,fch[x]};fch[x]=ms++;
*ms=(ted){y,x,w,fch[y]};fch[y]=ms++;
return;
}
int CG,size,siz[maxn],f[maxn],l[maxt],r[maxt],t[maxt],ln,rn,tot;bool vis[maxn];
void findcg(int x,int fa){
siz[x]=;int mxs=;
for(ted*e=fch[x];e;e=e->nxt){
int v=e->y;if(v!=fa&&!vis[v]){
findcg(v,x);siz[x]+=siz[v];mxs=max(mxs,siz[v]);
}
}f[x]=max(mxs,size-siz[x]);if(f[x]<f[CG])CG=x;return;
}
void dfs(int x,int fa,int dis){
siz[x]=;l[++tot]=ln;r[tot]=rn;t[tot]=dis;
for(ted*e=fch[x];e;e=e->nxt){
int v=e->y;if(v!=fa&&!vis[v])dfs(v,x,dis+e->w),siz[x]+=siz[v];
}return;
}
void solve(int x){
vis[x]=true;bool flag=true;
for(ted*e=fch[x];e;e=e->nxt){
int v=e->y;if(!vis[v]){
if(flag)flag=false,ln=rn=++tot;
dfs(v,x,e->w);rn=tot;
}
}
for(ted*e=fch[x];e;e=e->nxt){
int v=e->y;if(!vis[v]){
f[CG=]=size=siz[v];findcg(v,x);solve(CG);
}
}return;
}
struct node{
node*ch[];int p,v;node(){v=-inf;}
void update(){v=-inf;CH{if(v<ch[d]->v)p=ch[d]->p,v=ch[d]->v;}return;}
}seg[maxnode],*nodecnt=seg,*root;int n,k,ql,qr,_p,_v;
void build(node*&x=root,int L=,int R=tot){
x=nodecnt++;int M=L+R>>;if(L==R)x->p=M,x->v=t[M];
else build(lson),build(rson),x->update();return;
}
void query(node*x=root,int L=,int R=tot){
if(ql<=L&&R<=qr){
if(_v<x->v)_v=x->v,_p=x->p;
}else{int M=L+R>>;
if(ql<=M)query(lson);if(qr>M)query(rson);
}return;
}
struct info{int x,L,R,t;};
bool operator<(const info&a,const info&b){return t[a.t]+t[a.x]<t[b.t]+t[b.x];}
priority_queue<info>Q;
inline int read(){
int x=,sig=;char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=;
for(;isdigit(ch);ch=getchar())x=*x+ch-'';
return sig?x:-x;
}
inline void write(long long x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=;long long buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
void init(){
n=read();k=read();int x,y;
for(int i=;i<n;i++)x=read(),y=read(),add(x,y,read());
f[CG=]=size=n;findcg(,);solve(CG);
build();
for(int i=;i<=tot;i++)if(l[i]){
ql=l[i];qr=r[i];_v=-inf;query();Q.push((info){i,l[i],r[i],_p});
}
for(int i=;i<=k;i++){
info a=Q.top();Q.pop();write(t[a.x]+t[a.t]);ENT;
if(a.L<a.t){ql=a.L;qr=a.t-;_v=-inf;query();Q.push((info){a.x,a.L,a.t-,_p});}
if(a.t<a.R){ql=a.t+;qr=a.R;_v=-inf;query();Q.push((info){a.x,a.t+,a.R,_p});}
}
return;
}
void work(){
return;
}
void print(){
return;
}
int main(){init();work();print();return ;}

COJ 0349 WZJ的旅行(五)的更多相关文章

  1. COJ 0346 WZJ的旅行(二)更新动态树分治版本

    WZJ的旅行(二) 难度级别:D: 运行时间限制:3000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 时隔多日,WZJ又来到了幻想国旅行.幻想国由N个城市组成,由 ...

  2. COJ 0995 WZJ的数据结构(负五)区间操作

    WZJ的数据结构(负五) 难度级别:C: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大小为 ...

  3. COJ 0985 WZJ的数据结构(负十五)(限定区域不同数)

    传送门:http://oj.cnuschool.org.cn/oj/home/addSolution.htm?problemID=955 试题描述: CHX有一个问题想问问大家.给你一个长度为N的数列 ...

  4. COJ 0967 WZJ的数据结构(负三十三)

    WZJ的数据结构(负三十三) 难度级别:E: 运行时间限制:7000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大 ...

  5. COJ 0970 WZJ的数据结构(负三十)树分治

    WZJ的数据结构(负三十) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计 ...

  6. COJ 0990 WZJ的数据结构(负十)

    WZJ的数据结构(负十) 难度级别:D: 运行时间限制:5000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给你一个N个节点的有根树,从1到N编号,根节点为1并给 ...

  7. COJ 1008 WZJ的数据结构(八) 树上操作

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=986 WZJ的数据结构(八) 难度级别:E: 运行时间限制:3000ms: ...

  8. COJ 1007 WZJ的数据结构(七) 树上操作

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=983 WZJ的数据结构(七) 难度级别:C: 运行时间限制:1000ms: ...

  9. COJ 0343 WZJ的公司(二)

    传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=313 试题描述: WZJ的公司放假了!为了保证假期期间公司的安全,WZJ决定 ...

随机推荐

  1. svn命令的使用

    1.检出svn  co  http://路径(目录或文件的全路径) [本地目录全路径] --username 用户名 --password 密码svn  co  svn://路径(目录或文件的全路径) ...

  2. div css背景图片不显示

    我们在写页面时,为了便于维护,css样式通常都是通过link外部导入html的,有时在css中写入背景图片时,此时背景图片的路径应该是相对css文件的.比如,此时的文件有index.html,css. ...

  3. vim 缩写abbreviation

    创建 :ab abbreviation pharse 取消 :unab abbreviation 缩写使用 insert模式下输入缩写,Enter键获得pharse.

  4. SPFA 小优化*2

    /* bzoj 2763 SPFA小优化 循环队列+SLF 顺面改掉自己之前手打qeueu的坏毛病*/ #include<iostream> #include<cstring> ...

  5. android 如何解决模块之间的通讯的耦合问题

    使用EventBus http://wuyexiong.github.io/blog/2013/04/30/android-fragment/ http://yunfeng.sinaapp.com/? ...

  6. 记一次网站服务器迁移(my)

    遇到的难题: 基本没有遇到太大的问题,因为服务器环境是配好的阿里云环境,最后再nginx的rewrite配置遇到了一点问题,最后也算解决. 收获小点: 1) vim替换命令: 利用 :s 命令可以实现 ...

  7. windows sever 2008 r2 - 限制ip访问

    和win 7 旗舰版不同,该操作系统在安装IIS后,非本机的并不能直接访问主机.需要设置主机上的本机的IIS中的IP地址和域限制. 由于我是想在同一个局域网(路由器)中,通过Android操作系统访问 ...

  8. ubuntu JDK

    第一步:下载jdk-7-linux-i586.tar.gz 第二步:解压安装 cd /usr/libsudo mkdir jvm cd {你的目录jdk压缩文件目录} sudo tar zxvf jd ...

  9. How to Make LastPass Even More Secure with Google Authenticator

    Google Authenticator LastPass supports Google Authenticator, which is officially available as an app ...

  10. SGU 175.Encoding

    Solution: 简单题. 答案初始化为1. 从给定的n,q往下推出新的n和q,如果q是在右半边,答案加上 n-n/2. 一直到推到n==1. code: #include <iostream ...