【BZOJ1969】航线规划(Link-Cut Tree)

题面

BZOJ

题解

删边操作

套路呀

离线读入倒过来做

变成加边操作

现在考虑怎么确定两点直接的关键路径条数

如果是一棵树,那么每条边都是关键路径

在一棵树的基础上

如果连接了两点

那么,两点之间原来的路径一定都不是关键路径

所以,弄一个\(LCT\)

每次维护加边

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<set>
  8. #include<map>
  9. #include<vector>
  10. #include<queue>
  11. using namespace std;
  12. #define MAX 52000
  13. #define lson (t[x].ch[0])
  14. #define rson (t[x].ch[1])
  15. inline int read()
  16. {
  17. int x=0,t=1;char ch=getchar();
  18. while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
  19. if(ch=='-')t=-1,ch=getchar();
  20. while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
  21. return x*t;
  22. }
  23. map<pair<int,int>,int> M;
  24. struct qq{int opt,ans;int u,v;}q[MAX];
  25. struct Line{int v,next;}e[MAX*4];
  26. struct edge{int u,v,br;}E[MAX*4];
  27. int h[MAX],cnt=1,tot,n,m;
  28. int S[MAX<<2],top;
  29. inline void Add(int u,int v){e[cnt]=(Line){v,h[u]};h[u]=cnt++;}
  30. struct Node
  31. {
  32. int ch[2],ff;
  33. int sum,rev,v;
  34. int ly;
  35. }t[MAX<<2];
  36. bool isroot(int x){return t[t[x].ff].ch[0]!=x&&t[t[x].ff].ch[1]!=x;}
  37. void pushup(int x){t[x].sum=t[lson].sum+t[rson].sum+t[x].v;}
  38. void rotate(int x)
  39. {
  40. int y=t[x].ff,z=t[y].ff;
  41. int k=t[y].ch[1]==x;
  42. if(!isroot(y))t[z].ch[t[z].ch[1]==y]=x;t[x].ff=z;
  43. t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;
  44. t[x].ch[k^1]=y;t[y].ff=x;
  45. pushup(y);pushup(x);
  46. }
  47. void putrev(int x){swap(lson,rson);t[x].rev^=1;}
  48. void putly(int x){t[x].ly=1;t[x].sum=t[x].v=0;}
  49. void pushdown(int x)
  50. {
  51. if(t[x].rev)
  52. {
  53. if(lson)putrev(lson);
  54. if(rson)putrev(rson);
  55. t[x].rev^=1;
  56. }
  57. if(t[x].ly)
  58. {
  59. if(lson)putly(lson);
  60. if(rson)putly(rson);
  61. t[x].ly=0;
  62. }
  63. }
  64. void Splay(int x)
  65. {
  66. S[top=1]=x;
  67. for(int i=x;!isroot(i);i=t[i].ff)S[++top]=t[i].ff;
  68. while(top)pushdown(S[top--]);
  69. while(!isroot(x))
  70. {
  71. int y=t[x].ff,z=t[y].ff;
  72. if(!isroot(y))
  73. (t[y].ch[1]==x)^(t[z].ch[1]==y)?rotate(x):rotate(y);
  74. rotate(x);
  75. }
  76. }
  77. void access(int x){for(int y=0;x;y=x,x=t[x].ff)Splay(x),t[x].ch[1]=y,pushup(x);}
  78. void makeroot(int x){access(x);Splay(x);putrev(x);}
  79. void split(int x,int y){makeroot(x);access(y);Splay(y);}
  80. void cut(int x,int y){split(x,y);t[y].ch[0]=t[x].ff=0;pushup(y);}
  81. void link(int x,int y){makeroot(x);t[x].ff=y;}
  82. int findroot(int x){access(x);Splay(x);while(lson)x=lson;return x;}
  83. int main()
  84. {
  85. n=read();m=read();
  86. for(int i=1;i<=m;++i)
  87. {
  88. int u=read(),v=read();
  89. if(u>v)swap(u,v);
  90. E[i]=(edge){u,v};
  91. M[make_pair(u,v)]=i;
  92. t[i+n].v=1;
  93. }
  94. while(233)
  95. {
  96. int c=read();if(c==-1)break;
  97. int u=read(),v=read();if(u>v)swap(u,v);
  98. q[++tot]=(qq){c,0,u,v};
  99. if(!c)E[q[tot].ans=M[make_pair(u,v)]].br=1;
  100. }
  101. for(int i=1;i<=m;++i)
  102. if(!E[i].br)
  103. {
  104. if(findroot(E[i].u)!=findroot(E[i].v))
  105. link(E[i].u,i+n),link(E[i].v,i+n);
  106. else split(E[i].u,E[i].v),putly(E[i].v);
  107. }
  108. for(int i=tot;i;--i)
  109. {
  110. if(q[i].opt)
  111. {
  112. split(q[i].u,q[i].v);
  113. q[i].ans=t[q[i].v].sum;
  114. }
  115. else
  116. {
  117. if(findroot(q[i].u)!=findroot(q[i].v))
  118. link(q[i].u,q[i].ans+n),link(q[i].v,q[i].ans+n);
  119. else split(q[i].u,q[i].v),putly(q[i].v);
  120. }
  121. }
  122. for(int i=1;i<=tot;++i)
  123. if(q[i].opt)printf("%d\n",q[i].ans);
  124. return 0;
  125. }

