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的水 ...
随机推荐
- 解决burp suite 使用chrome訪问https失真的问题
用burp suite 訪问https网页 尤其使用chrome(有时候firefox也会) 会出现js或者css载入不出来的情况 这样的时候,导出burp suite的证书,保存为cer格式 然后进 ...
- Android学习笔记-tween动画之xml实现
继上篇tween动画的java实现:http://www.cnblogs.com/fengtengfei/p/3957800.html, 这里我接着介绍一下tween动画的xml实现的方法, 首先 ...
- JavaScript基础 -- DOM
一.DOM全称 文档对象模型(Document Object Model) 二.DOM是什么 DOM可以说是制作动态页面的强有力工具.DOM不是JavaScript语言的一部分,而是内置在浏览器中的一 ...
- python安装了2.7之后终端无法使用退格,上下左右
遇到RT问题,原因是由于在编译python的时候去烧readline库造成的 解决办法: yum install readline-devel 然后重新编译安装python,终端控制符可用!
- c#调用oracle存储过程返回数据集
c#调用oracle存储过程返回数据集 2008-12-20 10:59:57| 分类: net|字号 订阅 CREATE OR REPLACE PACKAGE pkg_tableTypeIS ...
- 玲珑学院OJ 1023 - Magic boy Bi Luo with his excited math problem 树状数组暴力
分析:a^b+2(a&b)=a+b so->a^(-b)+2(a&(-b))=a-b 然后树状数组分类讨论即可 链接:http://www.ifrog.cc/acm/probl ...
- Shell 通配符、元字符、转义符*****
说到shell通配符(wildcard),大家在使用时候会经常用到.下面是一个实例: 1 1 2 3 4 [chengmo@localhost ~/shell]$ ls a.txt b.txt ...
- nginx配置改变默认访问路径
在安装完nginx服务后,url访问的默认路径是安装的路径html文件夹下的内容,如果需要指定自定义的路径,需要配置nginx.conf文件内容,这样通过url访问就可以了,比如: http://12 ...
- Spark 多项式逻辑回归__二分类
package Spark_MLlib import org.apache.spark.ml.Pipeline import org.apache.spark.ml.classification.{L ...
- Eclipse中执行sql脚本文件
转自:https://blog.csdn.net/weixin_37778823/article/details/79614281 在Eclipse中导入或新建sql脚本文件(.sql文件),选择指定 ...