lct入门题?

得换根了吧TAT

这大概不是很成熟的版本..

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<algorithm>
  5. #include<cstdio>
  6. using namespace std;
  7.  
  8. const int Maxn = ;
  9.  
  10. int ch[Maxn][],p[Maxn],flip[Maxn];
  11. int n,m;
  12.  
  13. const int D=;
  14. char in[D],out[*],*I=in,*O=out;
  15. #define gc (*I++)
  16. #define pc(x) ((*O++)=x)
  17. template <typename Q>
  18. void gt(Q&x) {
  19. static char c,f;
  20. for(c=gc,f=;!isdigit(c);c=gc)if(c=='-') f=;
  21. for(x=;isdigit(c);c=gc) x=x*+c-'';
  22. f && (x=-x);
  23. }
  24.  
  25. template <typename Q>
  26. void pt(Q x){
  27. static char stk[];
  28. static int top;
  29. top=;
  30. if(x==) pc('');
  31. for(;x;x/=) stk[++top] = x%+'';
  32. for(;top;top--) pc(stk[top]);
  33. }
  34.  
  35. void down(int x){
  36. if(flip[x]) {
  37. swap(ch[x][],ch[x][]);
  38. flip[ch[x][]]^=;
  39. flip[ch[x][]]^=;
  40. flip[x]=;
  41. }
  42. }
  43.  
  44. bool isroot(int x){
  45. return ch[p[x]][]!=x && ch[p[x]][]!=x;
  46. }
  47.  
  48. void rotate(int x) {
  49. int y=p[x],z=p[y];
  50. int l=ch[y][]==x,r=l^;
  51. if(!isroot(y)) ch[z][ch[z][]==y]=x;
  52. p[ch[x][r]]=y;
  53. p[y]=x;
  54. p[x]=z;
  55.  
  56. ch[y][l]=ch[x][r];
  57. ch[x][r]=y;
  58. }
  59.  
  60. int stk[Maxn],top;
  61. void splay(int x){
  62. stk[top=]=x;
  63. for(int t=x;!isroot(t);t=p[t]) stk[++top]=p[t];
  64. for(;top;top--) down(stk[top]);
  65.  
  66. for(;!isroot(x);){
  67. int y=p[x],z=p[y];
  68. if(!isroot(y)){
  69. if( (ch[y][]==x)^(ch[z][]==y) )
  70. rotate(x);else rotate(y);
  71. }
  72. rotate(x);
  73. }
  74. }
  75.  
  76. void access(int x){
  77. for(int t=;x;x=p[t=x]){
  78. splay(x);
  79. ch[x][]=t;
  80. }
  81. }
  82.  
  83. int getroot(int x){
  84. for(access(x),splay(x);ch[x][];x=ch[x][]);
  85. return x;
  86. }
  87.  
  88. void newroot(int x) {
  89. access(x);
  90. splay(x);
  91. flip[x]^=;
  92. }
  93.  
  94. void cut(int x){
  95. access(x);
  96. splay(x);
  97. p[ch[x][]]=;
  98. ch[x][]=;
  99. }
  100.  
  101. void combine(int x,int y) {
  102. newroot(x);
  103. p[x]=y;
  104. }
  105.  
  106. void work() {
  107. gt(n),gt(m);
  108. char c;
  109. int a,b;
  110. for(int i=;i<=m;i++) {
  111. for(;c=gc,c!='Q' && c!='C' &&c!='D';);
  112. gt(a),gt(b);
  113. if(c=='Q') printf("%s\n",getroot(a)==getroot(b)?"Yes" : "No");
  114. else if(c=='C') combine(a,b);
  115. else newroot(a),cut(b);
  116. }
  117. }
  118.  
  119. int main() {
  120. #ifdef DEBUG
  121. freopen("in.txt","r",stdin);
  122. freopen("out.txt","w",stdout);
  123. #endif
  124. fread(in,,D,stdin);
  125.  
  126. work();
  127.  
  128. return printf(out),;
  129. }

