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的水 ...
随机推荐
- Unity3D游戏开发最佳实践20技巧(一)
关于这些技巧这些技巧不可能适用于每一个项目. 这些是基于我的一些项目经验.项目团队的规模从3人到20人不等. 框架结构的可重用性.清晰程度是有代价的--团队的规模和项目的规模决定你要在这个上面付出多少 ...
- JBOSS和EJB学习一
1.使用软件 IDE:Eclipse4.3(开普勒) EE版本 服务器:jboss EAP 6.2 eclipse-jboss plugin:jbosstools-Update-4.1.2.Final ...
- 嵌入式Linux系统---ppp拨号,4G模块上网【转】
本文转载自:http://blog.csdn.net/qq562029186/article/details/65438553 4G模块PPP拨号上网 方法1 所需文件: xxx-chat-conne ...
- EasyUI Datagrid 分页显示(客户端)
转自:https://blog.csdn.net/metal1/article/details/17536185 EasyUI Datagrid 分页显示(客户端) By ZYZ 在使用JQuery ...
- 在数据库中生成txt文件到网络驱动器中(计算机直接创建的网络驱动器在sql server中没有被找到)
环境:sql server 2008 一.创建网络驱动器映射 语法:exec master..xp_cmdshell 'net use Z: \\ip地址\网络路径 密码 /user:用户名' 例如: ...
- 【weiphp】安装中报错
问题描述:安装的第三部报错“SQLSTATE[HY000]: General error: 2030 This command is not supported in the prepared sta ...
- CodeIgnitor 配置类的使用
CI 的配置文件统一放在 application/config/ 目录下面,框架有一个默认的主配置文件 application/config/config.php.其部分内容如下: <?php ...
- spring cloud config搭建说明例子(四)-补充配置文件
服务端 ConfigServer pom.xml <dependency> <groupId>org.springframework.cloud</groupId> ...
- netty 引用计数器 ,垃圾回收
netty 引用计数器 ,垃圾回收 https://blog.csdn.net/u013851082/article/details/72170065 Netty之有效规避内存泄漏 https://w ...
- 图片选择器ImageEditContainer
1. 简介 本次demo中一共封装了两个组件:ImageEditButton 和 ImageEditContainer.其中ImageEditContainer 是在 ImageEditButton, ...