COJ 0349 WZJ的旅行(五)
| 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的旅行(五)的更多相关文章
- COJ 0346 WZJ的旅行(二)更新动态树分治版本
WZJ的旅行(二) 难度级别:D: 运行时间限制:3000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 时隔多日,WZJ又来到了幻想国旅行.幻想国由N个城市组成,由 ...
- COJ 0995 WZJ的数据结构(负五)区间操作
WZJ的数据结构(负五) 难度级别:C: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大小为 ...
- COJ 0985 WZJ的数据结构(负十五)(限定区域不同数)
传送门:http://oj.cnuschool.org.cn/oj/home/addSolution.htm?problemID=955 试题描述: CHX有一个问题想问问大家.给你一个长度为N的数列 ...
- COJ 0967 WZJ的数据结构(负三十三)
WZJ的数据结构(负三十三) 难度级别:E: 运行时间限制:7000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大 ...
- COJ 0970 WZJ的数据结构(负三十)树分治
WZJ的数据结构(负三十) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计 ...
- COJ 0990 WZJ的数据结构(负十)
WZJ的数据结构(负十) 难度级别:D: 运行时间限制:5000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给你一个N个节点的有根树,从1到N编号,根节点为1并给 ...
- COJ 1008 WZJ的数据结构(八) 树上操作
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=986 WZJ的数据结构(八) 难度级别:E: 运行时间限制:3000ms: ...
- COJ 1007 WZJ的数据结构(七) 树上操作
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=983 WZJ的数据结构(七) 难度级别:C: 运行时间限制:1000ms: ...
- COJ 0343 WZJ的公司(二)
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=313 试题描述: WZJ的公司放假了!为了保证假期期间公司的安全,WZJ决定 ...
随机推荐
- [转帖]FPGA--Vivado
来源:http://home.eeworld.com.cn/my/space-uid-639749-blogid-267593.html 一般的,在Verilog中最常用的编码方式有二进制编码(Bin ...
- [转]MySQL数据库备份和还原的常用命令小结
MySQL数据库备份和还原的常用命令小结,学习mysql的朋友可以参考下: 备份MySQL数据库的命令 mysqldump -hhostname -uusername -ppassword datab ...
- Java设计模式(学习整理)----装饰模式
1.概念: (在我看来,模式就像是是一种思想,在这种思想的指引下,对代码和结构的一番加工和整合而已!都是套路!) 装饰模式又称包装(Wrapper)模式,是以对客户端透明的方式扩展对象的功能,是继承关 ...
- Junit4_单元测试
不多说,直接练习学习. 1.将Junit4单元测试包引入项目:项目右键——“属性”,选择“Java Build Path”,然后到右上选择“Libraries”标签,之后在最右边点击“Add Libr ...
- Grunt:多个css,js,进行单独压缩
module.exports = function (grunt) { // 构建任务配置 grunt.initConfig({ //读取package.json的内容,形成个json数据 pkg: ...
- SQL 结构化查询语言
SQL 结构化查询语言 一.数据库的必要性: >>作用:存储数据.检索数据.生成新的数据 1)可以有效结构化存储大量的数据信息,方便用户进行有效的检索和访问. 2)可以有效地保持数据信息的 ...
- @import————————css代码内部链接另外css
在css代码里这样可以链接另外的css @import url("style.css"); @import语法结构 @import + 空格+ url(CSS文件路径地址); ...
- ubuntu 实现界面切换
1.按ALT+CTRL+F1切换到字符界面(Linux实体机) 如果是VMware虚拟机安装的Linux系统,则切换到字符界面的时候需要以下操作 按下ALT+CTRL+SPACE(空格),ALT+CT ...
- tableview 重用nib cell
#import "ViewController.h" #import "NewsTableViewCell.h" #define UISCREEN_HEIGHT ...
- C#执行oracle返回游标类型的存储过程
存储过程代码为: create or replace procedure proc_test(pCursor OUT pak_pub.ut_cursor) AS begin -- 使用游标 open ...