bzoj2049: [Sdoi2008]Cave 洞穴勘测的更多相关文章

  1. BZOJ2049 SDOI2008 Cave 洞穴勘测 【LCT】

    BZOJ2049 SDOI2008 Cave 洞穴勘测 Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分 ...

  2. 【LCT】BZOJ2049 [SDOI2008]Cave 洞穴勘测

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 10059  Solved: 4863[Submit ...

  3. [BZOJ2049][Sdoi2008]Cave 洞穴勘测 LCT模板

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 9705  Solved: 4674[Submit] ...

  4. [bzoj2049][Sdoi2008]Cave 洞穴勘测_LCT

    Cave 洞穴勘测 bzoj-2049 Sdoi-2008 题目大意:维护一个数据结构,支持森林中加边,删边,求两点连通性.n个点,m个操作. 注释:$1\le n\le 10^4$,$1\le m\ ...

  5. [BZOJ2049] [SDOI2008] Cave 洞穴勘测 (LCT)

    Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...

  6. BZOJ2049: [Sdoi2008]Cave 洞穴勘测 Link-Cut-Tree 模板题

    传送门 搞了这么长时间Splay终于可以搞LCT了,等等,什么是LCT? $LCT$就是$Link-Cut-Tree$,是维护动态树的一个很高效的数据结构,每次修改和查询的均摊复杂度为$O(logN) ...

  7. BZOJ2049——[Sdoi2008]Cave 洞穴勘测

    1.题目大意:就是一个动态维护森林联通性的题 2.分析:lct模板题 #include <stack> #include <cstdio> #include <cstdl ...

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

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

  9. bzoj2049: [Sdoi2008]Cave 洞穴勘测 lct裸题

    题意:三种操作一种摧毁一条边,一种链接一条边,一种查询两个点是否联通 题解:lct的link和cut即可 /********************************************** ...

随机推荐

  1. npm不能安装任何包,报错:npm WARN onload-script failed to require onload script npm-autoinit/autoinit及解决方法

    想要利用Hexo搭建一个博客,但是安装时npm一直报错,不仅仅是Hexo包,连别的其他包也不行,会提示下面的一堆错误 npm WARN onload-script failed to require ...

  2. 【BZOJ1050】【枚举+并查集】旅行comf

    Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求一条路径,使得路径上最大 ...

  3. CentOS 忘记root密码,解决方法

    1.开机后,在倒数5秒结束前,按下任意键 2.在显示centos...的那个界面下,按e键(edit) 3.会出现三行的界面,选择中间 kernel...那行,然后按e键 4.在接着出现的那个界面最后 ...

  4. linux c静态链接库与动态链接库

    库函数是我们编程的时候经常用到的,我们协作编程的时候可以将常用的函数封装成库供大家使用,这样能够提高大家的工作效率.对于库函数,它分为动态链接库和静态链接库.对于静态链接库我们必须是连接到可执行文件中 ...

  5. Linq/EF/lambda Group by/Order by 多个字段详细用法

    1)单个字段Group by: //a.Key类型与a.Province字段类型一样  .GroupBy(a => a.Province).Select(a => a.Key).ToLis ...

  6. Egret 双端接入爱贝支付遇到的问题

    首先要为 egret 工程引入第三方库: Egret 接第三方库:http://edn.egret.com/cn/index.php?g=&m=article&a=index& ...

  7. Github博客地址

    欢迎访问我的Github博客: J.R.Smith_blog

  8. Reducing the Dimensionality of data with neural networks / A fast learing algorithm for deep belief net

    Deeplearning原文作者Hinton代码注解 Matlab示例代码为两部分,分别对应不同的论文: . Reducing the Dimensionality of data with neur ...

  9. A Statistical View of Deep Learning (III): Memory and Kernels

    A Statistical View of Deep Learning (III): Memory and Kernels Memory, the ways in which we remember ...

  10. ROR入门之旅

    mac上为了不在登录画面看到其他账户,我禁用了root账户,而每次用Terminal的时候,先获得sudo账户的权限: sudo -s mac本身就安装有ruby ruby -v 查看当前安装的rub ...