update:2017.09.26

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Link_Cut_Tree
  6. {
  7. static const int MAXN = + ;
  8.  
  9. int ch[MAXN][], fa[MAXN], rev[MAXN], sz[MAXN];
  10. int sk[MAXN];
  11.  
  12. bool isroot(int x)
  13. {
  14. return ch[fa[x]][] != x && ch[fa[x]][] != x;
  15. }
  16.  
  17. void reverse(int x)
  18. {
  19. rev[x] ^= , swap(ch[x][],ch[x][]);
  20. }
  21.  
  22. void update(int x)
  23. {
  24. sz[x] = sz[ch[x][]] + sz[ch[x][]] +;
  25. }
  26.  
  27. void push_down(int x)
  28. {
  29. if(!rev[x]) return ;
  30. if(ch[x][]) reverse(ch[x][]);
  31. if(ch[x][]) reverse(ch[x][]);
  32. rev[x]=;
  33. }
  34.  
  35. void rotate(int x)
  36. {
  37. int f = fa[x], gf = fa[f];
  38. int t1 = ( x != ch[f][]), t2 = ( f != ch[gf][]), tmp = ch[x][^t1];
  39. if(!isroot(f)) ch[gf][^t2] = x;
  40. fa[tmp] = f, fa[x] = gf, ch[x][^t1] = f, fa[f] = x, ch[f][^t1] = tmp;
  41. update(f);
  42. }
  43.  
  44. void splay(int x)
  45. {
  46. int top = ;
  47. sk[++top] = x;
  48. for(int i = x; !isroot(i); i = fa[i]) sk[++top] = fa[i];
  49. while(top) push_down(sk[top--]);
  50. for(int f = fa[x], gf = fa[f]; !isroot(x); rotate(x), f = fa[x],gf = fa[f])
  51. if(!isroot(f))
  52. rotate((x==ch[f][]) ^ (f==ch[gf][]) ? x : f);
  53. update(x);
  54. }
  55.  
  56. void access(int x)
  57. {
  58. for(int p = ; x; p = x, x = fa[x])
  59. splay(x), ch[x][] = p, update(x);
  60. }
  61.  
  62. void makeroot(int x)
  63. {
  64. access(x), splay(x), reverse(x);
  65. }
  66.  
  67. int findroot(int x)
  68. {
  69. access(x), splay(x);
  70. while(ch[x][]) x = ch[x][];
  71. return x;
  72. }
  73. void link(int x,int y)
  74. {
  75. makeroot(x), fa[x] = y;
  76. }
  77.  
  78. void cut(int x,int y)
  79. {
  80. makeroot(x), access(y), splay(y);
  81. if(ch[y][] == x) ch[y][] = fa[x] = ;
  82. update(y);
  83. }
  84.  
  85. void debug(void)
  86. {
  87. for(int i=;i<=;i++)
  88. printf("%d %d %d %d %d %d %d\n",i,fa[i],ch[i][],ch[i][],rev[i],sz[i]);
  89. }
  90. }lct;
  91.  
  92. int main(void)
  93. {
  94.  
  95. return ;
  96. }

link cut tree模板(LCT模板)的更多相关文章

  1. Luogu P3690【模板】Link Cut Tree (LCT板题)

    省选前刷道LCT板题(话说之前没做这道题-) CODE #include<bits/stdc++.h> using namespace std; inline void read(int ...

  2. LUOGU P3690 【模板】Link Cut Tree (lct)

    传送门 解题思路 \(lct\)就是基于实链剖分,用\(splay\)来维护每一条实链,\(lct\)的维护对象是一棵森林.\(lct\)支持很多神奇的操作: \(1.\) \(access\):这是 ...

  3. 洛谷P3690 【模板】Link Cut Tree (LCT)

    题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...

  4. LuoguP3690 【模板】Link Cut Tree (LCT)

    勉强算是结了个大坑吧或者才开始 #include <cstdio> #include <iostream> #include <cstring> #include ...

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

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

  6. 洛谷P3690 [模板] Link Cut Tree [LCT]

    题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...

  7. LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

    P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...

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

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

  9. 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)

    题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...

  10. LG3690 【模板】Link Cut Tree (动态树)

    题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...

随机推荐

  1. ChemDraw中化学信息怎么通过Excel搜索

    用户可以通过ChemDraw for Excel插件功能在Office Excel中建立ChemOffice菜单将ChemOffice和Excel结合使用,使用电子表格的最大优势之一就是可以清晰查看并 ...

  2. JQuery------实现鼠标摁下抬起时div背景色改变

    作用:使用自定义一个按钮 代码: <div class = 'btn'>按钮</div> $(".btn").mousedown(function () { ...

  3. Perfmon - Windows 自带系统监测工具

    本文转载自oscar999 一. 简述 可以用于监视CPU使用率.内存使用率.硬盘读写速度.网络速度等. Perfmon提供了图表化的系统性能实时监视器.性能日志和警报管理,系统的性能日志可定义为二进 ...

  4. js 中导出excel 较长数字串会变成科学计数法(转载)

    在做项目中,碰到如题的问题.比如要将居民的信息导出到excel中,居民的身份证号码因为长度过长(大于10位),excel会自动的将过长的数字串转换成 科学计数法.现在网上找到解决方案之一: (在数字串 ...

  5. SpringMvc三大组件详解

    SpringMvc框架结构图 处理器映射器:用户请求路径到Controller方法的映射 处理器适配器:根据handler(controlelr类)的开发方式(注解开发/其他开发) 方式的不同区寻找不 ...

  6. onbeforeunload 适用DOM 0级,不适用 DOM 2级

    你可以在控制台试下: window.addEventListener("beforeunload", function () { return 'ss'; }); 我这里 chro ...

  7. 160607、springmvc+spring使用taskExecutor

    第一步:导入spring core的jar+springmvc的jar 第二步:springmvc的配置文件中 <bean id="taskExecutor" class=& ...

  8. 安装mysql最后一步未响应,卡死。(解决方法mySql5.5,以及安装教程)

    安装教程:http://www.server110.com/mysql/201308/784.html 重装mysql的时候,总是在提交配置后的最后一步,安装失败,进程管理器里显示程序无响应,mysq ...

  9. 百度jQuery库

    <script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.js"></script>

  10. logback.xml解读----日志配置解读

    初次接触javaweb项目的日志是log4j文件,但是后来发现通过配置logback.xml文件实现日志输出非常好用.经过上午的学习,现总结如下: 直接上配置文件和注释: <?xml versi ...