题目链接:BZOJ - 2049

题目分析

LCT的基本模型,包括 Link ,Cut 操作和判断两个点是否在同一棵树内。

Link(x, y) : Make_Root(x); Splay(x); Father[x] = y;

Cut(x, y) : Make_Root(x); Access(y); 断掉 y 和 Son[y][0]; 注意修改 Son[y][0] 的 isRoot 和 Father

判断 x, y 是否在同一棵数内,我们就看两个点所在树的根是否相同,使用 Find_Root();

Find_Root(x) : Access(x); Splay(x); while (Son[x][0] != 0) x = Son[x][0]; 然后 x 就是树根了。

代码

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. inline void Read(int &Num)
  11. {
  12. char c = getchar();
  13. bool Neg = false;
  14. while (c < '0' || c > '9')
  15. {
  16. if (c == '-') Neg = true;
  17. c = getchar();
  18. }
  19. Num = c - '0'; c = getchar();
  20. while (c >= '0' && c <= '9')
  21. {
  22. Num = Num * 10 + c - '0';
  23. c = getchar();
  24. }
  25. if (Neg) Num = -Num;
  26. }
  27.  
  28. const int MaxN = 10000 + 5;
  29.  
  30. int n, m;
  31. int Father[MaxN], Son[MaxN][2];
  32.  
  33. bool isRoot[MaxN], Rev[MaxN];
  34.  
  35. inline void Reverse(int x)
  36. {
  37. Rev[x] = !Rev[x];
  38. swap(Son[x][0], Son[x][1]);
  39. }
  40.  
  41. inline void PushDown(int x)
  42. {
  43. if (!Rev[x]) return;
  44. Rev[x] = false;
  45. if (Son[x][0]) Reverse(Son[x][0]);
  46. if (Son[x][1]) Reverse(Son[x][1]);
  47. }
  48.  
  49. void Rotate(int x)
  50. {
  51. int y = Father[x], f;
  52. PushDown(y); PushDown(x);
  53. if (x == Son[y][0]) f = 1;
  54. else f = 0;
  55. if (isRoot[y])
  56. {
  57. isRoot[y] = false;
  58. isRoot[x] = true;
  59. }
  60. else
  61. {
  62. if (y == Son[Father[y]][0]) Son[Father[y]][0] = x;
  63. else Son[Father[y]][1] = x;
  64. }
  65. Father[x] = Father[y];
  66. Son[y][f ^ 1] = Son[x][f];
  67. if (Son[x][f]) Father[Son[x][f]] = y;
  68. Son[x][f] = y;
  69. Father[y] = x;
  70. }
  71.  
  72. void Splay(int x)
  73. {
  74. int y;
  75. while (!isRoot[x])
  76. {
  77. y = Father[x];
  78. if (isRoot[y])
  79. {
  80. Rotate(x);
  81. break;
  82. }
  83. if (y == Son[Father[y]][0])
  84. {
  85. if (x == Son[y][0])
  86. {
  87. Rotate(y);
  88. Rotate(x);
  89. }
  90. else
  91. {
  92. Rotate(x);
  93. Rotate(x);
  94. }
  95. }
  96. else
  97. {
  98. if (x == Son[y][1])
  99. {
  100. Rotate(y);
  101. Rotate(x);
  102. }
  103. else
  104. {
  105. Rotate(x);
  106. Rotate(x);
  107. }
  108. }
  109. }
  110. }
  111.  
  112. int Access(int x)
  113. {
  114. int y = 0;
  115. while (x != 0)
  116. {
  117. Splay(x);
  118. PushDown(x);
  119. isRoot[Son[x][1]] = true;
  120. Son[x][1] = y;
  121. if (y) isRoot[y] = false;
  122. y = x;
  123. x = Father[x];
  124. }
  125. return y;
  126. }
  127.  
  128. void Make_Root(int x)
  129. {
  130. int t = Access(x);
  131. Reverse(t);
  132. }
  133.  
  134. int Find_Root(int x)
  135. {
  136. int t = Access(x);
  137. while (Son[t][0] != 0) t = Son[t][0];
  138. return t;
  139. }
  140.  
  141. int main()
  142. {
  143. scanf("%d%d", &n, &m);
  144. for (int i = 1; i <= n; ++i)
  145. {
  146. isRoot[i] = true;
  147. Father[i] = 0;
  148. }
  149. char Str[10];
  150. int a, b, x, y;
  151. for (int i = 1; i <= m; ++i)
  152. {
  153. scanf("%s", Str);
  154. Read(a); Read(b);
  155. if (strcmp(Str, "Connect") == 0)
  156. {
  157. Make_Root(a);
  158. Splay(a);
  159. Father[a] = b;
  160. }
  161. else if (strcmp(Str, "Destroy") == 0)
  162. {
  163. Make_Root(a);
  164. Access(b);
  165. Splay(b);
  166. PushDown(b);
  167. isRoot[Son[b][0]] = true;
  168. Father[Son[b][0]] = 0;
  169. Son[b][0] = 0;
  170. }
  171. else
  172. {
  173. x = Find_Root(a);
  174. y = Find_Root(b);
  175. if (x == y) printf("Yes\n");
  176. else printf("No\n");
  177. }
  178. }
  179. return 0;
  180. }

  

