题意:

  自己看...加边删边问联通...

SOL:

  就加了一个findroot而已...

  然而时间还是惨不忍睹...优化全开也才1700ms...膜seter...

Code:

  

  1. /*==========================================================================
  2. # Last modified: 2016-03-17 18:33
  3. # Filename: 2049.cpp
  4. # Description:
  5. ==========================================================================*/
  6. #define me AcrossTheSky
  7. #include <cstdio>
  8. #include <cmath>
  9. #include <ctime>
  10. #include <string>
  11. #include <cstring>
  12. #include <cstdlib>
  13. #include <iostream>
  14. #include <algorithm>
  15.  
  16. #include <set>
  17. #include <map>
  18. #include <stack>
  19. #include <queue>
  20. #include <vector>
  21.  
  22. #define lowbit(x) (x)&(-x)
  23. #define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++)
  24. #define FORP(i,a,b) for(int i=(a);i<=(b);i++)
  25. #define FORM(i,a,b) for(int i=(a);i>=(b);i--)
  26. #define ls(a,b) (((a)+(b)) << 1)
  27. #define rs(a,b) (((a)+(b)) >> 1)
  28. #define getlc(a) ch[(a)][0]
  29. #define getrc(a) ch[(a)][1]
  30.  
  31. #define maxn 100000
  32. #define maxm 100000
  33. #define pi 3.1415926535898
  34. #define _e 2.718281828459
  35. #define INF 1070000000
  36. using namespace std;
  37. typedef long long ll;
  38. typedef unsigned long long ull;
  39.  
  40. template<class T> inline
  41. void read(T& num) {
  42. bool start=false,neg=false;
  43. char c;
  44. num=0;
  45. while((c=getchar())!=EOF) {
  46. if(c=='-') start=neg=true;
  47. else if(c>='0' && c<='9') {
  48. start=true;
  49. num=num*10+c-'0';
  50. } else if(start) break;
  51. }
  52. if(neg) num=-num;
  53. }
  54. /*==================split line==================*/
  55. int ch[maxn][2],rev[maxn],nxt[maxn],fa[maxn],pre[maxn],s[maxn];
  56. int sta[maxn];
  57. int n,m;
  58. inline void pushup(int x){
  59. s[x]=1+s[ch[x][0]]+s[ch[x][1]];
  60. }
  61. inline void pushdown(int x){
  62. if (rev[x]){
  63. swap(ch[x][0],ch[x][1]);
  64. rev[ch[x][0]]^=1;
  65. rev[ch[x][1]]^=1;
  66. rev[x]=0;
  67. }
  68. }
  69. inline void setx(int x){
  70. if (ch[x][1]) {
  71. fa[ch[x][1]]=0; pre[ch[x][1]]=x; ch[x][1]=0; pushup(x);
  72. }
  73. }
  74. inline void rotate(int x){
  75. int p=fa[x],q=fa[p],d=ch[p][1]==x;
  76. fa[ch[p][d]=ch[x][d^1]]=p; pushup(p);
  77. fa[ch[x][d^1]=p]=x; pushup(x);
  78. fa[x]=q;
  79. if (q){
  80. if (ch[q][1]==p) ch[q][1]=x;
  81. else ch[q][0]=x;
  82. }
  83. else pre[x]=pre[p];
  84. }
  85. inline void splay(int x){
  86. int top=0;
  87. for (int i=x;i;i=fa[i]) sta[++top]=i;
  88. for (;top;top--) pushdown(sta[top]);
  89. for (int y;(y=fa[x])!=0;rotate(x))
  90. if (fa[y]) rotate((getlc(y)==x)==(getlc(fa[y])==y)?y:x);
  91. }
  92. inline void access(int x){
  93. splay(x); setx(x); int v;
  94. while(v=pre[x]){
  95. splay(v); setx(v);
  96. ch[v][1]=x; fa[x]=v; pushup(v);
  97. x=v;
  98. }
  99. }
  100. inline void beroot(int x){
  101. access(x);
  102. splay(x);
  103. rev[x]^=1;
  104. }
  105. inline void link(int x,int y){
  106. beroot(x);
  107. pre[x]=y;
  108. //access(x); splay(x);
  109. }
  110. inline void cut(int x,int y){
  111. beroot(x); access(y); splay(y);
  112. ch[y][0]=fa[x]=pre[x]=0;
  113. pushup(y);
  114. }
  115. inline int findroot(int x){
  116. while (ch[x][0]) x=ch[x][0];
  117. return x;
  118. }
  119. inline void query(int x,int y){
  120. beroot(y);
  121. access(x);
  122. splay(x);
  123. if (findroot(x)==y) printf("Yes\n");
  124. else printf("No\n");
  125. }
  126. int main(){
  127. read(n); read(m);
  128. FORP(i,1,n) s[i]=1;
  129. FORP(i,1,m){
  130. char temp[10]; int x,y;
  131. scanf("%s",temp); read(x); read(y);
  132. if (x<y) swap(x,y);
  133. if (temp[0]=='Q') query(x,y);
  134. else if (temp[0]=='C') link(x,y);
  135. else cut(x,y);
  136. }
  137. }

