题意

给定一个 \(n\) 个点 \(m\) 条边的图和 \(q\) 次操作,每次操作分为以下两种:

  • 1 u v:查询 \(u\) 到 \(v\) 的一条路径使得边权最大的边的权值最小。

  • 2 u v:将边 \((u,v)\) 删去。

\(\texttt{Data Range:}1\leq u\leq 10^3,1\leq m,q\leq 10^5\)

题解

边权最大的边的权值最小,考虑用 \(\texttt{LCT}\) 维护原图的最小生成树。同时由于存在断边操作不好维护,所以考虑时间倒流。

首先先将所有没有被割去的边连起来,然后对于每一个割边操作,由于时间倒流了,所以在原图上连边,找新的最小生成树。

这里涉及到割掉环上边权最大的边的操作,为了更好的处理这个操作,可以在 \(\texttt{LCT}\) 上维护一个 \(mx_i\) 表示以 \(i\) 为根的子树上点权最大的是哪个。

注意到我们将原树上的边拆成了点,而原树上的点在 \(\texttt{LCT}\) 上的权值为 \(0\),所以这样做是正确的。

代码

#include<bits/stdc++.h>
using namespace std;
typedef int ll;
typedef long long int li;
const ll MAXN=2e5+51;
struct Edge{
ll from,to,dist;
inline bool operator <(const Edge &rhs)const
{
return dist<rhs.dist;
}
};
Edge ed[MAXN];
ll n,m,qcnt,from,to,cnt,xx;
ll dis[1051][1051],op[MAXN],x[MAXN],y[MAXN],g[1051][1051],ffa[MAXN];
ll res[MAXN],ex[MAXN];
inline ll read()
{
register ll num=0,neg=1;
register char ch=getchar();
while(!isdigit(ch)&&ch!='-')
{
ch=getchar();
}
if(ch=='-')
{
neg=-1;
ch=getchar();
}
while(isdigit(ch))
{
num=(num<<3)+(num<<1)+(ch-'0');
ch=getchar();
}
return num*neg;
}
inline ll find(ll x)
{
return x==ffa[x]?x:ffa[x]=find(ffa[x]);
}
namespace LCT{
struct Node{
ll fa,mx,val,rv,sz;
ll ch[2];
};
struct LinkCutTree{
Node nd[MAXN];
ll st[MAXN];
#define ls nd[x].ch[0]
#define rs nd[x].ch[1]
inline bool nroot(ll x)
{
return nd[nd[x].fa].ch[0]==x||nd[nd[x].fa].ch[1]==x;
}
inline ll get(ll x,ll y)
{
return nd[x].val>nd[y].val?x:y;
}
inline void update(ll x)
{
nd[x].mx=get(x,get(nd[ls].mx,nd[rs].mx));
}
inline void reverse(ll x)
{
swap(ls,rs),nd[x].rv^=1;
}
inline void spread(ll x)
{
if(nd[x].rv)
{
ls?reverse(ls):(void)(1),rs?reverse(rs):(void)(1);
nd[x].rv=0;
}
}
inline void rotate(ll x)
{
ll fa=nd[x].fa,gfa=nd[fa].fa;
ll dir=nd[fa].ch[1]==x,son=nd[x].ch[!dir];
if(nroot(fa))
{
nd[gfa].ch[nd[gfa].ch[1]==fa]=x;
}
nd[x].ch[!dir]=fa,nd[fa].ch[dir]=son;
if(son)
{
nd[son].fa=fa;
}
nd[fa].fa=x,nd[x].fa=gfa,update(fa);
}
inline void splay(ll x)
{
ll fa=x,gfa,cur=0;
st[++cur]=fa;
while(nroot(fa))
{
st[++cur]=fa=nd[fa].fa;
}
while(cur)
{
spread(st[cur--]);
}
while(nroot(x))
{
fa=nd[x].fa,gfa=nd[fa].fa;
if(nroot(fa))
{
rotate((nd[fa].ch[0]==x)^(nd[gfa].ch[0]==fa)?x:fa);
}
rotate(x);
}
update(x);
}
inline void access(ll x)
{
for(register int i=0;x;x=nd[i=x].fa)
{
splay(x),rs=i,update(x);
}
}
inline void makeRoot(ll x)
{
access(x),splay(x),reverse(x);
}
inline void link(ll edx,ll x,ll y)
{
makeRoot(x);
nd[nd[ex[edx]=x].fa=edx].fa=y,nd[edx].val=dis[x][y],update(edx);
}
inline void cut(ll x)
{
access(ex[x]),splay(x);
ls=rs=nd[ls].fa=nd[rs].fa=0;
}
#undef ls
#undef rs
};
}
LCT::LinkCutTree lct;
int main()
{
n=read(),m=read(),qcnt=read();
for(register int i=1;i<=m;i++)
{
from=ed[i].from=read(),to=ed[i].to=read();
dis[from][to]=dis[to][from]=ed[i].dist=read();
}
for(register int i=1;i<=qcnt;i++)
{
op[i]=read(),x[i]=read(),y[i]=read();
if(op[i]==2)
{
g[x[i]][y[i]]=g[y[i]][x[i]]=1;
}
}
for(register int i=0;i<=n;i++)
{
ffa[i]=i;
}
sort(ed+1,ed+m+1),cnt=n*2-1;
for(register int i=1;cnt>n;i++)
{
from=ed[i].from,to=ed[i].to;
if(!g[from][to]&&find(from)!=find(to))
{
lct.link(cnt--,from,to),ffa[ffa[from]]=ffa[to];
}
}
cnt=0;
for(register int i=qcnt;i;i--)
{
lct.makeRoot(from=x[i]),lct.access(to=y[i]),lct.splay(to);
if(op[i]==1)
{
res[++cnt]=lct.nd[lct.nd[to].mx].val;
}
if(op[i]==2)
{
if(lct.nd[lct.nd[to].mx].val>dis[from][to])
{
lct.cut(xx=lct.nd[to].mx),lct.link(xx,from,to);
}
}
}
while(cnt)
{
printf("%d\n",res[cnt--]);
}
}

