ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[WC2006]水管局长

2022-02-05 17:02:01  阅读:145  来源: 互联网

标签:return lc int ed void WC2006 maxn 局长 水管


动态维护最小生成树

链接:P4172 [WC2006]水管局长 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

题意:给定一张简单无向图,边有权值,多次询问或者修改。询问是查询从 u 点到 v 点的所有路径中的最小值,路径的值为路径内经过的所有边的最大值。修改是将某条边断掉。修改次数不超过 5 ∗ 1 0 3 5*10^3 5∗103。

题解:首先观察询问这个操作,发现当用最小生成树时,任意两点间的路径距离会是最小的。那么题目转化为动态维护一棵最小生成树,很明显用 LCT 来维护。考虑删边操作,在删掉某条边之后,若是最小生成树因此被隔开,需要重新在无向图的边集中找一条最小的可以连接两个连通块的插入,这么遍历边集的话复杂度过高。将所有询问修改翻过来操作,查询操作无变化,但删边操作则改为加边操作,只需要维护每个 splay 中最大边权的边,加边时比较这条链上最大的边权是否大于加入的边权,是的话就将最大边删去,加入当前边;不是的话则跳过。复杂度 O ( n log ⁡ n ) O(n\log n) O(nlogn)。
注意:维护边权时把边转化为点即可。

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>

using namespace std;
const int maxn=2e5+5;

int n,m,q,x,y,z,w;
int f[maxn],c[maxn][2],v[maxn],s[maxn],mx[maxn],st[maxn];
int ans[maxn],id[1005][1005];
bool r[maxn];
#define lc c[x][0]
#define rc c[x][1]
struct node{
	int u,v,w,ex;
	inline bool operator<(const node&x)const{
		return w<x.w;
	}
}ed[maxn];
struct qy{
	int op,u,v;
}t[maxn];

bool nroot(int x)
{
	return c[f[x]][0]==x||c[f[x]][1]==x;
}

void pushup(int x)
{
	if(s[lc]<s[rc])s[x]=s[rc],mx[x]=mx[rc];
	else s[x]=s[lc],mx[x]=mx[lc];
	if(s[x]<v[x])s[x]=v[x],mx[x]=x;
}

void reverse(int x)
{
	swap(lc,rc),r[x]^=1;
}

void pushdown(int x)
{
	if(r[x])
	{
		if(lc)reverse(lc);
		if(rc)reverse(rc);
		r[x]=0;
	}
}

void rotate(int x)
{
	int y=f[x],z=f[y],k=c[y][1]==x,w=c[x][!k];
	if(nroot(y))c[z][c[z][1]==y]=x;
	c[y][k]=w,c[x][!k]=y;
	if(w)f[w]=y; f[y]=x,f[x]=z;
	pushup(y);
}

void splay(int x)
{
	int y=x,z=0;
	st[++z]=y;
	while(nroot(y))st[++z]=y=f[y];
	while(z)pushdown(st[z--]);
	while(nroot(x))
	{
		y=f[x],z=f[y];
		if(nroot(y))
		{
			rotate((c[y][0]==x)^(c[z][0]==y)?x:y);
		}
		rotate(x);
	}
	pushup(x);
}

void access(int x)
{
	for(int y=0;x;x=f[y=x])
	{
		splay(x),rc=y,pushup(x);
	}
}

void makeroot(int x)
{
	access(x),splay(x),reverse(x);
}

int findroot(int x)
{
	access(x),splay(x);
	while(lc)pushdown(x),x=lc;
	splay(x); return x;
}

void split(int x,int y)
{
	makeroot(x),access(y),splay(y);
}

bool link(int x,int y,int o)
{
	makeroot(x);
	if(findroot(y)!=x){if(o)f[x]=y; return true;}
	return false;
}

bool cut(int x,int y)
{
	makeroot(x);
	if(findroot(y)==x&&f[y]==x&&!c[y][0])
	{
		f[y]=c[x][1]=0; pushup(x); return true;
	}
	return false;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>n>>m>>q;
	for(int i=n+1;i<=n+m;i++)
	{
		cin>>ed[i].u>>ed[i].v>>ed[i].w; 
	}
	sort(ed+n+1,ed+n+1+m); 
	for(int i=n+1;i<=n+m;i++)
	{
		id[ed[i].u][ed[i].v]=id[ed[i].v][ed[i].u]=i;
		v[i]=ed[i].w;
	}	
	for(int i=1;i<=q;i++)
	{
		cin>>t[i].op>>t[i].u>>t[i].v;
		if(t[i].op==2)ed[id[t[i].u][t[i].v]].ex=1;
	}
	int cnt=0;
	for(int i=n+1;i<=n+m;i++)
	{
		if(cnt<n-1&&!ed[i].ex&&link(ed[i].u,ed[i].v,0))
		{
			link(ed[i].u,i,1),link(ed[i].v,i,1),cnt++;
		}
	}
	for(int i=q;i>=1;i--)
	{
		if(t[i].op==1)
		{
			split(t[i].u,t[i].v),ans[i]=s[t[i].v];
		}
		else
		{
			int d=id[t[i].u][t[i].v];
			split(t[i].u,t[i].v);
			if(s[t[i].v]>ed[d].w)
			{
				int d2=mx[t[i].v];
				cut(ed[d2].u,d2),cut(ed[d2].v,d2);
				link(t[i].u,d,1),link(t[i].v,d,1);
			}
		}
	}
	for(int i=1;i<=q;i++)
	{
		if(t[i].op==1)cout<<ans[i]<<"\n";
	}
	return 0;
}

标签:return,lc,int,ed,void,WC2006,maxn,局长,水管
来源: https://blog.csdn.net/m0_51547083/article/details/122791502

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有