BZOJ_2594_[Wc2006]水管局长数据加强版_LCT
BZOJ_2594_[Wc2006]水管局长数据加强版_LCT
Description
Input
Output
Sample Input
1 2 2
2 3 3
3 4 2
1 4 2
1 1 4
2 1 4
1 1 4
Sample Output
2
3
【原题数据范围】
N ≤ 1000
M ≤ 100000
Q ≤ 100000
测试数据中宣布报废的水管不超过5000条;且任何时候我们考虑的水管网络都是连通的,即从任一结点A必有至少一条水管路径通往任一结点B。
【加强版数据范围】
N ≤ 100000
M ≤ 1000000
Q ≤ 100000
删边不如离线加边往里插。
类似魔法森林那道题,LCT维护权值最大的边,然后需要在加边的时候判断是否要加进去。
听起来非常简单,然而删边时不告诉你编号。。。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char nc() {
static char buf[100000],*p1,*p2;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd() {
register int x=0; register char s=nc();
while(s<'0'||s>'9')s=nc();
while(s>='0'&&s<='9')x=(x<<3)+(x<<1)+s-'0',s=nc();
return x;
}
#define N 100050
#define M 1100050
#define ls ch[p][0]
#define rs ch[p][1]
#define get(x) (ch[f[x]][1]==x)
int ch[M][2],f[M],val[M],rev[M],tot,mx[M],n,m,qs,fa[N];
int killx[M],killy[M],ans[M],nxt[M];
int find(int x) {
return fa[x]==x?x:fa[x]=find(fa[x]);
}
struct A {
int x,y,v,id,flg;
}e[M],ev[M];
bool cmp1(const A &x,const A &y) {
if(x.x==y.x) return x.y<y.y;
return x.x<y.x;
}
bool cmp2(const A &x,const A &y) {
return x.v<y.v;
}
struct QAQ {
int opt,x,y,pos;
}q[N];
inline bool isrt(int p) {
return ch[f[p]][0]!=p&&ch[f[p]][1]!=p;
}
inline void pushup(int p) {
mx[p]=p;
if(val[mx[ls]]>val[mx[p]]) mx[p]=mx[ls];
if(val[mx[rs]]>val[mx[p]]) mx[p]=mx[rs];
}
inline void pushdown(int p) {
if(rev[p]) {
swap(ch[ls][0],ch[ls][1]);
swap(ch[rs][0],ch[rs][1]);
rev[ls]^=1; rev[rs]^=1; rev[p]=0;
}
}
void update(int p) {
if(!isrt(p)) update(f[p]);
pushdown(p);
}
void rotate(int x) {
int y=f[x],z=f[y],k=get(x);
if(!isrt(y)) ch[z][ch[z][1]==y]=x;
ch[y][k]=ch[x][!k]; f[ch[y][k]]=y;
ch[x][!k]=y; f[y]=x; f[x]=z;
pushup(y); pushup(x);
}
void splay(int x) {
update(x);
for(int fa;fa=f[x],!isrt(x);rotate(x))
if(!isrt(fa))
rotate(get(fa)==get(x)?fa:x);
}
void access(int p) {
int t=0;
while(p) splay(p),rs=t,pushup(p),t=p,p=f[p];
}
void makeroot(int p) {
access(p); splay(p); swap(ls,rs); rev[p]^=1;
}
void link(int x,int p) {
makeroot(x); splay(p); f[x]=p;
}
void cut(int x,int p) {
makeroot(x); access(p); splay(p); ls=f[x]=0;
}
int query(int x,int p) {
makeroot(x); access(p); splay(p); return mx[p];
}
int search(int x,int y) {
int l=1,r=m+1;
while(l<r) {
int mid=(l+r)>>1;
if(x>e[mid].x) l=mid+1;
else r=mid;
}
r=nxt[x]+1;
while(l<r) {
int mid=(l+r)>>1;
if(y>e[mid].y) l=mid+1;
else r=mid;
}
return l;
}
int main() {
n=rd(); m=rd(); qs=rd();
register int i,x,y,dx,dy,tmp,d;
for(i=1;i<=m;i++) {
//e为x升序边便于二分
e[i].x=rd(),e[i].y=rd(),e[i].v=rd();
if(e[i].x>e[i].y) swap(e[i].x,e[i].y);
}
sort(e+1,e+m+1,cmp1);
for(i=1;i<=n;i++) fa[i]=i;
//ev为权值排序
for(i=1;i<=m;i++) ev[i]=e[i],nxt[e[i].x]=max(nxt[e[i].x],i)/*,printf("e[i].x=%d,nxt[e[i].x]=%d\n",e[i].x,nxt[e[i].x])*/;
for(i=1;i<=qs;i++) {
q[i].opt=rd(); q[i].x=rd(); q[i].y=rd();
if(q[i].opt==1) continue;
if(q[i].x>q[i].y) swap(q[i].x,q[i].y);
q[i].pos=search(q[i].x,q[i].y);
//printf("q[i].pos=%d ,q[i].x=%d ,q[i].y=%d\n",q[i].pos,q[i].x,q[i].y);
e[q[i].pos].flg=ev[q[i].pos].flg=1;
}
int tot=n,cnt=0;
sort(ev+1,ev+m+1,cmp2);
for(i=1;i<=m;i++) {
if(!ev[i].flg) {
int x=ev[i].x,y=ev[i].y;
int dx=find(x),dy=find(y);
if(dx!=dy) {
//printf("x=%d , y=%d\n",x,y);
tot++; val[tot]=ev[i].v; killx[tot]=x; killy[tot]=y; mx[tot]=tot;
link(x,tot); link(tot,y); fa[dx]=dy;
}
}
}
for(i=qs;i;i--) {
x=q[i].x; y=q[i].y;
if(q[i].opt==1) {
makeroot(x); access(y); splay(y);
ans[++cnt]=val[mx[y]];
}else {
int tmp=q[i].pos;
int dx=find(x),dy=find(y);
if(dx!=dy) {
tot++; val[tot]=e[tmp].v; killx[tot]=x; killy[tot]=y; mx[tot]=tot;
link(x,tot); link(tot,y); fa[dx]=dy;
}else {
int d=query(x,y);
if(val[d]>e[tmp].v) {
tot++; val[tot]=e[tmp].v; killx[tot]=x; killy[tot]=y; mx[tot]=tot;
cut(killx[d],d); cut(d,killy[d]); link(x,tot); link(tot,y);
}
}
}
}
for(i=cnt;i;i--) {
printf("%d\n",ans[i]);
}
}

BZOJ_2594_[Wc2006]水管局长数据加强版_LCT的更多相关文章
- BZOJ2594: [Wc2006]水管局长数据加强版
题解: 裸LCT+离线+二分+MST... 代码:(几乎摘抄自hzwer) #include<cstdio> #include<cstdlib> #include<cma ...
- bzoj 2594: [Wc2006]水管局长数据加强版 动态树
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 934 Solved: 291[Submit][Sta ...
- BZOJ 2594: [Wc2006]水管局长数据加强版( LCT )
离线然后就是维护加边的动态MST, Link cut tree秒掉..不过我写+调了好久...时间复杂度O(NlogN + MlogM) ------------------------------- ...
- [bzoj2594][Wc2006]水管局长数据加强版 (lct)
论蒟蒻的自我修养T_T.. 和noi2014魔法森林基本一样...然而数据范围大得sxbk...UPD:这题如果用lct判联通的话可能会被卡到O(mlogm)..所以最好还是用并查集吧 一开始数组开太 ...
- BZOJ 2594: [Wc2006]水管局长数据加强版 [LCT kruskal]
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 2917 Solved: 918[Submit][St ...
- [WC2006]水管局长数据加强版
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
- 【刷题】BZOJ 2594 [Wc2006]水管局长数据加强版
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
- 2594. [WC2006]水管局长数据加强版【LCT+最小生成树】
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
- BZOJ2594 [Wc2006]水管局长数据加强版 【LCT维护最小生成树】
题目 SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水管的 ...
随机推荐
- Dubbo性能调优参数及原理
本文是针对 Dubbo 协议调用的调优指导,详细说明常用调优参数的作用域及源码. Dubbo调用模型 常用性能调优参数 参数名 作用范围 默认值 说明 备注 threads provider 200 ...
- Install OpenCV 3.0 and Python 2.7+ on OSX
http://www.pyimagesearch.com/2015/06/15/install-OpenCV-3-0-and-Python-2-7-on-osx/ As I mentioned las ...
- 自定义ListView android
定义一个实体类,作为ListView的适配器的适配类型.其中name为水果名称.imageId为水果的图片(图片资源可以随便弄几个占用) public class Fruit { private St ...
- python3学习笔记4---引用http://python3-cookbook.readthedocs.io/zh_CN/latest/
2018-03-01数据结构与算法(4) 1.16过滤序列元素 最简单的过滤序列元素的方法就是使用列表推导.比如: >>> mylist = [1, 4, -5, 10, -7, 2 ...
- 保证你能看懂的KMP字符串匹配算法
文章转载自一位大牛: 阮一峰原网址http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm ...
- Jmeter(二十六)_数据驱动测试
花了一点时间做了一个通用的执行引擎,好处就是我不用再关注测试脚本的内容,而是用测试用例的数据去驱动我们执行的方向.(这个只适合单个接口的测试,具体运用到接口自动化时,还是要靠手动去编写脚本!) 首先我 ...
- 搭建第一个spring boot项目
一.开发工具建议使用Spring Tool Suite 下载地址:http://spring.io/tools/sts/all/ 点击versions选择相应的版本下载,解压后直接运行即可. 二.创建 ...
- Django入门五之admin管理
1. 准备工作 #settings.py #urls.py 2. 创建管理员账号 进入CMD 3. 运行服务器 登录后,发现没有数据的 4. 创建数据 在website/blog/ 新建一个admin ...
- 使用on-my-zsh时,php 输出内容后面多个%号
今天用php写个命令行的小工具时,突然发现在echo输出后,总是会多个%号,开始以为是代码的问题,然后新建了一个代码文件: <?php echo 'hello world'; 输出结果: hel ...
- bootstrap-table 列宽问题解决
<th style="width:120px" data-field="Cel1"><div class="th-inner &qu ...