【题解】[WC2006]水管局长
感觉这题好强啊……本来以为能过,结果毫无疑问的被ge了一顿……在这里记录一下做的过程,也免得以后又忘记啦。
首先,我们应看出在这张图上,要让经过的水管最长的最短,就是要维护一棵动态的最小生成树。只是删除边不是很好操作,所以我们将删边改为加边,反向处理。如果发现新加的边可以更新最小生成树,我们就应该更新图,删去原图中最大的一条边。所以在LCT上我们要多维护一个信息,即当前最大的那条边是哪一条边。至此,大部分的难点都已解决,只是LCT维护的是点上的信息,而这道题的权值都在边上。我们将边转化成点,要将要链接的两个点分别与另一点相连,将信息存在另一个点上即可。
The road is tough and there's still a long way to go. Try your best and don't regret.
#include <bits/stdc++.h>
using namespace std;
#define maxn 150005
#define maxm 1500000
int n, m, opt[maxm], cnp, x[maxm], y[maxm];
int q[maxm], id, T[maxm];
int tot, ans[maxn], mark[maxn];
map <int, int> Map[maxn];
struct node
{
int fa, sum, rev;
int id, v, ch[];
}P[maxm]; struct edge
{
int u, v, t;
friend bool operator <(edge a, edge b)
{
return a.t < b.t;
}
}E[maxn]; int read()
{
int x = , k = ;
char c;
c = getchar();
while(c < '' || c > '') { if(c == '-') k = -; c = getchar(); }
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * k;
} void push_down(int x)
{
if(!P[x].rev) return;
swap(P[x].ch[], P[x].ch[]);
P[P[x].ch[]].rev ^= , P[P[x].ch[]].rev ^= ;
P[x].rev = ;
} void update(int x)
{
if(!x) return;
P[x].id = x, P[x].sum = P[x].v;
if(P[P[x].ch[]].sum > P[x].sum) P[x].sum = P[P[x].ch[]].sum, P[x].id = P[P[x].ch[]].id;
if(P[P[x].ch[]].sum > P[x].sum) P[x].sum = P[P[x].ch[]].sum, P[x].id = P[P[x].ch[]].id;
} bool is_root(int x) { return (P[P[x].fa].ch[] != x) && (P[P[x].fa].ch[] != x); } void rotate(int x)
{
int f = P[x].fa, gf = P[f].fa, k = P[f].ch[] == x;
if(!is_root(f)) P[gf].ch[P[gf].ch[] == f] = x;
P[x].fa = gf, P[f].fa = x;
P[f].ch[k] = P[x].ch[k ^ ], P[P[x].ch[k ^ ]].fa = f;
P[x].ch[k ^ ] = f;
update(f), update(x);
} void Splay(int x)
{
int top = ; q[top] = x;
for(int i = x; !is_root(i); i = P[i].fa) q[++ top] = P[i].fa;
for(int i = top; i; i --) push_down(q[i]);
while(!is_root(x))
{
int f = P[x].fa, gf = P[f].fa;
if(!is_root(f)) (P[f].ch[] == x) ^ (P[gf].ch[] == f) ? rotate(x) : rotate(f);
rotate(x);
}
update(x);
} void Access(int x)
{
for(int ff = ; x; ff = x, x = P[x].fa)
{
Splay(x);
P[x].ch[] = ff;
update(x);
}
} void make_root(int x) { Access(x); Splay(x); P[x].rev ^= ; }
void Split(int u, int v) { make_root(u); Access(v); Splay(v); }
void Link(int u, int v) { make_root(u); P[u].fa = v; }
void Cut(int u, int v) { Split(u, v); P[v].ch[] = P[u].fa = ; } int Get_fa(int x)
{
Access(x); Splay(x);
while(P[x].ch[]) x = P[x].ch[];
return x;
} int main()
{
n = read(), m = read();
int q = read();
for(int i = ; i <= m; i ++)
{
int u = read(), v = read(), w = read(); T[i] = w;
E[++ cnp].u = u; E[cnp].v = v, E[cnp].t = w;
}
sort(E + , E + + m);
for(int i = ; i <= m; i ++) Map[E[i].u][E[i].v] = Map[E[i].v][E[i].u] = i;
for(int i = ; i <= q; i ++)
{
opt[i] = read(); x[i] = read(); y[i] = read();
if(opt[i] == ) mark[Map[x[i]][y[i]]] = ;
}
for(int i = ; i <= m; i ++)
{
int u = E[i].u, v = E[i].v, t = E[i].t;
if(!mark[Map[u][v]] && Get_fa(u) != Get_fa(v))
{
id = i + n, P[id].sum = P[id].v = t;
Link(u, id), Link(id, v);
}
}
for(int i = q; i; i --)
{
Split(x[i], y[i]);
if(opt[i] == ) // find a route
ans[++ tot] = P[y[i]].sum;
else // add edge
{
int now = n + Map[x[i]][y[i]];
int tem = E[now - n].t;
if(tem < P[y[i]].sum)
{
P[now].v = tem; tem = P[y[i]].id;
Cut(tem, E[tem - n].u); Cut(tem, E[tem - n].v);
Link(now, x[i]), Link(now, y[i]);
}
}
}
for(int i = tot; i; i --)
printf("%d\n", ans[i]);
return ;
}
【题解】[WC2006]水管局长的更多相关文章
- BZOJ2594: [Wc2006]水管局长数据加强版
题解: 裸LCT+离线+二分+MST... 代码:(几乎摘抄自hzwer) #include<cstdio> #include<cstdlib> #include<cma ...
- P4172 [WC2006]水管局长
P4172 [WC2006]水管局长 前言 luogu数据太小 去bzoj,他的数据大一些 思路 正着删不好维护 那就倒着加,没了 LCT维护他的最小生成树MST 树上加一条边肯定会有一个环 看看环上 ...
- bzoj 2594: [Wc2006]水管局长数据加强版 动态树
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 934 Solved: 291[Submit][Sta ...
- BZOJ 2594: [Wc2006]水管局长数据加强版( LCT )
离线然后就是维护加边的动态MST, Link cut tree秒掉..不过我写+调了好久...时间复杂度O(NlogN + MlogM) ------------------------------- ...
- [bzoj2594][Wc2006]水管局长数据加强版 (lct)
论蒟蒻的自我修养T_T.. 和noi2014魔法森林基本一样...然而数据范围大得sxbk...UPD:这题如果用lct判联通的话可能会被卡到O(mlogm)..所以最好还是用并查集吧 一开始数组开太 ...
- BZOJ 2594: [Wc2006]水管局长数据加强版 [LCT kruskal]
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 2917 Solved: 918[Submit][St ...
- BZOJ_2594_[Wc2006]水管局长数据加强版_LCT
BZOJ_2594_[Wc2006]水管局长数据加强版_LCT Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供 ...
- P4172 [WC2006]水管局长(LCT)
P4172 [WC2006]水管局长 LCT维护最小生成树,边权化点权.类似 P2387 [NOI2014]魔法森林(LCT) 离线存储询问,倒序处理,删边改加边. #include<iostr ...
- [BZOJ2594][WC2006]水管局长加强版(LCT+Kruskal)
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 4452 Solved: 1385[Submit][S ...
- [BZOJ2594] [WC2006]水管局长(Kruskal+LCT)
[BZOJ2594] [WC2006]水管局长(Kruskal+LCT) 题面 SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可 ...
随机推荐
- 基于ftp服务的三种登录方式及其相关的访问控制和优化
ftp(简单文件传输协议),是一种应用广泛的网络文件传输协议和服务,占用20和21号端口,主要用于资源的上传和下载. 在linux对于ftp同widows一样具有很多的种类,这里主要介绍vsfptd( ...
- php常见面试(Smarty及相关知识)
1.Smarty简介: 是一种php模板引擎,它分开了后台程序和前台页面内容,使程序员和美工分工合作.还有其它模版引擎实现同样的目的. 2.Smarty使用: 建立php文件: 包含smarty类文件 ...
- PHP实现微信红包算法和微信红包的架构设计简介
微信红包的架构设计简介: 原文:https://www.zybuluo.com/yulin718/note/93148 @来源于QCon某高可用架构群整理,整理朱玉华. 背景:有某个朋友在朋友圈咨询微 ...
- C语言:类型、运算符、表达式
看了一天书,有点累了.就写写随笔记录一下今天的复习成果吧. C语言的基本数据类型 数值型:整型数,浮点数,布尔数,复数和虚数. 非数值型:字符. 整数最基本的是int,由此引出许多变式诸如有符号整数s ...
- Json格式化时间
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")@JsonFormat(timezone = "GMT+8", ...
- codeforces 845A Chess Tourney
参考:https://blog.csdn.net/zhongyuchen/article/details/77478039 #include <iostream> #include < ...
- Hive 函数之内置运算符
本章介绍Hive的内置运算符.在Hive有四种类型的运算符: 关系运算符 算术运算符 逻辑运算符 复杂运算符 关系运算符 这些操作符被用来比较两个操作数.下表描述了在Hive中可用的关系运算符: 运算 ...
- RedHat安装Oracle后中文乱码
radhat7.1 Oracle11gr2 安装Oracle时忘记设置字符集,导致安装后中文乱码,中文变成"???????????" 分析原因是Oracle服务器端和客户端的字符集 ...
- CDH,CM下载
wget -c -r -nd -np -k -L -A rpm http://archive-primary.cloudera.com/cdh5/parcels/latest/ http://arch ...
- 关于 poorpool
poorpool 真名 chenyixiao.是一条傻逼题都不会做的没有脑子的咸鱼. 山西省临汾第一中学 高二 sx省队里最菜的那一个进队靠暴力. 普通的理科生,曾经爱好数学,然而到了高中发现自己所谓 ...