[bzoj2594][Wc2006]水管局长数据加强版——lct+离线
Brief Description
您有一个无向带权图,您需要支持两种操作。
- 询问两个点之间的最大权最小路径。
- 删除一条边。
Algorithm Design
我们首先提出一个猜想:最优路径一定在原图的一个最小生成森林上,证明如下:
假设最优路径有\(\phi\)条边不再最小生成森林上,我们考察其中的一条边,根据定义,生成森林中一定有一条路径链接这条边的顶点且权值和小于这条边,那么我们如果使用这条路径代替这条边,\(\phi\)会减小而最大权不会增加,所以根据反证法我们就可以知道这个结论的正确性。
有了这个引理之后我们可以把原题转化为维护一个最小生成森林,这显然可以使用lct维护。
考虑细节。因为lct不是太支持删一条边,所以我们离线地倒过来做。
另外我们需要记录每条边是否被删。我开始使用了map,然而不知道为什么一直RE。后来改用二分查找就好了。所以——
抵制STL从我做起!(逃#
Code
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
const int maxv = 1500005;
int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
struct data {
int from, to, dat, br, id;
data(int x = 0, int y = 0, int z = 0, int i = 0, int j = 0)
: from(x), to(y), dat(z), br(i), id(j) {}
bool operator<(const data b) const {
return this->from < b.from || ((this->from == b.from) && (this->to < b.to));
}
} a[1000005];
struct req {
int opt, x, y, id, ans;
req(int x = 0, int y = 0, int z = 0, int k = 0, int fuck = 0) {
this->opt = x;
this->x = y;
this->y = z;
this->id = k;
this->ans = fuck;
}
} q[100005];
bool cmp(data a, data b) { return a.dat < b.dat; }
bool cmp2(data a, data b) { return a.id < b.id; }
int n, m, qaq, f[maxv], max[maxv], val[maxv], tot, maxnum[maxv];
int fa[maxv], ch[maxv][2];
bool rev[maxv];
int bisearch(int u, int v) {
int l = 1, r = m;
while (l <= r) {
int mid = (l + r) >> 1;
if (a[mid].from < u || (a[mid].from == u && a[mid].to < v))
l = mid + 1;
else if (a[mid].from == u && a[mid].to == v)
return mid;
else
r = mid - 1;
}
return -1;
}
bool isroot(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
void pushdown(int k) {
if (rev[k]) {
rev[k] ^= 1;
rev[ch[k][0]] ^= 1;
rev[ch[k][1]] ^= 1;
std::swap(ch[k][0], ch[k][1]);
}
}
void update(int x) {
maxnum[x] = x;
int l = maxnum[ch[x][0]], r = maxnum[ch[x][1]];
if (val[l] > val[maxnum[x]])
maxnum[x] = l;
if (val[r] > val[maxnum[x]])
maxnum[x] = r;
max[x] = val[maxnum[x]];
}
void zig(int x) {
int y = fa[x], z = fa[y], l = (ch[y][1] == x), r = l ^ 1;
if (!isroot(y))
ch[z][ch[z][1] == y] = x;
fa[ch[y][l] = ch[x][r]] = y;
fa[ch[x][r] = y] = x;
fa[x] = z;
update(y);
update(x);
}
void splay(int x) {
int s[maxv], top = 0;
s[++top] = x;
for (int i = x; !isroot(i); i = fa[i])
s[++top] = fa[i];
while (top)
pushdown(s[top--]);
for (int y; !isroot(x); zig(x)) {
if (!isroot(y = fa[x])) {
zig((ch[fa[y]][0] == y) == (ch[y][0] == x) ? y : x);
}
}
update(x);
}
void access(int x) {
for (int t = 0; x; t = x, x = fa[x]) {
splay(x);
ch[x][1] = t;
update(x);
}
}
void makeroot(int x) {
access(x);
splay(x);
rev[x] ^= 1;
}
void split(int x, int y) {
makeroot(y);
access(x);
splay(x);
}
void link(int x, int y) {
makeroot(x);
fa[x] = y;
}
void cut(int x, int y) {
makeroot(x);
access(y);
splay(y);
ch[y][0] = fa[x] = 0;
}
void init() {
memset(val, 0, sizeof(val));
n = read();
m = read();
qaq = read();
for (int i = 1; i <= n; i++)
f[i] = i;
for (int i = 1; i <= m; i++) {
int x = read(), y = read(), z = read();
if (x > y)
std::swap(x, y);
a[i] = data(x, y, z);
}
std::sort(a + 1, a + 1 + m, cmp);
for (int i = 1; i <= m; i++) {
a[i].id = i;
val[n + i] = a[i].dat;
maxnum[n + i] = n + i;
}
std::sort(a + 1, a + 1 + m);
for (int i = 1; i <= qaq; i++) {
int x = read(), y = read(), z = read();
q[i] = req(x, y, z);
if (x == 2) {
if (q[i].x > q[i].y)
std::swap(q[i].x, q[i].y);
int t = bisearch(q[i].x, q[i].y);
a[t].br = 1;
q[i].id = a[t].id;
}
}
}
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
void kruskal() {
std::sort(a + 1, a + m + 1, cmp2);
tot = 0;
for (int i = 1; i <= m; i++) {
if (!a[i].br) {
int u = a[i].from, v = a[i].to, x = find(u), y = find(v);
if (x != y) {
f[x] = y;
link(u, i + n);
link(v, i + n);
tot++;
if (tot == n - 1)
break;
}
}
}
}
void solve() {
for (int i = qaq; i >= 1; i--) {
int op = q[i].opt, x = q[i].x, y = q[i].y;
if (op == 1) {
split(x, y);
q[i].ans = val[maxnum[x]];
}
if (op == 2) {
int k = q[i].id;
split(x, y);
int t = maxnum[x];
if (a[k].dat < val[t]) {
cut(a[t - n].from, t);
cut(a[t - n].to, t);
link(x, k + n);
link(y, k + n);
}
}
}
for (int i = 1; i <= qaq; i++) {
if (q[i].opt == 1)
printf("%d\n", q[i].ans);
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input", "r", stdin);
#endif
init();
kruskal();
solve();
return 0;
}
[bzoj2594][Wc2006]水管局长数据加强版——lct+离线的更多相关文章
- [bzoj2594][Wc2006]水管局长数据加强版 (lct)
论蒟蒻的自我修养T_T.. 和noi2014魔法森林基本一样...然而数据范围大得sxbk...UPD:这题如果用lct判联通的话可能会被卡到O(mlogm)..所以最好还是用并查集吧 一开始数组开太 ...
- BZOJ2594 [Wc2006]水管局长数据加强版 LCT kruskal
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2594 题意概括 N个点的图,M条带权边.(N<=100000,M<=1000000) ...
- bzoj2594 [Wc2006]水管局长数据加强版——LCT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2594 时间倒序一下,就是 魔法森林 那道题: 有个不解的地方,是 access 里面关于 p ...
- BZOJ2594: [Wc2006]水管局长数据加强版
题解: 裸LCT+离线+二分+MST... 代码:(几乎摘抄自hzwer) #include<cstdio> #include<cstdlib> #include<cma ...
- BZOJ 2594: [Wc2006]水管局长数据加强版 [LCT kruskal]
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 2917 Solved: 918[Submit][St ...
- BZOJ 2594: [Wc2006]水管局长数据加强版( LCT )
离线然后就是维护加边的动态MST, Link cut tree秒掉..不过我写+调了好久...时间复杂度O(NlogN + MlogM) ------------------------------- ...
- BZOJ2594 [Wc2006]水管局长数据加强版 【LCT维护最小生成树】
题目 SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水管的 ...
- [BZOJ2594] [Wc2006]水管局长数据加强版(LCT + kruskal + 离线)
传送门 WC这个题真是丧心病狂啊,就是想学习一下怎么处理边权,给我来了这么一个破题! ORZ hzwer 临摹黄学长代码233 但还是复杂的一匹 理一下思路吧 题目大意:给定一个无向图,多次删除图中的 ...
- BZOJ 2594: [Wc2006]水管局长数据加强版 (LCT维护最小生成树)
离线做,把删边转化为加边,那么如果加边的两个点不连通,直接连就行了.如果联通就找他们之间的瓶颈边,判断一下当前边是否更优,如果更优就cut掉瓶颈边,加上当前边. 那怎么维护瓶颈边呢?把边也看做点,向两 ...
随机推荐
- jmeter的基本使用过程
jmeter的基本使用过程 接下来几周,我将通过视频的方式,录制下来jmeter的基本用法,方便大家参考学习 可能导图会随时调整
- Python 学习笔记之 Numpy 库——数组基础
1. 初识数组 import numpy as np a = np.arange(15) a = a.reshape(3, 5) print(a.ndim, a.shape, a.dtype, a.s ...
- VS2013 启用avalon 智能提示 Intelligence
第一步: 关闭VS2013. 第二步: 进入目录: C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Packages\schem ...
- TemplateDoesNotExist 异常
TemplateDoesNotExist 异常 如果 get_template() 找不到给定名称的模板,将会引发一个 TemplateDoesNotExist 异常.假设你的 DEBUG项设置为 T ...
- 201621123033 《Java程序设计》第13周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主等)-分组完成 为了让你的系统可以被多个用户通过网 ...
- [BinaryTree] 二叉搜索树(二叉查找树、二叉排序树)
二叉查找树(BinarySearch Tree,也叫二叉搜索树,或称二叉排序树BinarySort Tree)或者是一棵空树,或者是具有下列性质的二叉树: (1)若它的左子树不为空,则左子树上所有结点 ...
- MongoDB 存储日志数据
MongoDB 存储日志数据 https://www.cnblogs.com/nongchaoer/archive/2017/01/11/6274242.html 线上运行的服务会产生大量的运行及访问 ...
- 【bzoj3653】谈笑风生 DFS序+树状数组
题目描述 给出一棵以1为根的有根树,q次询问,每次询问给出a和k,求点对 (b,c) 的数目,满足:a.b.c互不相同,b与a距离不超过k,且a和b都是c的祖先. 输入 输入文件的第一行含有两个正整数 ...
- $.ajax()方法参数总结
url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址.type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和d ...
- [洛谷P1951]收费站_NOI导刊2009提高(2)
题目大意:有一张$n$个点$m$条边的图,每个点有一个权值$w_i$,有边权,询问从$S$到$T$的路径中,边权和小于$s$,且$\max\limits_{路径经过k}\{w_i\}$最小,输出这个最 ...