离线后逆序处理所有操作,那么就变成了加边询问,根据MST的性质,显然维护MST询问链上max即可

#include <bits/stdc++.h>
using namespace std; #define int long long const int N = 1000000; int n,m,q,t1,t2,t3,t4; struct Edge {
int u,v,w,f;
} e[N]; struct Operating {
int o,p,q;
} op[N]; struct LinkCutTree {
int top, q[N], ch[N][2], fa[N], rev[N], mx[N], mp[N], val[N];
inline void pushup(int x) {
mx[0]=mp[0]=0;
mx[x] = max(max(mx[ch[x][0]],mx[ch[x][1]]),val[x]);
if(mx[x] == mx[ch[x][0]])
mp[x]=mp[ch[x][0]];
if(mx[x] == mx[ch[x][1]])
mp[x]=mp[ch[x][1]];
if(mx[x] == val[x])
mp[x]=x;
}
inline void pushdown(int x) {
if(!rev[x])
return;
rev[ch[x][0]]^=1;
rev[ch[x][1]]^=1;
rev[x]^=1;
swap(ch[x][0],ch[x][1]);
}
inline bool isroot(int x) {
return ch[fa[x]][0]!=x && ch[fa[x]][1]!=x;
}
inline void rotate(int p) {
int q=fa[p], y=fa[q], x=ch[fa[p]][1]==p;
ch[q][x]=ch[p][x^1];
fa[ch[q][x]]=q;
ch[p][x^1]=q;
fa[q]=p;
fa[p]=y;
if(y)
if(ch[y][0]==q)
ch[y][0]=p;
else if(ch[y][1]==q)
ch[y][1]=p;
pushup(q);
pushup(p);
}
inline void splay(int x) {
q[top=1]=x;
for(int i=x; !isroot(i); i=fa[i])
q[++top]=fa[i];
for(int i=top; i; i--)
pushdown(q[i]);
for(; !isroot(x); rotate(x))
if(!isroot(fa[x]))
rotate((ch[fa[x]][0]==x)==(ch[fa[fa[x]]][0]==fa[x])?fa[x]:x);
}
void access(int x) {
for(int t=0; x; t=x,x=fa[x])
splay(x),ch[x][1]=t,pushup(x);
}
void makeroot(int x) {
access(x);
splay(x);
rev[x]^=1;
}
int find(int x) {
access(x);
splay(x);
while(ch[x][0])
x=ch[x][0];
return x;
}
void split(int x,int y) {
makeroot(x);
access(y);
splay(y);
}
void cut(int x,int y) {
split(x,y);
if(ch[y][0]==x)
ch[y][0]=0, fa[x]=0;
}
void link(int x,int y) {
makeroot(x);
fa[x]=y;
}
void setval(int p,int v) {
val[p]=mx[p]=v;
mp[p]=p;
}
int queryv(int p,int q) {
split(p,q);
return mx[q];
}
int queryp(int p,int q) {
split(p,q);
return mp[q];
}
} lct; bool check(int p,int q) {
return lct.find(p)==lct.find(q);
} int queryv(int p,int q) {
return lct.queryv(p,q);
} int queryp(int p,int q) {
return lct.queryp(p,q)-n;
} void link(int i) {
lct.link(n+i,e[i].u);
lct.link(n+i,e[i].v);
} void cut(int i) {
lct.cut(n+i,e[i].u);
lct.cut(n+i,e[i].v);
} void add(int i) {
if(check(e[i].u,e[i].v)) {
int v=queryv(e[i].u,e[i].v), p=queryp(e[i].u,e[i].v);
if(v > e[i].w) {
cut(p);
link(i);
}
} else
link(i);
} map <int,int> mp[N];
vector <int> vc; signed main() {
scanf("%lld%lld%lld",&n,&m,&q);
for(int i=1; i<=m; i++) {
scanf("%lld%lld%lld",&t1,&t2,&t3);
mp[t1][t2]=i;
mp[t2][t1]=i;
e[i]=(Edge) {
t1,t2,t3,0
};
}
for(int i=1; i<=n; i++)
lct.setval(i, 0);
for(int i=1; i<=m; i++)
lct.setval(i+n, e[i].w);
for(int i=1; i<=q; i++) {
scanf("%lld%lld%lld",&t1,&t2,&t3);
if(t1==2) {
t4 = mp[t2][t3];
e[t4].f=1;
}
op[i]=(Operating) {
t1,t2,t3
};
}
reverse(op+1,op+q+1);
for(int i=1; i<=m; i++) {
if(e[i].f==0) {
add(i);
}
}
for(int i=1; i<=q; i++) {
if(op[i].o == 1) {
vc.push_back(queryv(op[i].p,op[i].q));
} else {
add(mp[op[i].p][op[i].q]);
}
}
while(vc.size()) {
printf("%lld\n",vc.back());
vc.pop_back();
}
}

