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 水管局长数据加强版的更多相关文章

  1. [BZOJ]2594 水管局长数据加强版(Wc2006)

    失踪人口回归. LCT一直是小C的弱项,特别是这种维护链的信息的,写挂了就会调代码调到心态爆炸. 不过还好这一次的模板练习没有出现太多的意外. Description SC省MY市有着庞大的地下水管网 ...

  2. bzoj 2594: 水管局长数据加强版 Link-Cut-Tree

    题目: Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公 ...

  3. BZOJ 2594 水管局长数据加强版(动态树)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2594 题意:给出一个无向图,边有权值.定义一条路径的长度为该路径所有边的最大值.两种操作 ...

  4. bzoj 2594: [Wc2006]水管局长数据加强版 动态树

    2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec  Memory Limit: 128 MBSubmit: 934  Solved: 291[Submit][Sta ...

  5. BZOJ 2594: [Wc2006]水管局长数据加强版( LCT )

    离线然后就是维护加边的动态MST, Link cut tree秒掉..不过我写+调了好久...时间复杂度O(NlogN + MlogM) ------------------------------- ...

  6. BZOJ 2594: [Wc2006]水管局长数据加强版 [LCT kruskal]

    2594: [Wc2006]水管局长数据加强版 Time Limit: 25 Sec  Memory Limit: 128 MBSubmit: 2917  Solved: 918[Submit][St ...

  7. BZOJ 2594 【WC2006】 水管局长数据加强版

    题目链接:水管局长数据加强版 好久没写博客了…… 上次考试的时候写了一发LCT,但是写挂了……突然意识到我已经很久没有写过LCT了,于是今天找了道题来练练手. 首先,LCT这里不讲.这道题要求支持动态 ...

  8. BZOJ2594: [Wc2006]水管局长数据加强版

    题解: 裸LCT+离线+二分+MST... 代码:(几乎摘抄自hzwer) #include<cstdio> #include<cstdlib> #include<cma ...

  9. [bzoj2594][Wc2006]水管局长数据加强版 (lct)

    论蒟蒻的自我修养T_T.. 和noi2014魔法森林基本一样...然而数据范围大得sxbk...UPD:这题如果用lct判联通的话可能会被卡到O(mlogm)..所以最好还是用并查集吧 一开始数组开太 ...

随机推荐

  1. 分享几个常见的CMD命令,可能会用的上

    win7快捷命令.CMD命令secpol.msc(设置开机启动提示信息)services.msc(打开服务)dxdiag(检查DirectX信息)winver(检查Windows版本)regedit( ...

  2. 【网摘】C#.NET 在 MVC 中动态绑定下拉菜单的方法

    1. 已知下拉菜单列表项: 在 Controller 控制器类中输入已下代码  1 public class DemoController : Controller 2 { 3     public  ...

  3. C#中的CultureInfo类

    CultureInfo类位于System.Globalization命名空间内,这个类和命名空间许多人都不是很熟悉,实际我们在写程序写都经常间接性的接触这个类,当进行数字,日期时间,字符串匹配时,都会 ...

  4. hive基本操作与应用

    通过hadoop上的hive完成WordCount 启动hadoop Hdfs上创建文件夹 上传文件至hdfs 启动Hive 创建原始文档表 导入文件内容到表docs并查看 用HQL进行词频统计,结果 ...

  5. 自己实现的TypeOf函数2

    自己实现的typeOf函数:返回传入参数的类型 主要用于解决,js自带的typeof返回结果不精确:Ext JS中typeOf对字符串对象.元素节点.文本节点.空白文本节点判断并不准确的问题 与上一篇 ...

  6. 微信小程序 canvas 文字自动换行

    Page({ drawCanvas: function(ctx) {// 地址 ctx.setFontSize() ctx.setFillStyle('#9E7240') ctx.textAlign= ...

  7. FFmpeg部署及相关指令操作说明

    1.首先在http://ffmpeg.zeranoe.com/builds/上下载static版本, 下载好以后解压缩到 c:/ffmpeg/ 2.配置环境变量 path -> c:/ffmpe ...

  8. RuntimeException和Exception区别

    1.java将所有的错误封装为一个对象,其根本父类为Throwable, Throwable有两个子类:Error和Exception. 2.Error是Throwable 的子类,用于指示合理的应用 ...

  9. centos7 最小安装初始化

    配置阿里yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup \&&cu ...

  10. [RHEL 7]ISCSI服务端及客户端连接配置

    环境RHEL7.4 1.搭建服务器端主机环境 网络配置 网卡eth0 10.0.0.1 网卡eth1 10.1.0.1 网卡eth2 10.2.0.1 网卡eth3 10.3.0.1 硬盘配置 添加一 ...