BZOJ 2594 水管局长数据加强版
LCT维护最小生成树
要求两点路径最大的最小,首先想到的肯定是最小生成树,再加上有删边操作,那就得用LCT维护了。
可是对于cut一条边,我们要时刻维护图中的最小生成树,需要把之前被我们淘汰的边找回,那无法处理,所以我们考虑和并查集一样的技巧,倒着离线处理问题。
我们首先把询问中要删的边全部过滤掉,然后维护其他边产生的最小生成树,然后倒着回答每一个询问,把原来的删边变成连边,这样就方便多啦。
每一个cut需要比较边的大小,所以我们可以用map来记录每条边的编号。
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 400005;
int n, m, q, tot, fa[N], mx[N], val[N], ch[N][2], id[N], rev[N], st[N], vis[N];
map<pair<int, int>, int> r;
vector<int> ans;
struct Edge {
int u, v, w;
bool operator < (const Edge &rhs) const {
return w < rhs.w;
}
}e[N<<2];
struct Query{ int opt, u, v; } query[N<<2];
int init(int v){
++tot;
id[tot] = tot, val[tot] = mx[tot] = v;
fa[tot] = ch[tot][0] = ch[tot][1] = rev[tot] = 0;
return tot;
}
bool isRoot(int x){
return ch[fa[x]][0] != x && ch[fa[x]][1] != x;
}
void reverse(int x){
rev[x] ^= 1, swap(ch[x][0], ch[x][1]);
}
void push_up(int x){
int l = ch[x][0], r = ch[x][1];
mx[x] = val[x], id[x] = x;
if(mx[l] > mx[x]) mx[x] = mx[l], id[x] = id[l];
if(mx[r] > mx[x]) mx[x] = mx[r], id[x] = id[r];
}
void push_down(int x){
if(rev[x]){
rev[x] ^= 1;
if(ch[x][0]) reverse(ch[x][0]);
if(ch[x][1]) reverse(ch[x][1]);
}
}
void rotate(int x){
int y = fa[x], z = fa[y], p = (ch[y][1] == x) ^ 1;
ch[y][p^1] = ch[x][p], fa[ch[x][p]] = y;
if(!isRoot(y)) ch[z][ch[z][1] == y] = x;
fa[x] = z, fa[y] = x, ch[x][p] = y;
push_up(y), push_up(x);
}
void splay(int x){
int pos = 0; st[++pos] = x;
for(int i = x; !isRoot(i); i = fa[i]) st[++pos] = fa[i];
while(pos) push_down(st[pos--]);
while(!isRoot(x)){
int y = fa[x], z = fa[y];
if(!isRoot(y)) (ch[y][0] == x) ^ (ch[z][0] == y) ? rotate(x) : rotate(y);
rotate(x);
}
push_up(x);
}
void access(int x){
for(int p = 0; x; p = x, x = fa[x])
splay(x), ch[x][1] = p, push_up(x);
}
void makeRoot(int x){
access(x), splay(x), reverse(x);
}
int findRoot(int x){
access(x), splay(x);
while(ch[x][0]) push_down(x), x = ch[x][0];
splay(x);
return x;
}
bool isConnect(int x, int y){
makeRoot(x);
return findRoot(y) == x;
}
void link(int x, int y){
makeRoot(x), fa[x] = y;
}
void split(int x, int y){
makeRoot(x), access(y), splay(y);
}
int main(){
n = read(), m = read(), q = read();
for(int i = 1; i <= n; i ++) init(0);
for(int i = 0; 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, e + m);
for(int i = 0; i < m; i ++) r[make_pair(e[i].u, e[i].v)] = i;
for(int i = 0; i < q; i ++){
query[i].opt = read(), query[i].u = read(), query[i].v = read();
if(query[i].u > query[i].v) swap(query[i].u, query[i].v);
if(query[i].opt == 2) vis[r[make_pair(query[i].u, query[i].v)]] = true;
}
int cnt = 0;
for(int i = 0; i < m; i ++){
if(cnt == n - 1) break;
if(vis[i]) continue;
int u = e[i].u, v = e[i].v, t = init(e[i].w);
if(isConnect(u, v)) continue;
link(u, t), link(t, v), cnt ++;
}
for(int i = q - 1; i >= 0; i --){
int u = query[i].u, v = query[i].v, opt = query[i].opt;
if(opt == 1){
split(u, v);
ans.push_back(mx[v]);
}
else{
split(u, v);
int d = r[make_pair(u, v)];
if(e[d].w > mx[v]) continue;
int tmp = id[v], t = init(e[d].w);
splay(tmp);
fa[ch[tmp][0]] = fa[ch[tmp][1]] = 0;
ch[tmp][0] = ch[tmp][1] = 0;
link(u, t), link(t, v);
}
}
for(int i = ans.size() - 1; i >= 0; i --){
printf("%d\n", ans[i]);
}
return 0;
}
BZOJ 2594 水管局长数据加强版的更多相关文章
- [BZOJ]2594 水管局长数据加强版(Wc2006)
失踪人口回归. LCT一直是小C的弱项,特别是这种维护链的信息的,写挂了就会调代码调到心态爆炸. 不过还好这一次的模板练习没有出现太多的意外. Description SC省MY市有着庞大的地下水管网 ...
- bzoj 2594: 水管局长数据加强版 Link-Cut-Tree
题目: Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公 ...
- BZOJ 2594 水管局长数据加强版(动态树)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2594 题意:给出一个无向图,边有权值.定义一条路径的长度为该路径所有边的最大值.两种操作 ...
- 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) ------------------------------- ...
- BZOJ 2594: [Wc2006]水管局长数据加强版 [LCT kruskal]
2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec Memory Limit: 128 MBSubmit: 2917 Solved: 918[Submit][St ...
- BZOJ 2594 【WC2006】 水管局长数据加强版
题目链接:水管局长数据加强版 好久没写博客了…… 上次考试的时候写了一发LCT,但是写挂了……突然意识到我已经很久没有写过LCT了,于是今天找了道题来练练手. 首先,LCT这里不讲.这道题要求支持动态 ...
- BZOJ2594: [Wc2006]水管局长数据加强版
题解: 裸LCT+离线+二分+MST... 代码:(几乎摘抄自hzwer) #include<cstdio> #include<cstdlib> #include<cma ...
- [bzoj2594][Wc2006]水管局长数据加强版 (lct)
论蒟蒻的自我修养T_T.. 和noi2014魔法森林基本一样...然而数据范围大得sxbk...UPD:这题如果用lct判联通的话可能会被卡到O(mlogm)..所以最好还是用并查集吧 一开始数组开太 ...
随机推荐
- 如何在Linux服务器和windows系统之间上传与下载文件
Do not let dream just be your dream. 背景:Linux服务器文件上传下载. XShell+Xftp安装包(解压即用)百度网盘链接:https://pan.baidu ...
- Lucene配置步骤详解
Lucene配置步骤说明: 1.搭建环境: 2.创建索引库: 3搜索索引库. Lucene配置步骤: 第一部分:搭建环境(创建环境导入jar包) 前提:已经创建好了数据库(直接导入book.sql文件 ...
- 升级SCCM 2012R2 SP1故障解决
故障一: 上周7月5号进行升级sccm至2012 R2 SP1的操作,执行升级程序splash.hta,在最后核心程序安装步骤失败,关闭升级程序,打开SCCM控制台报错如下: Configuratio ...
- ext组件中的查询
组件中的查询依赖于组件树,往上可追溯父组件,往下可查找子组件. 组件中的查询主要包括8个方法:up.down.query.child.nextNode.nextSibiling.previoutNod ...
- 数据库原理剖析 - 序列1 - B+树
本文节选自<软件架构设计:大型网站技术架构与业务架构融合之道>第6.3章节. 作者微信公众号: 架构之道与术.进入后,可以加入书友群,与作者和其他读者进行深入讨论.也可以在京东.天猫上购买 ...
- butterknife使用
butterknife是一个轻量级的DI框架,官网地址:http://jakewharton.github.io/butterknife/ 注意事项:使用前需要对编辑器进行相关设置,详见官网说明. 以 ...
- i春秋misc部分writeup
i春秋misc部分writeup 一.敲击 方方格格,然后看到下面的格式,猜测出是键盘上的布局,然后看这些字母形成的形状想那些字母,就是flag了 2.滴滴滴 放到ctfcack里解密,发现时栅栏密码 ...
- java 位运算符 以及加法 交换两个变量值
先给出十转二的除法 2 60 30 0 15 0 7 1 3 1 1 1 0 1 60转二 111100 再介绍位运算符 a=60 b=13 A = 0011 1100 B ...
- 父页面内获取获取iframe内的变量或者是获取iframe内的值
前提:页面不可跨域访问,必须同一域名下,否则返回值为空 父页面 <!DOCTYPE html> <html lang="en"> <head> ...
- WPF中查看PDF文件之MoonPdfLib类库
最近研究了两种PDF文件查看器,MoonPdfLib或者AdobeReader. 今天先说第一种,在网上扒到的很好的WPF中用MoonPdf类库来展示PDF文件. 在Sourceforge上下载到Mo ...