洛谷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 ...
随机推荐
- Ceph的工作原理及流程
本文将对Ceph的工作原理和若干关键工作流程进行扼要介绍.如前所述,由于Ceph的功能实现本质上依托于RADOS,因而,此处的介绍事实上也是针对RADOS进行.对于上层的部分,特别是RADOS GW和 ...
- jQuery之事件和动画
1.加载DOM $(document).ready(function(){ }) 简写形式: $(function(){ }) 事件绑定: 合成事件 事件冒泡 移除事件 JQuery中的动画 show ...
- From Ruby array to JS array in Rails- 'quote'?
From Ruby array to JS array in Rails- 'quote'? <%= raw @location_list.as_json %>
- 封装basedao
package com.huawei.common; import java.sql.ResultSet;import java.sql.SQLException; public interface ...
- 配置siebel捕捉SQL语句
C:\Siebel\15.0.0.0.0\Client\BIN\siebel.exe /c c:\Siebel\15.0.0.0.0\Client\bin\chs\siebel.cfg /B &quo ...
- Oracle 下基于 DBMS_RESOURCE_MANAGER 包估算数据库存储 IO 性能
:first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { border: 0; m ...
- Zookeeper的基本概念和重要特性
目录 1. 什么是Zookeeper 2. Zookeeper集群角色 3. Zookeeper的数据模型 3.1 Znode的类型 3.2 Znode的结构 4. Zookeeper的事件监听机制 ...
- Java-CSV文件读取
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import ja ...
- faster-rcnn训练自己的数据集参考文章
https://www.cnblogs.com/CarryPotMan/p/5390336.html
- Java程序员常用工具类库 - 目录
有人说当你开始学习Java的时候,你就走上了一条不归路,在Java世界里,包罗万象,从J2SE,J2ME,J2EE三大平台,到J2EE中的13中核心技术,再到Java世界中万紫千红的Framework ...