Luogu P4172 [WC2006]水管局长的更多相关文章

  1. luogu P4172 [WC2006]水管局长 LCT维护动态MST + 离线

    Code: #include<bits/stdc++.h> #define maxn 1200000 #define N 120000 using namespace std; char ...

  2. P4172 [WC2006]水管局长

    P4172 [WC2006]水管局长 前言 luogu数据太小 去bzoj,他的数据大一些 思路 正着删不好维护 那就倒着加,没了 LCT维护他的最小生成树MST 树上加一条边肯定会有一个环 看看环上 ...

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

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

  4. 洛谷P4172 [WC2006]水管局长(lct求动态最小生成树)

    SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水管的路径, ...

  5. P4172 [WC2006]水管局长 LCT维护最小生成树

    \(\color{#0066ff}{ 题目描述 }\) SC 省 MY 市有着庞大的地下水管网络,嘟嘟是 MY 市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的 ...

  6. [洛谷P4172] WC2006 水管局长

    问题描述 SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水 ...

  7. 洛谷P4172 [WC2006]水管局长 (LCT,最小生成树)

    洛谷题目传送门 思路分析 在一个图中,要求路径上最大边边权最小,就不难想到最小生成树.而题目中有删边的操作,那肯定是要动态维护啦.直接上LCT维护边权最小值(可以参考一下蒟蒻的Blog) 这时候令人头 ...

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

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

  9. bzoj 2594: [Wc2006]水管局长数据加强版 动态树

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

随机推荐

  1. Activiti工作流系统环境搭建

    一.创建Activiti工程,并导入Activiti包及数据库驱动包 二.用代码方式创建 流程引擎 1 @Test 2 public void createProcessEngineWithCode( ...

  2. c++中的GetModuleFileName函数的用法以及作用

    参考: 1. http://blog.sina.com.cn/s/blog_b078a1cb0101fw48.html 2. https://www.cnblogs.com/Satu/p/820393 ...

  3. DOS批处理中%cd%与%~dp0的区别详解

    转载:https://www.jb51.net/article/105325.htm DOS批处理中%cd%与%~dp0的区别详解     Windows下批处理中%cd%和%~dp0都能用来表示当前 ...

  4. Open CV leaning

    刚接触Open CV 几个比较好的介绍: OpenCV学习笔记:https://blog.csdn.net/yang_xian521/column/info/opencv-manual/3 OpenC ...

  5. 返回头添加cookie信息

    返回类型 HttpResponseMessage //构建返回对象 var res= Request.CreateResponse(HttpStstusCode.Ok,返回体) //创建cookie对 ...

  6. 《流畅的Python》第二部分 数据结构 【序列构成的数组】【字典和集合】【文本和字节序列】

    第二部分 数据结构 第2章 序列构成的数组 内置序列类型 序列类型 序列 特点 容器序列 list.tuple.collections.deque - 能存放不同类型的数据:- 存放的是任意类型的对象 ...

  7. git-代码分支管理

    1. git代码分支管理     DEV SIT UAT PET PRE PRD PROD常见环境英文缩写含义 英文缩写 英文 中文 DEV development 开发 SIT System Int ...

  8. 蒲公英 · JELLY技术周刊 Vol.25 · Webpack 5 正式发布,你学废了么

    蒲公英 · JELLY技术周刊 Vol.25 阔别两年,Webpack 5 正式发布了,不仅清理掉很多冗余的功能,同样也为我们带来了很多新鲜的能力,不论是默认开启的持久缓存,还是反病毒保护,亦或者被其 ...

  9. lua 1.1 源码阅读总结

    GC 1. 怎么回收的lua 中所有已经分配的数据都会用一些指令的数据结构来记录,当需要回收时,先遍历当前栈内所有 object,把 ref 标志位打上 1,遍历符号表(这部分不能回收),反符号表中的 ...

  10. Mac快捷键整理(随时更新)

    一.文字编辑 快捷键 含义 解释 control + A 到行首 A没找到对应单词,暂时认为A是字母表的开始 control + E 到行尾 E是end的缩写