LCT维护最小生成树

要求两点路径最大的最小,首先想到的肯定是最小生成树,再加上有删边操作,那就得用LCT维护了。

可是对于cut一条边,我们要时刻维护图中的最小生成树,需要把之前被我们淘汰的边找回,那无法处理,所以我们考虑和并查集一样的技巧,倒着离线处理问题。

我们首先把询问中要删的边全部过滤掉,然后维护其他边产生的最小生成树,然后倒着回答每一个询问,把原来的删边变成连边,这样就方便多啦。

每一个cut需要比较边的大小,所以我们可以用map来记录每条边的编号。

  1. #include <bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3. #define full(a, b) memset(a, b, sizeof a)
  4. using namespace std;
  5. typedef long long ll;
  6. inline int lowbit(int x){ return x & (-x); }
  7. inline int read(){
  8. int X = 0, w = 0; char ch = 0;
  9. while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
  10. while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  11. return w ? -X : X;
  12. }
  13. inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
  14. inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
  15. template<typename T>
  16. inline T max(T x, T y, T z){ return max(max(x, y), z); }
  17. template<typename T>
  18. inline T min(T x, T y, T z){ return min(min(x, y), z); }
  19. template<typename A, typename B, typename C>
  20. inline A fpow(A x, B p, C lyd){
  21. A ans = 1;
  22. for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
  23. return ans;
  24. }
  25. const int N = 400005;
  26. int n, m, q, tot, fa[N], mx[N], val[N], ch[N][2], id[N], rev[N], st[N], vis[N];
  27. map<pair<int, int>, int> r;
  28. vector<int> ans;
  29. struct Edge {
  30. int u, v, w;
  31. bool operator < (const Edge &rhs) const {
  32. return w < rhs.w;
  33. }
  34. }e[N<<2];
  35. struct Query{ int opt, u, v; } query[N<<2];
  36. int init(int v){
  37. ++tot;
  38. id[tot] = tot, val[tot] = mx[tot] = v;
  39. fa[tot] = ch[tot][0] = ch[tot][1] = rev[tot] = 0;
  40. return tot;
  41. }
  42. bool isRoot(int x){
  43. return ch[fa[x]][0] != x && ch[fa[x]][1] != x;
  44. }
  45. void reverse(int x){
  46. rev[x] ^= 1, swap(ch[x][0], ch[x][1]);
  47. }
  48. void push_up(int x){
  49. int l = ch[x][0], r = ch[x][1];
  50. mx[x] = val[x], id[x] = x;
  51. if(mx[l] > mx[x]) mx[x] = mx[l], id[x] = id[l];
  52. if(mx[r] > mx[x]) mx[x] = mx[r], id[x] = id[r];
  53. }
  54. void push_down(int x){
  55. if(rev[x]){
  56. rev[x] ^= 1;
  57. if(ch[x][0]) reverse(ch[x][0]);
  58. if(ch[x][1]) reverse(ch[x][1]);
  59. }
  60. }
  61. void rotate(int x){
  62. int y = fa[x], z = fa[y], p = (ch[y][1] == x) ^ 1;
  63. ch[y][p^1] = ch[x][p], fa[ch[x][p]] = y;
  64. if(!isRoot(y)) ch[z][ch[z][1] == y] = x;
  65. fa[x] = z, fa[y] = x, ch[x][p] = y;
  66. push_up(y), push_up(x);
  67. }
  68. void splay(int x){
  69. int pos = 0; st[++pos] = x;
  70. for(int i = x; !isRoot(i); i = fa[i]) st[++pos] = fa[i];
  71. while(pos) push_down(st[pos--]);
  72. while(!isRoot(x)){
  73. int y = fa[x], z = fa[y];
  74. if(!isRoot(y)) (ch[y][0] == x) ^ (ch[z][0] == y) ? rotate(x) : rotate(y);
  75. rotate(x);
  76. }
  77. push_up(x);
  78. }
  79. void access(int x){
  80. for(int p = 0; x; p = x, x = fa[x])
  81. splay(x), ch[x][1] = p, push_up(x);
  82. }
  83. void makeRoot(int x){
  84. access(x), splay(x), reverse(x);
  85. }
  86. int findRoot(int x){
  87. access(x), splay(x);
  88. while(ch[x][0]) push_down(x), x = ch[x][0];
  89. splay(x);
  90. return x;
  91. }
  92. bool isConnect(int x, int y){
  93. makeRoot(x);
  94. return findRoot(y) == x;
  95. }
  96. void link(int x, int y){
  97. makeRoot(x), fa[x] = y;
  98. }
  99. void split(int x, int y){
  100. makeRoot(x), access(y), splay(y);
  101. }
  102. int main(){
  103. n = read(), m = read(), q = read();
  104. for(int i = 1; i <= n; i ++) init(0);
  105. for(int i = 0; i < m; i ++){
  106. e[i].u = read(), e[i].v = read(), e[i].w = read();
  107. if(e[i].u > e[i].v) swap(e[i].u, e[i].v);
  108. }
  109. sort(e, e + m);
  110. for(int i = 0; i < m; i ++) r[make_pair(e[i].u, e[i].v)] = i;
  111. for(int i = 0; i < q; i ++){
  112. query[i].opt = read(), query[i].u = read(), query[i].v = read();
  113. if(query[i].u > query[i].v) swap(query[i].u, query[i].v);
  114. if(query[i].opt == 2) vis[r[make_pair(query[i].u, query[i].v)]] = true;
  115. }
  116. int cnt = 0;
  117. for(int i = 0; i < m; i ++){
  118. if(cnt == n - 1) break;
  119. if(vis[i]) continue;
  120. int u = e[i].u, v = e[i].v, t = init(e[i].w);
  121. if(isConnect(u, v)) continue;
  122. link(u, t), link(t, v), cnt ++;
  123. }
  124. for(int i = q - 1; i >= 0; i --){
  125. int u = query[i].u, v = query[i].v, opt = query[i].opt;
  126. if(opt == 1){
  127. split(u, v);
  128. ans.push_back(mx[v]);
  129. }
  130. else{
  131. split(u, v);
  132. int d = r[make_pair(u, v)];
  133. if(e[d].w > mx[v]) continue;
  134. int tmp = id[v], t = init(e[d].w);
  135. splay(tmp);
  136. fa[ch[tmp][0]] = fa[ch[tmp][1]] = 0;
  137. ch[tmp][0] = ch[tmp][1] = 0;
  138. link(u, t), link(t, v);
  139. }
  140. }
  141. for(int i = ans.size() - 1; i >= 0; i --){
  142. printf("%d\n", ans[i]);
  143. }
  144. return 0;
  145. }

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. 如何在Linux服务器和windows系统之间上传与下载文件

    Do not let dream just be your dream. 背景:Linux服务器文件上传下载. XShell+Xftp安装包(解压即用)百度网盘链接:https://pan.baidu ...

  2. Lucene配置步骤详解

    Lucene配置步骤说明: 1.搭建环境: 2.创建索引库: 3搜索索引库. Lucene配置步骤: 第一部分:搭建环境(创建环境导入jar包) 前提:已经创建好了数据库(直接导入book.sql文件 ...

  3. 升级SCCM 2012R2 SP1故障解决

    故障一: 上周7月5号进行升级sccm至2012 R2 SP1的操作,执行升级程序splash.hta,在最后核心程序安装步骤失败,关闭升级程序,打开SCCM控制台报错如下: Configuratio ...

  4. ext组件中的查询

    组件中的查询依赖于组件树,往上可追溯父组件,往下可查找子组件. 组件中的查询主要包括8个方法:up.down.query.child.nextNode.nextSibiling.previoutNod ...

  5. 数据库原理剖析 - 序列1 - B+树

    本文节选自<软件架构设计:大型网站技术架构与业务架构融合之道>第6.3章节. 作者微信公众号: 架构之道与术.进入后,可以加入书友群,与作者和其他读者进行深入讨论.也可以在京东.天猫上购买 ...

  6. butterknife使用

    butterknife是一个轻量级的DI框架,官网地址:http://jakewharton.github.io/butterknife/ 注意事项:使用前需要对编辑器进行相关设置,详见官网说明. 以 ...

  7. i春秋misc部分writeup

    i春秋misc部分writeup 一.敲击 方方格格,然后看到下面的格式,猜测出是键盘上的布局,然后看这些字母形成的形状想那些字母,就是flag了 2.滴滴滴 放到ctfcack里解密,发现时栅栏密码 ...

  8. 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 ...

  9. 父页面内获取获取iframe内的变量或者是获取iframe内的值

    前提:页面不可跨域访问,必须同一域名下,否则返回值为空 父页面 <!DOCTYPE html> <html lang="en"> <head> ...

  10. WPF中查看PDF文件之MoonPdfLib类库

    最近研究了两种PDF文件查看器,MoonPdfLib或者AdobeReader. 今天先说第一种,在网上扒到的很好的WPF中用MoonPdf类库来展示PDF文件. 在Sourceforge上下载到Mo ...