有了LCT这不就是思博题了吗

#include <bits/stdc++.h>
using namespace std; const int N = 1000000; int n,m,t1,t2,t3;
char op[5]; struct LinkCutTree {
int top, q[N], ch[N][2], fa[N], rev[N];
inline void pushup(int x) { }
inline void pushdown(int x) {
if(!rev[x])
return;
rev[ch[x][0]]^=1;
rev[ch[x][1]]^=1;
rev[x]^=1;
swap(ch[x][0],ch[x][1]);
}
inline bool isroot(int x) {
return ch[fa[x]][0]!=x && ch[fa[x]][1]!=x;
}
inline void rotate(int p) {
int q=fa[p], y=fa[q], x=ch[fa[p]][1]==p;
ch[q][x]=ch[p][x^1];
fa[ch[q][x]]=q;
ch[p][x^1]=q;
fa[q]=p;
fa[p]=y;
if(y)
if(ch[y][0]==q)
ch[y][0]=p;
else if(ch[y][1]==q)
ch[y][1]=p;
pushup(q);
pushup(p);
}
inline void splay(int x) {
q[top=1]=x;
for(int i=x; !isroot(i); i=fa[i])
q[++top]=fa[i];
for(int i=top; i; i--)
pushdown(q[i]);
for(; !isroot(x); rotate(x))
if(!isroot(fa[x]))
rotate((ch[fa[x]][0]==x)==(ch[fa[fa[x]]][0]==fa[x])?fa[x]:x);
}
void access(int x) {
for(int t=0; x; t=x,x=fa[x])
splay(x),ch[x][1]=t,pushup(x);
}
void makeroot(int x) {
access(x);
splay(x);
rev[x]^=1;
}
int find(int x) {
access(x);
splay(x);
while(ch[x][0])
x=ch[x][0];
return x;
}
void split(int x,int y) {
makeroot(x);
access(y);
splay(y);
}
void cut(int x,int y) {
split(x,y);
if(ch[y][0]==x)
ch[y][0]=0, fa[x]=0;
}
void link(int x,int y) {
makeroot(x);
fa[x]=y;
}
} tree; vector <pair<int,int> > vc; int main() {
scanf("%d%d",&n,&m);
for(int i=1; i<n; i++) {
scanf("%d%d",&t1,&t2);
tree.link(t1,t2);
}
for(int i=1; i<=m; i++) {
scanf("%s",op);
if(op[0]=='Q') {
scanf("%d%d",&t1,&t2);
if(tree.find(t1) == tree.find(t2))
printf("Yes\n");
else
printf("No\n");
}
if(op[0]=='C') {
scanf("%d%d",&t1,&t2);
tree.cut(t1,t2);
vc.push_back(make_pair(t1,t2));
}
if(op[0]=='U') {
scanf("%d",&t1);
t2=vc[t1-1].first, t3=vc[t1-1].second;
tree.link(t2,t3);
}
}
}

[luogu3950] 部落冲突 - Link Cut Tree的更多相关文章

  1. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

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

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

  3. Link/cut Tree

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

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

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

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

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

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

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

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

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

  8. Link Cut Tree学习笔记

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

  9. [CodeForces - 614A] A - Link/Cut Tree

    A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...

随机推荐

  1. Kali Linux中Chrome浏览器不能启动的问题

    kali中自带了Chromium Web Browser,我点了几次没反应.我还以为是Chrome的版本问题.于是下载了Chrome的deb包. 安装中还解决了一个包依赖问题.安装成功还是不能启动.于 ...

  2. session 控制

    session 控制 beego 内置了 session 模块,目前 session 模块支持的后端引擎包括 memory.cookie.file.mysql.redis.couchbase.memc ...

  3. [大数据技术]Kettle初次连接MySQL数据库 报错问题 错误连接数据库 Error occured while trying to connect to the database Exception while loading class org.gjt.mm.mysql.Driver

    报错内容如下: 错误连接数据库 [foodmartconn] : org.pentaho.di.core.exception.KettleDatabaseException: Error occure ...

  4. 【终端使用】用户权限和"chmod"命令的简单使用

    一.用户权限知识点 1.1.基本概念 用户是Linux系统工作中重要的一环,用户管理包括 用户管理 和 组管理. 在Linux系统中,不论由本机登录系统 或者 远程登录系统,每个系统都必须拥有一个账号 ...

  5. Python If&字典 初学者笔记

    and 当俩个条件都满足时为True否为False or 任意一个条件满足时为True否为Flase not in  通常用于If语句,用来判断一个元素是否不在某个列表中 banned_user = ...

  6. CSS实现同一行中图片和文本垂直居中

    1.为图片和文本都设置vertical-align:middle

  7. LeetCode 9、判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

    class Solution: def isPalindrome(self, x: int) -> bool: a = x if a<0: return False else: num = ...

  8. gulp常用插件之gulp-notify使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 gulp-notify这是一款gulp通知插件. 更多使用文档请点击访问gulp-notify工具官网. 安装 一键安装不多解释 npm inst ...

  9. redis 4.x及以上的未授权访问

    00x01 环境搭建 选择在kali中复现 选择了redis5.0.5版本 1.下载并安装: $ wget http://download.redis.io/releases/redis-5.0.5. ...

  10. Edit页面返回ViewBag json字符串与前端js交互

    很多时候,在打开一个新页面的时候,后端需要同时传一个json字符串给前端页面,但是我们又不能把action的返回值改为string,这时候我们就需要用到ViewBag将json字符串传回到前端,前端j ...