[BZOJ 2049] [Sdoi2008] Cave 洞穴勘测 【LCT】的更多相关文章

  1. bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2049 题面: 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 ...

  2. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 LCT

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...

  3. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测——LCT

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 省选之前来切一道数据结构模板题. 题意 这是一道模板题. N个点,M次操作,每次加边/ ...

  4. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (LCT维护连通性)

    直接把x设为根,然后查询y所在联通块的根是不是x就行了. CODE #include <cstdio> #include <cstring> #include <algo ...

  5. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (动态树入门)

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

  6. bzoj 2049: [Sdoi2008]Cave 洞穴勘测 动态树

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

  7. 【刷题】BZOJ 2049 [Sdoi2008]Cave 洞穴勘测

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

  8. bzoj 2049 [Sdoi2008]Cave 洞穴勘测(LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2049 [题意] 给定森林,可能有连边或断边的操作,回答若干个连通性的询问. [思路] ...

  9. 【BZOJ】2049: [Sdoi2008]Cave 洞穴勘测 LCT

    [题意]给定n个点和m个操作,每次操作:1.连接2个点.2.断开2个点.3.查询2个点是否连通.m<=2*10^5. [算法]Link-Cut Tree [题解]LCT模板题,Link,Cut, ...

随机推荐

  1. [互联网面试笔试汇总C/C++-7] 寻找两个链表的第一个交点-微策略

    题目:有两个链表,找出他们的第一个交点,要求每个链表只能遍历一次,可以对链表进行任何操作,空间O(1). 思路: 这个题目刚开始看觉得要遍历一次有点困难,但是仔细一想,人家告诉说可以对链表进行任何操作 ...

  2. 5 Ways to Use Log Data to Analyze System Performance--reference

    Recently we looked across some of the most common behaviors that our community of 25,000 users looke ...

  3. adb取出安装在手机中的apk

    Android实战技巧之十八:adb取出安装在手机中的apk 场景: 朋友看见你Android手机中的游戏或应用很好玩,也想装一个此程序,但限于网络条件不能从网上下载.那么最简单的办法就是直接从你手机 ...

  4. CXF整合Spring开发WebService

    刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开 ...

  5. Linux下搭建Oracle11g RAC(6)----安装Grid Infrastructure

    从此步骤开始,我们正式安装Grid软件: ① 以grid用户登录图形界面,执行/home/grid/grid/runInstaller,进入OUI的图形安装界面: ② 进入OUI安装界面后,选择第3项 ...

  6. 【原】个人对win7开机黑屏只有鼠标排障总结

    个人对win7开机黑屏只有鼠标排障总结 文:铁乐猫 第一种情况是explorer.exe进程丢失或损坏有关: 判断方法是按Ctrl+Alt+Del键能呼出任务管理器,结束explorer.exe进程, ...

  7. C#/.net七牛云存储上传图片(文件)操作

    七牛云存储官方: C#SDK(http://developer.qiniu.com/docs/v6/sdk/csharp-sdk.html) 注册成为标准用户就可获得:10GB永久免费存储空间/ 每月 ...

  8. 【锋利的jQuery】学习笔记01

    第一章 认识jQuery 一.常见的JS库 Prototype 最早的js库之一.把好用JS方法组合,缺点结构松散. Dojo 离线存储API.生成图标组件.矢量图形库.Comet支持.企业级JS库, ...

  9. c语言学习之基础知识点介绍(一):输出语句和变量简单介绍

    本系列是为了学习ios做准备的,也能作为c语言入门的教程看看. c语言的程序结构: 1.顺序结构:自上而下依次执行. 2.分支结构:程序有选择的执行某段代码或者不执行某段代码. 3.循环结构:程序循环 ...

  10. 这次是C#中的接口

    接口的出现,是为了解决C#中不允许多重继承的问题. 1.什么是接口? 我觉得可以把接口理解为对一组方法声明进行的统一命名,但这些方法没有提供任何实现. 通过接口,就可以对方法进行统一管理,避免了在每种 ...