luogu P4172 [WC2006]水管局长 LCT维护动态MST + 离线
Description
Input
Output
#include<bits/stdc++.h>
#define maxn 1200000
#define N 120000
using namespace std;
char *p1,*p2,buf[100000];
#define nc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int rd() {int x=0,f=1; char c=nc(); while(c<48) {if(c=='-') f=-1; c=nc();} while(c>47) x=(((x<<2)+x)<<1)+(c^48),c=nc(); return x*f;}
void setIO(string s)
{
string in=s+".in";
string out=s+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
struct Union
{
int p[maxn];
inline void init() { for(int i=0;i<maxn;++i) p[i]=i; }
inline int find(int x) { return p[x]==x?x:p[x]=find(p[x]); }
inline int merge(int x,int y)
{
int a=find(x),b=find(y);
if(a==b) return 0;
p[a]=b;
return 1;
}
}con;
struct LCT
{
#define lson ch[x][0]
#define rson ch[x][1]
#define isrt(x) (!(ch[f[x]][1]==x || ch[f[x]][0]==x))
#define get(x) (ch[f[x]][1]==x)
int ch[maxn][2], maxv[maxn], rev[maxn], sumv[maxn], val[maxn], sta[maxn], idx[maxn], f[maxn];
inline void mark(int x)
{
if(!x)return;
swap(lson,rson), rev[x]^=1;
}
inline void pushup(int x)
{
if(!x)return;
idx[x]=x, maxv[x]=val[x];
if(maxv[lson] > maxv[x]) idx[x] = idx[lson], maxv[x]=maxv[lson];
if(maxv[rson] > maxv[x]) idx[x] = idx[rson], maxv[x]=maxv[rson];
sumv[x]=sumv[lson]+sumv[rson]+val[x];
}
inline void pushdown(int x)
{
if(!x)return;
if(rev[x]) mark(lson), mark(rson), rev[x]^=1;
}
inline void rotate(int x)
{
int old=f[x],fold=f[old], which=get(x);
if(!isrt(old)) ch[fold][ch[fold][1]==old]=x;
ch[old][which]=ch[x][which^1], f[ch[old][which]]=old;
ch[x][which^1]=old, f[old]=x, f[x]=fold;
pushup(old), pushup(x);
}
inline void splay(int x)
{
int u=x,v=0,fa;
for(sta[++v]=u;!isrt(u);u=f[u]) sta[++v]=f[u];
while(v) pushdown(sta[v--]);
for(u=f[u];(fa=f[x])!=u;rotate(x))
if(f[fa]!=u)
rotate(get(fa)==get(x)?fa:x);
}
inline void Access(int x)
{
int t=0;
while(x)
{
splay(x), rson=t, pushup(x), t=x,x=f[x];
}
}
inline void MakeRoot(int x)
{
Access(x), splay(x), mark(x);
}
inline void split(int x,int y)
{
MakeRoot(x), Access(y), splay(y);
}
inline void cut(int x,int y)
{
MakeRoot(x), Access(y), splay(y);
ch[y][0]=f[x]=0;
pushup(y);
}
inline void link(int x,int y)
{
MakeRoot(x), f[x]=y;
}
}tr;
int n,m,Q,eds;
int from[maxn],to[maxn], cap[maxn], answer[maxn];
map<int,int>ck[maxn];
struct OPT
{
int o, x,y,eds, id, ans;
}opt[maxn];
int main()
{
int i,j;
setIO("input");
con.init();
n=rd(),m=rd(),Q=rd();
for(i=1;i<=m;++i)
{
++eds;
from[eds]=rd(), to[eds]=rd(), cap[eds]=rd();
ck[from[eds]][to[eds]]=ck[to[eds]][from[eds]]=eds;
}
for(i=1;i<=Q;++i)
{
opt[i].o=rd();
if(opt[i].o==1) opt[i].x=rd(), opt[i].y=rd();
if(opt[i].o==2)
{
int a,b;
a=rd(),b=rd();
opt[i].eds = ck[a][b];
ck[a][b]=ck[b][a]=0;
}
}
for(i=1;i<=eds;++i)
{
if(ck[from[i]][to[i]])
{
++Q;
opt[Q].o=2;
opt[Q].eds=ck[from[i]][to[i]];
}
}
for(i=Q;i>=1;--i)
{ if(opt[i].o==1)
{
tr.split(opt[i].x,opt[i].y);
opt[i].ans=tr.maxv[opt[i].y];
}
if(opt[i].o==2)
{
int cur=opt[i].eds,c,_new;
int x=from[cur], y=to[cur], cc=cap[cur],old;
if(con.merge(x, y))
{
_new=cur + n;
tr.val[_new]=tr.maxv[_new]=tr.sumv[_new]=cap[cur];
tr.link(_new, x), tr.link(_new, y);
}
else
{
tr.split(x,y);
c=tr.idx[y];
old=tr.val[c];
if(cc < old)
{
int a=from[c-n], b=to[c-n];
tr.cut(a, c), tr.cut(b, c);
_new=cur+n;
tr.val[_new]=tr.maxv[_new]=tr.sumv[_new]=cap[cur];
tr.link(_new, x), tr.link(_new, y);
}
}
}
}
for(i=1;i<=Q;++i) if(opt[i].o==1) printf("%d\n",opt[i].ans);
return 0;
}
luogu P4172 [WC2006]水管局长 LCT维护动态MST + 离线的更多相关文章
- P4172 [WC2006]水管局长 LCT维护最小生成树
\(\color{#0066ff}{ 题目描述 }\) SC 省 MY 市有着庞大的地下水管网络,嘟嘟是 MY 市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的 ...
- Luogu P4172 [WC2006]水管局长
题意 给定一个 \(n\) 个点 \(m\) 条边的图和 \(q\) 次操作,每次操作分为以下两种: 1 u v:查询 \(u\) 到 \(v\) 的一条路径使得边权最大的边的权值最小. 2 u v: ...
- 洛谷4172 WC2006水管局长(LCT维护最小生成树)
这个题和魔法森林感觉有很相近的地方啊 同样也是维护一个类似最大边权最小的生成树 但是不同的是,这个题是有\(cut\)和询问,两种操作.... 这可如何是好啊? 我们不妨倒着来考虑,假设所有要\(cu ...
- P4172 [WC2006]水管局长(LCT)
P4172 [WC2006]水管局长 LCT维护最小生成树,边权化点权.类似 P2387 [NOI2014]魔法森林(LCT) 离线存储询问,倒序处理,删边改加边. #include<iostr ...
- P4172 [WC2006]水管局长
P4172 [WC2006]水管局长 前言 luogu数据太小 去bzoj,他的数据大一些 思路 正着删不好维护 那就倒着加,没了 LCT维护他的最小生成树MST 树上加一条边肯定会有一个环 看看环上 ...
- bzoj 2594: [Wc2006]水管局长数据加强版 动态树
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 934 Solved: 291[Submit][Sta ...
- 洛谷P4172 [WC2006]水管局长(lct求动态最小生成树)
SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水管的路径, ...
- 洛谷P4172 [WC2006]水管局长 (LCT,最小生成树)
洛谷题目传送门 思路分析 在一个图中,要求路径上最大边边权最小,就不难想到最小生成树.而题目中有删边的操作,那肯定是要动态维护啦.直接上LCT维护边权最小值(可以参考一下蒟蒻的Blog) 这时候令人头 ...
- [洛谷P4172] WC2006 水管局长
问题描述 SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水 ...
随机推荐
- java 学习第一步---安装JDK以及配置环境变量
1.下载jdk 链接:https://pan.baidu.com/s/1FiTGhxdHK0KTFawdkLT26g 提取码:zcy0 我已经在官网上面下载了1.8的jdk,通过百度云盘分 ...
- debug 和release 的区别
http://blog.csdn.net/h_wlyfw/article/details/26688677
- linux之安装软件出现Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)解决总结
sudo rm -rf /var/lib/dpkg/lock sudo rm -rf /var/cache/apt/archives/lock
- scapy基础-网络数据包结构
网络层次模型,数据包的组成是学习scapy的基础,下文主要关注模型中各个层次的用途,ethernet II和ip包数据结构. 1.五层模型简介 名称 作用 包含协议 应用层 面向程序对程序的传输 ...
- luogu 4630 [APIO2018] Duathlon 铁人两项
题目大意: 无向图上找三个点 a b c使存在一条从a到b经过c的路径 求取这三个点的方案数 思路: 建立圆方树 这个圆方树保证没有两个圆点相连或两个方点相连 对于每个节点x 设该节点为路径的中间节点 ...
- bzoj2594 [Wc2006]水管局长数据加强版——LCT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2594 时间倒序一下,就是 魔法森林 那道题: 有个不解的地方,是 access 里面关于 p ...
- 1998 NOIP 拼数 string 和sort的新理解
题目:https://www.luogu.org/problemnew/show/P1012 今天真是长了见识.这道题做了十几分钟,用模拟愣是调不出来.直到我看了题解——(当场去世)…… 题的意思是n ...
- sql复杂查询语句总结
转自:http://blog.csdn.net/fengfeng91/article/details/15029173 create table student( sno varchar2(10) p ...
- Objective-C 对象的类型与动态结合
创建: 2018/01/21 更新: 2018/01/22 标题前增加 [Objective-C] 完成: 2018/01/24 更新: 2018/01/24 加红加粗属性方法的声明 [不直接获取内部 ...
- E20170610-hm
presence n. 出席; 仪表; 风度; 鬼魂,神灵; defence n. 防御; 辩护; 防御工事; 后卫; phyle n. 种族,宗族; race n. 赛跑; 民族; 人种; ...