【BZOJ2959】长跑(Link-Cut Tree,并查集)

题面

BZOJ

题解

如果保证不出现环的话

妥妥的\(LCT\)傻逼题

现在可能会出现环

环有什么影响?

那就可以沿着环把所有点全部走一遍吧

所以,相当于把环看成一个点来搞一搞

所以,维护一个并查集

记录一下每个点被缩成了哪个点

然后再用\(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 320000
  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. struct Node
  24. {
  25. int ch[2],ff;
  26. int sum,rev,v;
  27. }t[MAX<<1];
  28. int S[MAX<<1],top;
  29. int tot,n,m,a[MAX<<1];
  30. int f[MAX<<1];
  31. int ff[MAX<<1];
  32. int getf(int x){return x==f[x]?x:f[x]=getf(f[x]);}
  33. int Getf(int x){return x==ff[x]?x:ff[x]=Getf(ff[x]);}
  34. bool isroot(int x){return t[getf(t[x].ff)].ch[0]!=x&&t[getf(t[x].ff)].ch[1]!=x;}
  35. void pushup(int x){t[x].sum=t[lson].sum+t[rson].sum+t[x].v;}
  36. void pushdown(int x)
  37. {
  38. if(!t[x].rev)return;
  39. swap(lson,rson);
  40. t[lson].rev^=1;t[rson].rev^=1;
  41. t[x].rev^=1;
  42. }
  43. void rotate(int x)
  44. {
  45. int y=getf(t[x].ff),z=getf(t[y].ff);
  46. int k=t[y].ch[1]==x;
  47. if(!isroot(y))t[z].ch[t[z].ch[1]==y]=x;t[x].ff=z;
  48. t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y;
  49. t[x].ch[k^1]=y;t[y].ff=x;
  50. pushup(y);pushup(x);
  51. }
  52. void Splay(int x)
  53. {
  54. S[top=1]=x=getf(x);
  55. for(int i=x;!isroot(i);i=getf(t[i].ff))S[++top]=getf(t[i].ff);
  56. while(top)pushdown(S[top--]);
  57. while(!isroot(x))
  58. {
  59. int y=getf(t[x].ff),z=getf(t[y].ff);
  60. if(!isroot(y))
  61. (t[y].ch[1]==x)^(t[z].ch[1]==y)?rotate(x):rotate(y);
  62. rotate(x);
  63. }
  64. }
  65. void access(int x){x=getf(x);for(int y=0;x;y=x,x=getf(t[x].ff))Splay(x),t[x].ch[1]=y,pushup(x);}
  66. void makeroot(int x){x=getf(x);access(x);Splay(x);t[x].rev^=1;}
  67. void split(int x,int y){x=getf(x);y=getf(y);makeroot(x);access(y);Splay(y);}
  68. void link(int x,int y){x=getf(x);y=getf(y);makeroot(x);t[x].ff=y;pushup(y);}
  69. int findroot(int x){getf(x);access(x);Splay(x);while(lson)x=lson;return x;}
  70. void dfs(int x)
  71. {
  72. f[getf(x)]=tot;
  73. t[tot].v+=t[x].v;
  74. t[tot].sum+=t[x].v;
  75. if(lson)dfs(lson);
  76. if(rson)dfs(rson);
  77. }
  78. void Link(int x,int y)
  79. {
  80. if(Getf(x)!=Getf(y))ff[Getf(x)]=Getf(y),link(getf(x),getf(y));
  81. else
  82. {
  83. x=getf(x);y=getf(y);
  84. ++tot;ff[tot]=f[tot]=tot;
  85. split(x,y);
  86. dfs(y);
  87. }
  88. }
  89. void Modify(int u,int v)
  90. {
  91. makeroot(getf(u));
  92. t[getf(u)].v-=a[u];a[u]=v;
  93. t[getf(u)].v+=a[u];
  94. pushup(getf(u));
  95. }
  96. int Query(int u,int v)
  97. {
  98. if(Getf(u)!=Getf(v))return -1;
  99. u=getf(u);v=getf(v);
  100. split(u,v);
  101. return t[v].sum;
  102. }
  103. int main()
  104. {
  105. tot=n=read();m=read();
  106. for(int i=1;i<=n;++i)f[i]=ff[i]=i,t[i].v=a[i]=read();
  107. int opt,u,v;
  108. while(m--)
  109. {
  110. opt=read();u=read();v=read();
  111. if(opt==1)Link(u,v);
  112. else if(opt==2)Modify(u,v);
  113. else printf("%d\n",Query(u,v));
  114. }
  115. return 0;
  116. }

【BZOJ2959】长跑(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. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

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

  4. Link Cut Tree学习笔记

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

  5. (RE) luogu P3690 【模板】Link Cut Tree

    二次联通门 : luogu P3690 [模板]Link Cut Tree 莫名RE第8个点....如果有dalao帮忙查错的话万分感激 #include <cstdio> #includ ...

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

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

  7. Link/cut Tree

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

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

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

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

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

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

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

随机推荐

  1. Sql server 卸载方法

    sql server不正确卸载时,重新安装会失败,会提示各种错误:如数据库实例已存在等... 下面是我摸索总结出来的卸载方法,以及重装失败后的处理方法: 卸载方法: 注意:SQL Server 200 ...

  2. mac给文件批量添加后缀名

    for i in *;do mv "$i" "$i.mp4";done

  3. centos7使用docker部署gitlab-ce-zh应用

    1.国内拉取镜像比较慢,所以这里采用DaoCloud源. # curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http ...

  4. 【考试】java基础知识测试,看你能得多少分?

    1 前言 共有5道java基础知识的单项选择题,每道20分,共计100分.解析和答案在最后. 2 试题 2.1 如下程序运行结果是什么? class Parent { public Parent(St ...

  5. 2015年百度实习生前端笔试题上海卷a

    1.写出javascript运行结果:alert(‘5’+5); 结果:’55’ 2.写出javascript运行结果:for(var i=0; i<10; i++){} alert(i); 结 ...

  6. 解决 java.lang.ClassNotFoundException: org.springframework.beans.factory.config.EmbeddedValueResolver

    1.今天用maven配置了一下dubbo的项目发现启动项目后意外报错: java.lang.ClassNotFoundException: org.springframework.beans.fact ...

  7. POJ - 2698 贪心

    经典面替换算法,每次选择最远的那个碟片请求进行替换. AC代码 #include <cstdio> #include <cmath> #include <algorith ...

  8. 《Java编程思想》读书笔记

    前言 这个月一直没更新,就是一直在读这本<Java编程思想>,这本书可以在Java业界被传神的一本书,无论谁谈起这本书都说好,不管这个人是否真的读过这本书,都说啊,这本书很好.然后再看这边 ...

  9. CEPH RGW 设置 user default_placement为ssd-placement,优化100KB-200KB小文件性能,使用户创建的bucket对象放置到 SSD设备的Pool上。

    sudo radosgw-admin metadata get user:tuanzi > user.md.json vi user.md.json #to add ssd-placement ...

  10. 【java学习笔记】线程

    1.线程的定义 ①继承Thread类,将执行的任务逻辑放到run方法中,调用start方法来开启线程 public class ThreadDemo { public static void main ...