洛谷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 ...
随机推荐
- 22_java之File对象
01IO技术概述 * A:IO技术概述 * a: Output * 把内存中的数据存储到持久化设备上这个动作称为输出(写)Output操作 * b: Input * 把持久设备上的数据读取到内存中的这 ...
- 你应该使用 Django admin 的 9 个理由(转)
你应该使用 Django admin 的 9 个理由 “问题是,我问到的每个人都持反对意见,他们认为 admin 只限于超级用户,很不灵活并且是难以定制.”—来自 Reddit 的 andybak 我 ...
- Set8087CW
Set8087CWThis example accesses the Floating Point Unit (FPU) control register. Try turning floating ...
- SpringMVC前后台数据传递中Json格式的相互转换(前台显示格式、Json-lib日期处理)及Spring中的WebDataBinder浅析
两个方向: 一.前台至后台: Spring可以自动封装Bean,也就是说可以前台通过SpringMVC传递过来的属性值会自动对应到对象中的属性并封装成javaBean,但是只能是基本数据类型(int, ...
- AutoMapper差异内容备份
公司就得项目用的automapper 是 4.2.1 当时用的方法是:Mapper.CreateMap<source,sourceDto>(); 在最新版 6.0.1 中,这些个方法被删除 ...
- MySQL数据库篇之完整性约束和表关系
主要内容: 一.完整性约束 二.表关系 1️⃣ 完整性约束 (1)何为完整性约束? 约束条件与数据类型的宽度一样,都是可选参数. 作用:用于保证数据的完整性和一致性 (2)分类主要有以下五类: 1.n ...
- vmvare centos7 切换桌面和命令行模式
systemctl set-default multi-user.target 命令行 systemctl set-default graphical.target 桌面
- Github 使用的Markdown语言
简介 官方站点:http://daringfireball.net/projects/markdown/syntax 中文介绍:http://www.worldhello.net/gotgithub/ ...
- phpmailer配置qq邮箱
function send_email2($email = '*****@perspectivar.com'){ $this->autoRender = false; date_default_ ...
- oracle sql 数结构表id降序
UPDATE BAS_ORGANIZATION_TYPE T1SET T1.PARENTID=(select rn from (SELECT id,rownum rn FROM BAS_ORGANIZ ...