BZOJ 2049 & LCT又一模板的更多相关文章

  1. BZOJ 2049 LCT

    思路:LCT的基本操作 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm&g ...

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

    题目链接:BZOJ - 2049 题目分析 LCT的基本模型,包括 Link ,Cut 操作和判断两个点是否在同一棵树内. Link(x, y) : Make_Root(x); Splay(x); F ...

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

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

  4. bzoj 2049: [Sdoi]Cave 洞穴探测 (LCT)

    第一次写lct (这是一道lct裸题 这次没有可爱(划掉)的同学教我,虽然有模板,但是配合网上的讲解还是看不懂QAQ 然后做了几道题之后总算有些感觉辣 于是决定给自己挖个坑,近期写一个lct详解(不过 ...

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

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

  6. BZOJ 2049: [Sdoi2008]Cave 洞穴勘測 LCT

    入门级LCT: 仅仅有 Cut Link 2049: [Sdoi2008]Cave 洞穴勘測 Time Limit: 10 Sec  Memory Limit: 259 MB Submit: 3073 ...

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

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

  8. BZOJ 2049 SDOI2008 洞穴勘测 LCT板子

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 题意概述:给出N个点,一开始不连通,M次操作,删边加边,保证图是一个森林,询问两点连 ...

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

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

随机推荐

  1. Python中format的用法

    自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱.语法 它通过{}和: ...

  2. hdu 1754:I Hate It(线段树,入门题,RMQ问题)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. Could not link against boost_system 解决办法

    Could not link against boost_system 解决办法: 先安装 libboost-all-dev ./configure --with-incompatible-bdb - ...

  4. 运维自动化之ansible的安装与使用(包括模块与playbook使用)(转发)

    原文  http://dl528888.blog.51cto.com/2382721/1435415 我使用过puppet(地址是http://dl528888.blog.51cto.com/2382 ...

  5. Java 初学记录之一 快速输入

    1. sysout 按回车 System.out.println();

  6. 【leetcode】Reverse Words in a String

    今天第一次在leetcode上提交了一个题目,据说这个网站基本上都是名企面试笔试题,今天无意一进去就看到第一题居然就是昨天的腾讯实习生笔试题,赶紧注册了个账号做题. 题目描述: Given an in ...

  7. 在ASP.NET 5中使用SignalR

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:SignalR作为ASP.NET中进行Web实时双向通信的组件,在ASP.NET 5中 ...

  8. 在Virtulbox上装Ubuntu

    做个程序员,会用Linux,这应该是最基本的要求吧.可惜本人经常用Windows,只是偶尔去服务器上做些操作的时候才接触到linux.so,我要学Linux.刚学所以还是先装个虚拟机吧,等在虚拟机上用 ...

  9. hpunix下11gRac的安装

    一.检查环境 1.操作系统版本# uname -a 2.补丁包三大补丁包#swlist -l bundle|grep QPKAPPS#swlist -l bundle|grep QPKBASE#swl ...

  10. POJ 1163:The Triangle

    Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a progr ...