☆ [WC2006] 水管局长 「LCT动态维护最小生成树」
题目类型:\(LCT\)动态维护最小生成树
传送门:>Here<
题意:给出一张简单无向图,要求找到两点间的一条路径,使其最长边最小。同时有删边操作
解题思路
两点间路径的最长边最小,也就是等同于要求最小生成树。因此如果没有删边操作,那么只要\(Kruscal\)一遍就好了。
然而现在需要删边,也就是意味着最小生成树需要不停地重构……那怎么维护呢?
考虑离线,倒着处理所有的删边。于是乎就成为了一条一条加边。这就是标准的\(LCT\)动态维护最小生成树了。具体方法就是:如果两个点不在同一颗树中,那么\(link\)(注意,我们可以用\(findroot\)操作直接充当并查集的作用,常数稍大);否则一定出现环,查询原本环中的最大边,如果大于当前边,那么\(cut\)那条边,\(link\)这条边。
因此,我们刚开始用所有不会被删去的边维护一个最小生成树(注意要用\(kruscal\),不要动态维护,会被卡),然后再动态维护即可。
细节还是非常多的……
\(LCT\)维护点权最大值,如何来维护生成树呢?考虑将每条边变为一个点,也就是建立虚拟点,编号为\(i\)的边即为\(N+i\)。令虚拟点的权值为这条边的权值,原本的节点的权值为0,这样在查询路径上点权的最大值就好了。
如何确定被删除的边对应的权值是多少?一种思路是用邻接矩阵,要么就是用\(map\)
如果我们仅仅就是查找最大值,那么如何将其\(cut\)呢?显然我们不应该存值,而是应该存最大值的节点编号。
细节太恐怖了,调试了一晚上+一早上,结果发现是在判边的时候忘记用\(q[i]\)了……
反思
当在线无法处理的时候,可以考虑离线,将问题转化为已知的可解决的问题。
Code
/*By DennyQi 2018*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
#define int long long
const int MAXN = 200010;
const int MAXM = 200010;
const int MAXS = MAXN+MAXM;
const int INF = 1061109567;
inline int Max(const int a, const int b){ return (a > b) ? a : b; }
inline int Min(const int a, const int b){ return (a < b) ? a : b; }
inline int read(){
int x = 0; int w = 1; register char c = getchar();
for(; c ^ '-' && (c < '0' || c > '9'); c = getchar());
if(c == '-') w = -1, c = getchar();
for(; c >= '0' && c <= '9'; c = getchar()) x = (x<<3) + (x<<1) + c - '0'; return x * w;
}
struct Edge{
int u,v,w;
}e[MAXM];
int N,M,Q,top;
int X[MAXM],Y[MAXM],K[MAXM],val[MAXN],ans[MAXM],idmax[MAXS],q[MAXN];
bool used[MAXM];
map <pair<int,int>, int> E;
struct LinkCutTree{
int fa[MAXS],ch[MAXS][2];
bool tag[MAXS];
inline bool rson(int f, int x){
return ch[f][1] == x;
}
inline bool isroot(int x){
return ch[fa[x]][rson(fa[x],x)] != x;
}
inline void pushup(int x){
idmax[x] = x;
if(val[idmax[ch[x][0]]] > val[idmax[x]]) idmax[x] = idmax[ch[x][0]];
if(val[idmax[ch[x][1]]] > val[idmax[x]]) idmax[x] = idmax[ch[x][1]];
}
inline void rotate(int x){
if(!x || !fa[x]) return;
int f = fa[x], gf = fa[f];
int p = rson(f, x), q = !p;
if(!isroot(f)) ch[gf][rson(gf,f)] = x; fa[x] = gf;
ch[f][p] = ch[x][q], fa[ch[x][q]] = f;
ch[x][q] = f, fa[f] = x;
pushup(f), pushup(x);
}
inline void reverse(int x){
if(!isroot(x)) reverse(fa[x]);
if(!tag[x]) return;
tag[x] = 0;
swap(ch[x][0], ch[x][1]);
if(ch[x][0]) tag[ch[x][0]] ^= 1;
if(ch[x][1]) tag[ch[x][1]] ^= 1;
}
inline void splay(int x){
reverse(x);
while(!isroot(x)){
if(isroot(fa[x])){
rotate(x); break;
}
if(rson(fa[fa[x]],fa[x]) ^ rson(fa[x],x)) rotate(x); else rotate(fa[x]);
rotate(x);
}
}
inline void access(int x){
for(int y = 0; x; y = x, x = fa[x]){
splay(x);
ch[x][1] = y;
pushup(x);
}
}
inline void mroot(int x){
access(x);
splay(x);
tag[x] ^= 1;
}
inline int findroot(int x){
access(x);
splay(x);
while(ch[x][0]) x = ch[x][0];
return x;
}
inline void split(int x, int y){
mroot(x);
access(y);
splay(y);
}
inline void link(int x, int y){
if(findroot(x) == findroot(y)) return;
mroot(x);
fa[x] = y;
}
inline void cut(int x, int y){
split(x, y);
ch[y][0] = fa[x] = 0;
pushup(y);
}
}qxz;
inline bool cmp(const Edge& a, const Edge& b){
return a.w < b.w;
}
signed main(){
N = read(), M = read(), Q = read();
for(int i = 1; i <= M; ++i){
e[i].u = read(), e[i].v = read(), e[i].w = read();
if(e[i].u > e[i].v) swap(e[i].u, e[i].v);
}
sort(e+1, e+M+1, cmp);
for(int i = 1; i <= M; ++i){
val[N+i] = e[i].w;
E[make_pair(e[i].u,e[i].v)] = i;
}
for(int i = 1; i <= Q; ++i){
K[i] = read(), X[i] = read(), Y[i] = read() ;
if(X[i] > Y[i]) swap(X[i], Y[i]);
if(K[i] == 2){
q[i] = E[make_pair(X[i],Y[i])];
used[q[i]] = 1;
}
}
int x,y,cnt(0);
for(int i = 1; i <= M && cnt < N-1; ++i){
if(!used[i]){
x = e[i].u, y = e[i].v;
if(qxz.findroot(x) != qxz.findroot(y)){
qxz.link(x, N+i);
qxz.link(y, N+i);
++cnt;
}
}
}
for(int i = Q; i; --i){
x = X[i], y = Y[i];
if(K[i] == 2){
qxz.split(x, y);
if(val[idmax[y]] > e[q[i]].w){
int temp = idmax[y],t1 = e[temp-N].u, t2 = e[temp-N].v;
qxz.cut(t1, temp), qxz.cut(t2, temp);
qxz.link(x, N+q[i]), qxz.link(y, N+q[i]);
}
}
else{
qxz.split(x, y);
ans[++top] = val[idmax[y]];
}
}
while(top) printf("%lld\n", ans[top--]);
return 0;
}
☆ [WC2006] 水管局长 「LCT动态维护最小生成树」的更多相关文章
- ☆ [NOI2014] 魔法森林 「LCT动态维护最小生成树」
题目类型:\(LCT\)动态维护最小生成树 传送门:>Here< 题意:带权无向图,每条边有权值\(a[i],b[i]\).要求一条从\(1\)到\(N\)的路径,使得这条路径上的\(Ma ...
- [BZOJ2594] [WC2006]水管局长(Kruskal+LCT)
[BZOJ2594] [WC2006]水管局长(Kruskal+LCT) 题面 SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可 ...
- P4172 [WC2006]水管局长(LCT)
P4172 [WC2006]水管局长 LCT维护最小生成树,边权化点权.类似 P2387 [NOI2014]魔法森林(LCT) 离线存储询问,倒序处理,删边改加边. #include<iostr ...
- bzoj 2594: [Wc2006]水管局长数据加强版 动态树
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 934 Solved: 291[Submit][Sta ...
- [WC2006]水管局长(LCT)
题目大意: 给定一张图,支持删边,求两点的路径中所有权值的最大值的最小值,貌似很绕的样子 由于有删边,不难想到\(LCT\),又因为\(LCT\)不支持维护图,而且只有删边操作,于是我们考虑时间回溯. ...
- 【洛谷4172】 [WC2006]水管局长(LCT)
传送门 洛谷 BZOJ Solution 如果不需要动态的话,那就是一个裸的最小生成树上的最大边权对吧. 现在动态了的话,把这个过程反着来,就是加边对吧. 现在问题变成了怎么动态维护加边的最小生成树, ...
- [BZOJ2594][WC2006]水管局长加强版(LCT+Kruskal)
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 4452 Solved: 1385[Submit][S ...
- 洛谷P4172 [WC2006]水管局长(lct求动态最小生成树)
SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水管的路径, ...
- 洛谷P4172 [WC2006]水管局长 (LCT,最小生成树)
洛谷题目传送门 思路分析 在一个图中,要求路径上最大边边权最小,就不难想到最小生成树.而题目中有删边的操作,那肯定是要动态维护啦.直接上LCT维护边权最小值(可以参考一下蒟蒻的Blog) 这时候令人头 ...
随机推荐
- 如何解决angular不自动生成spec.ts文件
"schematics":{ "@schematics/angular:component": { "styleext": ...
- border,padding,margin盒模型理解
安静的敲着键盘,已势不可挡的姿势逼近php,我想我是一个幸福的人,未来不可期,做好现在,偶尔写着自己能看懂的API,慢慢悠悠的回味一下前端基础知识. 本文盒模型理解. <!DOCTYPE htm ...
- Scrapped or attached views may not be recycled
在使用recycleView的时候出现了错误Scrapped or attached views may not be recycled 原因: view没有被recycled,recyclerVie ...
- Python 使用Python远程连接并操作InfluxDB数据库
使用Python远程连接并操作InfluxDB数据库 by:授客 QQ:1033553122 实践环境 Python 3.4.0 CentOS 6 64位(内核版本2.6.32-642.el6.x86 ...
- QT获取本机IP和Mac地址
#include <QNetworkInterface> #include <QList> void MainWindow::getIPPath() { QString str ...
- Windows Server 2012 NIC Teaming 网卡绑定介绍及注意事项
Windows Server 2012 NIC Teaming 网卡绑定介绍及注意事项 转载自:http://www.it165.net/os/html/201303/4799.html Window ...
- Ehcache入门经典:第二篇ehcache.xml的参数
继续第一篇 diskStorepath:指定在硬盘上存储对象的路径path属性可以配置的目录有: user.home(用户的家目录) user.dir(用户当前的工作目录) java.io.tmpdi ...
- SQLServer之创建显式事务
显式事务定义 显式事务以 BEGIN TRANSACTION 语句开始,并以 COMMIT 或 ROLLBACK 语句结束. 备注 BEGIN TRANSACTION 使 @@TRANCOUNT 按 ...
- Unity ECS 视频笔记
视频摘要 本文视频资料:使用Entity Component System开发<快乐的Minecraft>游戏 使用Unity2018及以上版本才有ECS功能. 本文是看视频的一些摘要. ...
- Spring Boot 2.x 快速入门(下)HelloWorld示例详解
上篇 Spring Boot 2.x 快速入门(上)HelloWorld示例 进行了Sprint Boot的快速入门,以实际的示例代码来练手,总比光看书要强很多嘛,最好的就是边看.边写.边记.边展示. ...