[WC2006] 水管局长 - Link Cut Tree的更多相关文章

  1. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  2. BZOJ 2594: [Wc2006]水管局长数据加强版( LCT )

    离线然后就是维护加边的动态MST, Link cut tree秒掉..不过我写+调了好久...时间复杂度O(NlogN + MlogM) ------------------------------- ...

  3. Link Cut Tree 总结

    Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...

  4. [BZOJ2594] [WC2006]水管局长(Kruskal+LCT)

    [BZOJ2594] [WC2006]水管局长(Kruskal+LCT) 题面 SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可 ...

  5. BZOJ2594: [Wc2006]水管局长数据加强版

    题解: 裸LCT+离线+二分+MST... 代码:(几乎摘抄自hzwer) #include<cstdio> #include<cstdlib> #include<cma ...

  6. [bzoj2594][Wc2006]水管局长数据加强版 (lct)

    论蒟蒻的自我修养T_T.. 和noi2014魔法森林基本一样...然而数据范围大得sxbk...UPD:这题如果用lct判联通的话可能会被卡到O(mlogm)..所以最好还是用并查集吧 一开始数组开太 ...

  7. BZOJ 2594: [Wc2006]水管局长数据加强版 [LCT kruskal]

    2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec  Memory Limit: 128 MBSubmit: 2917  Solved: 918[Submit][St ...

  8. BZOJ_2594_[Wc2006]水管局长数据加强版_LCT

    BZOJ_2594_[Wc2006]水管局长数据加强版_LCT Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供 ...

  9. P4172 [WC2006]水管局长(LCT)

    P4172 [WC2006]水管局长 LCT维护最小生成树,边权化点权.类似 P2387 [NOI2014]魔法森林(LCT) 离线存储询问,倒序处理,删边改加边. #include<iostr ...

随机推荐

  1. 纪中23日c组T3 2161. 【2017.7.11普及】围攻 斐波那契数列

    2161. 围攻 (File IO): input:siege.in output:siege.out 时间限制: 1000 ms  空间限制: 262144 KB  具体限制   Goto Prob ...

  2. beego的请求数据处理

    我们经常需要获取用户传递的数据,包括 Get.POST 等方式的请求,beego 里面会自动解析这些数据,你可以通过如下方式获取数据: GetString(key string) string Get ...

  3. 吴裕雄--天生自然 PYTHON数据分析:所有美国股票和etf的历史日价格和成交量分析

    # This Python 3 environment comes with many helpful analytics libraries installed # It is defined by ...

  4. JN_0014:win10命令窗口以管理员方式启动

    1,先打开命令窗口 2,固定到任务栏 3,右键任务栏图标 4,右键命令提示符, 5,选择以管理员方式启动.

  5. spring boot中通用应用程序属性

    可以在application.properties文件内部application.yml,文件内部或命令行开关中指定各种属性.本附录提供了常见的Spring Boot属性列表以及对使用它们的基础类的引 ...

  6. 移动Web开发-WebApp(flex布局+移动端导航案例)

    实际开发中的像素:css像素设备像素比dpr=设备像素/css像素标清屏dpr=1 高清屏dpr=2缩放改变的是css像素大小PPI(每英寸的物理像素点)=根号(屏幕横向分辨率²+屏幕纵向分辨率²)/ ...

  7. Linux 编译安装python3

    编译安装python3的步骤 1.很重要,必须执行此操作,安装好编译环境,c语言也是编译后运行,需要gcc编译器golang,对代码先编译,再运行,python是直接运行yum install gcc ...

  8. CTS、CLS、CLR

    CTS.CLS和CLR是.NET框架的3个核心部分,下面分别对它们进行介绍. 1)CTS  Common Type System CTS即通用类型系统,它定义了如何在.NET Framework运行库 ...

  9. [Err] 1248 - Every derived table must have its own alias

    问题描述 [Err] 1248 - Every derived table must have its own alias 问题原因 这句话的意思是说每个派生出来的表都必须有一个自己的别名 我的Mys ...

  10. POJ2226(二分图建图/最小点覆盖)

    题意: 给定m*n的棋盘,有若干只咕咕.希望去掉一部分咕咕使得剩下的咕咕在上下左右四个方向越过咕咕槽的情况下都看不到咕咕. 思路: 建立一个二分图的方法有很多,这里采用xy二分. 假设没有咕咕槽的情况 ...