LGOJ4172 WC2006水管局长
首先声明,这份代码空间复杂度 \(O(n^2)\),瓶颈在给边打标记
由于博主太菜,懒得再改成低复杂度的打标记了,所以\(BZOJ\)的数据过不去
Description
给一张图,会有删边操作,求当前图路径\((x,y)\) 的最大边权
\(n \leq 10^3 ,m \le 10^5\)
或\(BZOJ\)版:\(n\le 10^5,m\le10^6\)
Solution
\]
明确两点:
1.这个题要\(LCT\)(因为有删边的操作)
2.这个题把询问离线下来,然后逆序加边更可做
这个题中LCT维护的是“边权”
这里有一个点边转化:
把每一个边拆成点,对端点连两条边(边权就是\(e[i].dis\)),这样就可以维护点权了,在\(link\)和\(cut\)操作中有点点不同
如下:
cut(e[tmp - n].from, tmp);
cut(e[tmp - n].to, tmp);
link(x[i], id[x[i]][y[i]] + n);
link(y[i], id[x[i]][y[i]] + n);
先给出图的最终态,然后求一个最小生成树
每个操作先打通链,也就是 \(split\)
然后对于询问操作,直接给\(s[y]\)
对于改边的操作
每一次加边,都会在生成树上面添加一个环,然后我们找到环上最大边 \(cut\) 就可以
(\(split\)已经通链了,所以直接断就可以)
然后\(link\)就行了
\]
Code
不是很难写吧
#include <bits/stdc++.h>
using namespace std;
#define int long long
namespace yspm {
inline int read() {
int res = 0, f = 1;
char k;
while (!isdigit(k = getchar()))
if (k == '-')
f = -1;
while (isdigit(k)) res = res * 10 + k - '0', k = getchar();
return res * f;
}
const int M = 1e6 + 10, N = 1e5 + 10;
int f[N], c[N][2], s[N], v[N], st[N], n, m, q, x[N], y[N], opt[N], fa[N], ans[N], id[2010][2010];
bool r[N];
struct node {
int from, to, dis, vis;
bool operator<(const node &a) const { return dis < a.dis; }
} e[M];
inline void push_up(int x) {
s[x] = max(s[c[x][0]], max(s[c[x][1]], v[x]));
return;
}
inline bool notroot(int x) { return c[f[x]][1] == x || c[f[x]][0] == x; }
inline void pushr(int x) {
swap(c[x][0], c[x][1]);
r[x] ^= 1;
return;
}
inline void push_down(int x) {
if (r[x]) {
if (c[x][0])
pushr(c[x][0]);
if (c[x][1])
pushr(c[x][1]);
}
return r[x] = 0, void();
}
inline void rotate(int tx) {
int ty = f[tx], z = f[ty], k = (c[ty][1] == tx), w = c[tx][!k];
if (notroot(ty))
c[z][c[z][1] == ty] = tx;
c[tx][!k] = ty;
c[ty][k] = w;
if (w)
f[w] = ty;
f[ty] = tx;
f[tx] = z;
return push_up(ty), push_up(tx);
}
inline void splay(int x) {
int y = x, z = 0;
st[++z] = y;
while (notroot(y)) st[++z] = y = f[y];
while (z) push_down(st[z--]);
while (notroot(x)) {
y = f[x], z = f[y];
if (notroot(y))
rotate((c[y][0] == x) ^ (c[z][0] == y) ? x : y);
rotate(x);
}
return push_up(y);
}
inline void access(int x) {
for (int y = 0; x; x = f[y = x]) splay(x), c[x][1] = y, push_up(x);
return;
}
inline void makeroot(int x) {
access(x);
splay(x);
pushr(x);
return;
}
inline int findroot(int x) {
access(x);
splay(x);
while (c[x][0]) push_down(x), x = c[x][0];
splay(x);
return x;
}
inline void split(int x, int y) {
makeroot(x);
access(y);
splay(y);
return;
}
inline void link(int x, int y) {
makeroot(x);
if (findroot(y) != x)
f[x] = y;
return;
}
inline void cut(int x, int y) {
makeroot(x);
if (findroot(y) == x && f[y] == x && !c[y][0])
f[y] = c[x][1] = 0, push_up(x);
return;
}
inline int find(int x, int val) {
if (v[x] == val)
return x;
return s[c[x][0]] == val ? find(c[x][0], val) : find(c[x][1], val);
}
inline int getf(int x) { return x == fa[x] ? x : fa[x] = getf(fa[x]); }
inline void merge(int x, int y) { fa[getf(x)] = getf(y); }
inline void Kruskal() {
for (int i = 1; i <= n; ++i) fa[i] = i;
for (int i = 1; i <= m; ++i) {
if (!e[i].vis && getf(e[i].from) != getf(e[i].to)) {
merge(e[i].from, e[i].to);
link(e[i].from, n + i);
link(e[i].to, n + i);
}
}
return;
}
signed main() {
n = read();
m = read();
q = read();
for (int i = 1; i <= m; ++i) e[i].from = read(), e[i].to = read(), e[i].dis = read();
sort(e + 1, e + m + 1);
for (int i = 1; i <= m; ++i) {
id[e[i].from][e[i].to] = id[e[i].to][e[i].from] = i;
v[n + i] = e[i].dis;
}
for (int i = 1; i <= q; ++i) {
opt[i] = read();
x[i] = read();
y[i] = read();
if (opt[i] == 2)
e[id[x[i]][y[i]]].vis = 1;
}
Kruskal();
int num = 0;
for (int i = q; i >= 1; --i) {
split(x[i], y[i]);
if (opt[i] == 1)
ans[++num] = s[y[i]];
else {
int tmp = find(y[i], s[y[i]]);
if (v[id[x[i]][y[i]] + n] < s[y[i]]) {
cut(e[tmp - n].from, tmp);
cut(e[tmp - n].to, tmp);
link(x[i], id[x[i]][y[i]] + n);
link(y[i], id[x[i]][y[i]] + n);
}
}
}
for (int i = num; i >= 1; --i) printf("%lld\n", ans[i]);
return 0;
}
} // namespace yspm
signed main() { return yspm::main(); }
LGOJ4172 WC2006水管局长的更多相关文章
- BZOJ2594: [Wc2006]水管局长数据加强版
题解: 裸LCT+离线+二分+MST... 代码:(几乎摘抄自hzwer) #include<cstdio> #include<cstdlib> #include<cma ...
- 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 ...
- P4172 [WC2006]水管局长
P4172 [WC2006]水管局长 前言 luogu数据太小 去bzoj,他的数据大一些 思路 正着删不好维护 那就倒着加,没了 LCT维护他的最小生成树MST 树上加一条边肯定会有一个环 看看环上 ...
- [BZOJ2594][WC2006]水管局长加强版(LCT+Kruskal)
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 4452 Solved: 1385[Submit][S ...
随机推荐
- 在Centos安装redis-孙志奇
最近在阿里云服务器上部署redis,遇到了很多的问题,经过不懈的努力终于配置成功, 按照下面的步骤一步一步来就好了 wget http://download.redis.io/releases/red ...
- junit基础学习之-多线程测试(6)
步骤: 1.定义单个TestRunner 2.重载单个TestRunner的runTest() 3.定义TestRunner数组,并添加多个TestRunner 4.MultiThreadedTest ...
- hdu4632 Palindrome subsequence 回文子序列个数 区间dp
Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/ ...
- HDU - 5591 ZYB's Game(博弈)
题意:A和B两人在1~N中选数字.已知1<=X<=N,谁先选中X谁就输.每当一个人选出一个不是X的数,裁判都会说明这个数比X大还是小,与此同时,可选范围随之缩小.已知A先选,求满足能让B赢 ...
- UVA - 116 Unidirectional TSP (单向TSP)(dp---多段图的最短路)
题意:给一个m行n列(m<=10, n<=100)的整数矩阵,从第一列任何一个位置出发每次往右,右上或右下走一格,最终到达最后一列.要求经过的整数之和最小.第一行的上一行是最后一行,最后一 ...
- 中国移动携手华为百度展示5G应用,实现8K视频传输
在今日举行的 2019 年百度云智峰会上,中国移动携手华为和百度,首次展示基于 SA 架构的 5G Vertical LAN (行业局域网)技术,承载 8K 实时会议系统,助力企业云办公.该技术可为合 ...
- CGridCtrl 添加button (CGridCellButton类)
#ifndef __GRID_CELL_BUTTON__ #define __GRID_CELL_BUTTON__ #include "../GridCtrl_src/GridCell.h& ...
- 量化交易回测系统---RQalpha、qstrade学习笔记
一.RQalpha github 地址 https://github.com/ricequant/rqalpha 1.运行test.py文件,显示 No module named 'logbook. ...
- python3.7使用etree遇到的问题
使用python3.6时安装好lxml时按照许多网上的教程来引入会发现etree没被引入进来 解决办法: 一.import lxml.htmletree = lxml.html.etree这样就可以使 ...
- java中的字符串String
一.String简介d 参考:https://www.cnblogs.com/zhangyinhua/p/7689974.html String类代表字符串. java.lang.String: Ja ...