BZOJ2594:水管局长数据加强版
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
3
HINT
【原题数据范围】
N ≤ 1000
M ≤ 100000
Q ≤ 100000
测试数据中宣布报废的水管不超过5000条;且任何时候我们考虑的水管网络都是连通的,即从任一结点A必有至少一条水管路径通往任一结点B。
【加强版数据范围】
N ≤ 100000
M ≤ 1000000
Q ≤ 100000
任何时候我们考虑的水管网络都是连通的,即从任一结点A必有至少一条水管路径通往任一结点B。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,Q,top;
int f[];
int fa[],c[][],s[];
int mx[],val[];
bool rev[];
struct edge{int u,v,w,id;bool d;}e[];
struct que{int f,x,y,ans,id;}q[];
bool operator<(edge a,edge b)
{
return a.u<b.u||(a.u==b.u&&a.v<b.v);
}
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
bool cmp2(edge a,edge b)
{
return a.id<b.id;
}
int getf(int x){return x==f[x]?x:f[x]=getf(f[x]);}
int find(int u,int v)
{
int l=,r=m;
while(l<=r)
{
int mid=(l+r)>>;
if(e[mid].u<u||(e[mid].u==u&&e[mid].v<v))l=mid+;
else if(e[mid].u==u&&e[mid].v==v)return mid;
else r=mid-;
}
}
bool isroot(int x)
{
return c[fa[x]][]!=x&&c[fa[x]][]!=x;
}
void update(int x)
{
int l=c[x][],r=c[x][];
mx[x]=x;
if(val[mx[l]]>val[mx[x]])mx[x]=mx[l];
if(val[mx[r]]>val[mx[x]])mx[x]=mx[r];
}
void rotate(int x)
{
int y=fa[x],z=fa[y],l,r;
if(c[y][]==x)l=;else l=;r=l^;
if(!isroot(y))
{
if(c[z][]==y)c[z][]=x;else c[z][]=x;
}
fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
c[y][l]=c[x][r];c[x][r]=y;
update(y);update(x);
}
void pushdown(int x)
{
int l=c[x][],r=c[x][];
if(rev[x])
{
rev[x]^=;
rev[l]^=;rev[r]^=;
swap(c[x][],c[x][]);
}
}
void splay(int x)
{
top=;s[++top]=x;
for(int i=x;!isroot(i);i=fa[i])
s[++top]=fa[i];
for(int i=top;i;i--)
pushdown(s[i]);
while(!isroot(x))
{
int y=fa[x],z=fa[y];
if(!isroot(y))
{
if(c[y][]==x^c[z][]==y)rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x)
{
int t=;
while(x)
{
splay(x);c[x][]=t;update(x);t=x;x=fa[x];
}
}
void makeroot(int x)
{
access(x);splay(x);rev[x]^=;
}
void link(int x,int y)
{
makeroot(x);fa[x]=y;
}
void cut(int x,int y)
{
makeroot(x);access(y);splay(y);c[y][]=fa[x]=;
}
int query(int x,int y)
{
makeroot(x);access(y);splay(y);return mx[y];
}
int main()
{
n=read();m=read();Q=read();
for(int i=;i<=n;i++)f[i]=i;
for(int i=;i<=m;i++)
{
e[i].u=read(),e[i].v=read(),e[i].w=read();
if(e[i].u>e[i].v)swap(e[i].u,e[i].v);
}
sort(e+,e+m+,cmp);
for(int i=;i<=m;i++)
{
e[i].id=i;
val[n+i]=e[i].w;
mx[n+i]=n+i;
}
sort(e+,e+m+);
for(int i=;i<=Q;i++)
{
q[i].f=read(),q[i].x=read(),q[i].y=read();
if(q[i].f==)
{
if(q[i].x>q[i].y)swap(q[i].x,q[i].y);
int t=find(q[i].x,q[i].y);
e[t].d=;q[i].id=e[t].id;
}
}
sort(e+,e+m+,cmp2);
int tot=;
for(int i=;i<=m;i++)
if(!e[i].d)
{
int u=e[i].u,v=e[i].v,x=getf(u),y=getf(v);
if(x!=y)
{
f[x]=y;
link(u,i+n);link(v,i+n);
tot++;
if(tot==n-)break;
}
}
for(int i=Q;i;i--)
{
if(q[i].f==)
q[i].ans=val[query(q[i].x,q[i].y)];
else
{
int u=q[i].x,v=q[i].y,k=q[i].id;
int t=query(u,v);
if(e[k].w<val[t])
{
cut(e[t-n].u,t);cut(e[t-n].v,t);
link(u,k+n);link(v,k+n); }
}
}
for(int i=;i<=Q;i++)
if(q[i].f==)printf("%d\n",q[i].ans);
return ;
}
BZOJ2594:水管局长数据加强版的更多相关文章
- BZOJ2594: [Wc2006]水管局长数据加强版
题解: 裸LCT+离线+二分+MST... 代码:(几乎摘抄自hzwer) #include<cstdio> #include<cstdlib> #include<cma ...
- [bzoj2594][Wc2006]水管局长数据加强版 (lct)
论蒟蒻的自我修养T_T.. 和noi2014魔法森林基本一样...然而数据范围大得sxbk...UPD:这题如果用lct判联通的话可能会被卡到O(mlogm)..所以最好还是用并查集吧 一开始数组开太 ...
- 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) ------------------------------- ...
- BZOJ 2594: [Wc2006]水管局长数据加强版 [LCT kruskal]
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 2917 Solved: 918[Submit][St ...
- BZOJ_2594_[Wc2006]水管局长数据加强版_LCT
BZOJ_2594_[Wc2006]水管局长数据加强版_LCT Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供 ...
- BZOJ 2594 【WC2006】 水管局长数据加强版
题目链接:水管局长数据加强版 好久没写博客了…… 上次考试的时候写了一发LCT,但是写挂了……突然意识到我已经很久没有写过LCT了,于是今天找了道题来练练手. 首先,LCT这里不讲.这道题要求支持动态 ...
- BZOJ2594 [Wc2006]水管局长数据加强版 【LCT维护最小生成树】
题目 SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水管的 ...
- [WC 2006]水管局长数据加强版
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
随机推荐
- jquery preventDefault()事件
定义和用法 preventDefault() 方法阻止元素发生默认的行为(例如,当点击提交按钮时阻止对表单的提交). 语法 event.preventDefault() 参数 描述 event 必需. ...
- 配置Dubbo Demo遇到的坑之一---找不到dubbo.xsd文件
原文地址:https://blog.csdn.net/qq_36654870/article/details/80603302 1.dubbo.xsd文件不能读取 因为阿里http://code.al ...
- BBB 常用指令
source .bashrc root@beaglebone:~# route add default gw 192.168.7.1 echo BB-SPIDEV0 > /sys/devices ...
- Python3 From Zero——{最初的意识:003~数字、日期、时间}
一.对数值进行取整:round(value,ndigits) >>> round(15.5,-1) #可以取负数 20.0 >>> round(15.5,0) #当 ...
- jpa现有接口方法说明 (转https://www.cnblogs.com/rulian/p/6557471.html)
一.接口方法整理速查 下表针对于简单查询,即JpaRepository接口(继承了CrudRepository接口.PagingAndSortingRepository接口)中的可访问方法进行整理.( ...
- scrapy的使用--Rcrapy-Redis
Scrapy-Redis分布式爬虫组件 Scrapy是一个框架,他本身是不支持分布式的.如果我们想要做分布式的爬虫.就需要借助一个组件叫做Scrapy-Redis.这个组件正式利用了Redis可以分布 ...
- pycharm快捷键表
快捷键 作用 ctrl(command)+c 复制 ctrl+v 粘贴 ctrl+z 撤销 ctrl+x 剪切,默认整行 ctrl+a 全选 ctrl+f 查找:选中批量修改 shift+ctrl+z ...
- Response案例1_重定向
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.serv ...
- IdentityServer_0_参考资料
官方 项目地址:https://github.com/IdentityServer/IdentityServer4 官方Demo:https://github.com/IdentityServer/I ...
- 2019牛客暑期多校训练营(第八场) E 线段树+可撤销并查集
题目传送门 题意: 给出m条无向边,每条边都有一个$[l,r]$,意思是体积在这个范围内的人才能通过这条边,询问有多少种体积的可能性,能使人从1到n 思路:由于是无向边,1和n的连通性可以用并查集维护 ...