[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掉瓶颈边,加上当前边. 那怎么维护瓶颈边呢?把边也看做点,向两 ...
随机推荐
- mysql8.0.14 安装
1.下载 地址:https://dev.mysql.com/downloads/mysql/ 找到zip压缩文件. 2.配置环境变量 把解压后的路径配置到环境变量中 3.安装 在解压后的文件夹中新建m ...
- Web框架本质及浅谈HTTP协议
Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. 半成品自定义web框架 impor ...
- 剑指offer-调整数组顺序使奇数位于偶数前面13
题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. class Solu ...
- JAVA的文件操作【转】
11.3 I/O类使用 由于在IO操作中,需要使用的数据源有很多,作为一个IO技术的初学者,从读写文件开始学习IO技术是一个比较好的选择.因为文件是一种常见的数据源,而且读写文件也是程序员进行IO编程 ...
- java设计模式之命令模式以及在java中作用
命令模式属于对象的行为模式.命令模式又称为行动(Action)模式或交易(Transaction)模式. 命令模式把一个请求或者操作封装到一个对象中.命令模式允许系统使用不同的请求把客户端参数化,对请 ...
- mysql语法总结
增: 删: 改: 查: 索引: 建: alter table: sql一些常用的经典语句,最后是select as的用法
- mongodb数据库高级操作
1.创建索引 2.索引名称 3.其他索引 4.explain 5.操作索引 6.高级特性 7.固定集合 8.导入导出 9.上锁 10.添加用户 11.主从复制
- NO2——最短路径
[Dijkstra算法] 复杂度O(n2) 权值必须非负 /* 求出点beg到所有点的最短路径 */ // 邻接矩阵形式 // n:图的顶点数 // cost[][]:邻接矩阵 // pre[i]记录 ...
- html5实现web app摇一摇换歌
微信可以摇歌曲,根据声音识别出歌曲,然后返回歌曲信息,利用html5的deviceOrientation特性和deviceMotion事件也可以在web app上实现类似于微信摇一摇的功能,原生的ap ...
- Java面试题-字符串操作
题目:输入一行字符,分别统计出其中英文字母,空格,数字和其他字符个数 //创建一个容器,用来保存结果,英文字母空格数组和其他字符做key,个数为value Map<String,Integer& ...