【BZOJ1969】航线规划(Link-Cut Tree)的更多相关文章

  1. Link Cut Tree 总结

    Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...

  2. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  3. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  4. Link/cut Tree

    Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...

  5. 洛谷P3690 Link Cut Tree (模板)

    Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...

  6. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  7. bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门

    link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...

  8. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  9. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

随机推荐

  1. MAC下secretCRT使用技巧(转)

    1.打开secureCRT,按alt+b,可以调出快速启动栏,我相信secureCRT的忠实用户,都会保存一堆的sessions.2.按ctrl,可以同时选中多个session,再点击连接,可快速连接 ...

  2. ★Linux命令行操作技巧(作为服务器端)

    1.统计某个目录下总共有多少个文件(递归统计所有子目录)ls -lR|grep "^-"|wc -l

  3. 关于 JS 拖拽功能的冲突问题及解决方法

    前言 我在之前写过关于 JS 拖拽的文章,实现方式和网上能搜到的方法大致相同,别无二致,但是在一次偶然的测试中发现,这种绑定事件的方式可能会和其它的拖拽事件产生冲突,由此产生了对于事件绑定的思考.本文 ...

  4. 个人觉得实用的Python姿势

    以后会陆续补充 偶然在Python Cookbook看到一个format操作,想到一个问题, 感觉用了!r之后,会把传入的对象按照原来形式保留 d = {"foo": " ...

  5. 统计表中 重复出现 XX次以上的数据

    在平时使用数据库查询数据时 经常会遇到查询表中出现XX次以上的数据  以前自己遇到就直接百度  然后拿来就用  在过段时间遇到就懵逼了 还得百度.... so  还是加深理解下  省的以后遇到再次一脸 ...

  6. JVM笔记4-对象的创建

    1.对象的创建过程: 1.new 类名 2.根据new的参数在常量池中定位一个类的符号的引用. 3.如果没找到这个符号的引用,说明类还没有被加载.则进行类的加载,解析和初始化 4.虚拟机为对象分配内存 ...

  7. Redis持久化存储

    Redis是一个支持持久化的内存数据库,也就是说redis需要经常将内存中的数据同步到磁盘来保证持久化.redis支持四种持久化方式,一是 Snapshotting(快照)也是默认方式:二是Appen ...

  8. Spring初始化ApplicationContext为null

    1. ApplicationContextAware初始化 通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationConte ...

  9. pc浏览器css和js计算浏览器宽度的差异以及和滚动条的关系

    如图: css宽度:1250 不包括滚动条宽度 用控制台箭头选取元素显示的左边的宽度:1250  不包含滚动条宽度 缩放浏览器右上角显示的宽度:1267 包含了滚动条宽度 再看下控制台: 由此可计算浏 ...

  10. 【前端】Vue和Vux开发WebApp日志四、增加命令行参数

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/vue_vux_4.html 项目github地址:https://github.com/shamoyuu/vue- ...