洛谷P4172 [WC2006]水管局长(lct求动态最小生成树)
Input
Output
Sample Input
1 2 2
2 3 3
3 4 2
1 4 2
1 1 4
2 1 4
1 1 4
Sample Output
3
【原题数据范围】
N ≤ 1000
M ≤ 100000
Q ≤ 100000
测试数据中宣布报废的水管不超过5000条;且任何时候我们考虑的水管网络都是连通的,即从任一结点A必有至少一条水管路径通往任一结点B。
#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 1100010
#define mp make_pair
#define lson ch[x][0]
#define rson ch[x][1]
#define pii pair<int,int>
using namespace std; struct node
{
int from,to,w;
} e[N];
struct o
{
int from,to,kd,w,ori;
} op[N];
int n,m,k;
map<pii,int> m1,m2; int cmp(node a,node b)
{
return a.w<b.w;
} //lct start int f[N],ch[N][],w[N],tag[N],sum[N]; int not_root(int now)
{
int x=f[now];
return lson==now||rson==now;
} int push_up(int x)
{
sum[x]=x;
if(e[sum[lson]].w>e[sum[x]].w)
{
sum[x]=sum[lson];
}
if(e[sum[rson]].w>e[sum[x]].w)
{
sum[x]=sum[rson];
}
} int rev(int x)
{
swap(lson,rson);
tag[x]^=;
} int push_down(int x)
{
if(tag[x])
{
rev(lson);
rev(rson);
tag[x]^=;
}
} int rotate(int x)
{
int y=f[x],z=f[y],kd=ch[y][]==x,xs=ch[x][!kd];
if(not_root(y)) ch[z][ch[z][]==y]=x;
ch[x][!kd]=y;
ch[y][kd]=xs;
if(xs) f[xs]=y;
f[x]=z;
f[y]=x;
push_up(y);
} int push_all(int x)
{
if(not_root(x))
{
push_all(f[x]);
}
push_down(x);
} int splay(int x)
{
int y,z;
push_all(x);
while(not_root(x))
{
int y=f[x],z=f[y];
if(not_root(y))
{
(ch[y][]==x)^(ch[z][]==y)?rotate(x):rotate(y);
}
rotate(x);
}
push_up(x);
} int access(int x)
{
for(int y=; x; y=x,x=f[x])
{
splay(x);
rson=y;
push_up(x);
}
} int make_root(int x)
{
access(x);
splay(x);
rev(x);
} int split(int x,int y)
{
make_root(x);
access(y);
splay(y);
} int find_root(int x)
{
access(x);
splay(x);
while(lson)
{
push_down(x);
x=lson;
}
return x;
} int link(int x,int y)
{
make_root(x);
if(find_root(y)==x) return ;
f[x]=y;
return ;
} int cut(int x,int y)
{
make_root(x);
if(find_root(y)!=x||f[x]!=y||rson) return ;
f[x]=ch[y][]=;
push_up(y);
return ;
} int print(int x)
{
if(lson) print(lson);
printf("%d ",x);
if(rson) print(rson);
} //lct end // dsu start int fa[N]; int init()
{
for(int i=; i<N; i++)
{
fa[i]=i;
}
} int find(int x)
{
if(fa[x]==x) return x;
return fa[x]=find(fa[x]);
} int unity(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx==fy) return ;
fa[x]=y;
return ;
} //dsu end int main()
{
init();
scanf("%d%d%d",&n,&m,&k);
for(int i=; i<=m; i++)
{
scanf("%d%d%d",&e[i].from,&e[i].to,&e[i].w); }
sort(e+,e+m+,cmp);
for(int i=; i<=m; i++)
{
m2[mp(e[i].from,e[i].to)]=i;
m2[mp(e[i].to,e[i].from)]=i;
}
int kd,from,to;
for(int i=; i<=k; i++)
{
scanf("%d%d%d",&op[i].kd,&op[i].from,&op[i].to);
if(op[i].kd==)
{
m1[mp(op[i].from,op[i].to)]=;
m1[mp(op[i].to,op[i].from)]=;
op[i].ori=m2[mp(op[i].from,op[i].to)];
op[i].w=e[op[i].ori].w;
}
}
for(int i=; i<=m; i++)
{
if(m1[mp(e[i].from,e[i].to)]>) continue;
if(unity(e[i].from,e[i].to))
{
link(e[i].from+m,i);
link(e[i].to+m,i);
}
}
stack<int> ans;
for(int i=k; i>=; i--)
{
if(op[i].kd==)
{
split(op[i].from+m,op[i].to+m);
ans.push(e[sum[op[i].to+m]].w);
}
if(op[i].kd==)
{
split(op[i].from+m,op[i].to+m);
if(e[sum[op[i].to+m]].w>op[i].w)
{
int gg=sum[op[i].to+m];
cut(e[sum[op[i].to+m]].from+m,gg);
cut(e[sum[op[i].to+m]].to+m,gg);
link(op[i].from+m,op[i].ori);
link(op[i].to+m,op[i].ori);
}
}
}
while(!ans.empty())
{
printf("%d\n",ans.top());
ans.pop();
}
}
洛谷P4172 [WC2006]水管局长(lct求动态最小生成树)的更多相关文章
- 洛谷P4172 [WC2006]水管局长 (LCT,最小生成树)
洛谷题目传送门 思路分析 在一个图中,要求路径上最大边边权最小,就不难想到最小生成树.而题目中有删边的操作,那肯定是要动态维护啦.直接上LCT维护边权最小值(可以参考一下蒟蒻的Blog) 这时候令人头 ...
- 洛谷.4172.[WC2006]水管局长(LCT Kruskal)
题目链接 洛谷(COGS上也有) 不想去做加强版了..(其实处理一下矩阵就好了) 题意: 有一张图,求一条x->y的路径,使得路径上最长边尽量短并输出它的长度.会有<=5000次删边. 这 ...
- [洛谷P4172] WC2006 水管局长
问题描述 SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水 ...
- luogu P4172 [WC2006]水管局长 LCT维护动态MST + 离线
Code: #include<bits/stdc++.h> #define maxn 1200000 #define N 120000 using namespace std; char ...
- 洛谷4172 WC2006水管局长(LCT维护最小生成树)
这个题和魔法森林感觉有很相近的地方啊 同样也是维护一个类似最大边权最小的生成树 但是不同的是,这个题是有\(cut\)和询问,两种操作.... 这可如何是好啊? 我们不妨倒着来考虑,假设所有要\(cu ...
- P4172 [WC2006]水管局长 LCT维护最小生成树
\(\color{#0066ff}{ 题目描述 }\) SC 省 MY 市有着庞大的地下水管网络,嘟嘟是 MY 市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的 ...
- 洛谷 4172 [WC2006]水管局长
[题解] 我们把操作倒过来做,就变成了加边而不是删边.于是用LCT维护动态加边的最小生成树就好了.同样要注意把边权变为点权. #include<cstdio> #include<al ...
- 【洛谷P4172】水管局长
题目大意:给定 N 个点,M 条边的无向图,支持两种操作:动态删边和查询任意两点之间路径上边权的最大值最小是多少. 题解: 引理:对原图求最小生成树,可以保证任意两点之间的路径上边权的最大值取得最小值 ...
- P4172 [WC2006]水管局长(LCT)
P4172 [WC2006]水管局长 LCT维护最小生成树,边权化点权.类似 P2387 [NOI2014]魔法森林(LCT) 离线存储询问,倒序处理,删边改加边. #include<iostr ...
随机推荐
- [Cpp primer] Library vector Type
#include<vector> using std::vector; //Vector is a container. //It has a collection of same typ ...
- Django学习---缓存
缓存 由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存. 缓存将一个某个views的返回值保存至内存或者memcach ...
- 用CSS制作小三角提示符号
今天在项目中遇到了如下图的切图要求. 对,重点就是那个小三角提示符号. html 结构如下 <div class="wrap"> <div class=" ...
- 第一章IP:网际协议
I P是T C P / I P协议族中最为核心的协议.所有的 T C P.U D P.I C M P及I G M P数据都以I P数据报格式传输(见图 1 - 4).许多刚开始接触 T C P / I ...
- HTTP之Tcp/Ip协议的工作原理
计算机与网络设备要相互通信,双方就必须基于相同的方法.比如,如何探测到通信目标.由哪一边先发起通信.使用哪种语言进行通信.怎样结束通信等规则都需要事先确定.不同的硬件.操作系统之间的通信,所有的这一切 ...
- Vue2不使用Vuex如何实现兄弟组件间的通信
在一些正规的大型项目的企业级开发过程中我们一般会引入Vuex来对Vue所有组件进行状态管理,可以轻松实现各组件间的通信.但是有时候做做自己的小项目,没有必要使用Vuex时,如何简单的实现组件间的通信? ...
- ReportMachine 打印机横向
Portrait 纵向 landscape 横向 RM_reg.pas :TRMPageSetupForm 打印机设置RM_PageSetup.dfm TRMPageSetting定义在RM_Pri ...
- 自动删除 Elasticsearch 索引
#!/bin/bash # author: Wang XiaoQiang# crontab -e# 0 0 * * * /root/script/del_esindex.sh # auto delet ...
- 后端生成二维码 - C#生成二维码(QR)
最近在github上找到一个相对比较好的C#二维码生成类库.在这里和大家分享一下. github地址:https://github.com/codebude/QRCoder 把解决方案下载下来,编译生 ...
- Python实现七牛云视频播放
这篇文章是使用Python的Web框架Django Rest Framework来提供视频相关的api接口,主要功能包括视频上传.视频转码.视频访问授权.删除视频文件.视频截图功能. 七牛云上的基本概 ...