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处,嘟嘟需要为供水公司找到一 ...
随机推荐
- Python3 From Zero——{最初的意识:000~Initial consciousness}
http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000 a.编码 默认情况下,Python ...
- element-ui表格合计不显示&被遮挡问题
step1:修改全局样式 .el-table{ overflow:visible !important; } .el-card__body { padding: 20px 20px 50px 20px ...
- Producer-Consumer 生产者,消费者
这个模式跟Guarded模式有点类似,不过需要一个控制台限制请求方和处理方的频度和数量. public class ProducerConsumerTest { /** * @param args * ...
- [java]反转单项链表,用O(n)时间和O(1)空间
链表数据结构 public class ListNode { public int val; public ListNode next; public ListNode(int x) { val = ...
- Python学习笔记(四)——文件永久存储
文件的永久存储 pickle模块的使用 pickle的实质就是将数据对象以二进制的形式存储 存储数据 pickle.dump(data,file) data表示想要存储的数据元素,file表示要将数据 ...
- indexof方法区分大小写
1)全部转换为大写:str.toUpperCase().IndexOf(s.toUpperCase()) 2)全部转换为小写:str.toLowerCase().IndexOf(s.toLowerCa ...
- ransformResourcesWithMergeJavaResForDebug问题
错误内容: Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. > java.i ...
- ES5给object扩展的一些静态方法
1. Object.create(prototype[, descriptors]) : 创建一个新的对象 1). 以指定对象为原型创建新的对象 2). 指定新的属性, 并对属性进行描述 value ...
- C/C++ Microsoft Visual Studio c++ DOC Home
{ // https://docs.microsoft.com/zh-cn/cpp/overview/visual-cpp-in-visual-studio?view=vs-2017 // https ...
- 关于maven工程将model删除重建之后变为灰色的问题的解决
问题描述: IDEA中的maven工程中有时候将model或者子model建错,删除之后,子模块在maven在侧栏的maven projects中是灰色的,而且是并没有依赖父工程 在网上搜了搜解决办法 ...