[luogu3950] 部落冲突 - Link Cut Tree
有了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的更多相关文章
- link cut tree 入门
鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题
A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...
- Link/cut Tree
Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...
- 洛谷P3690 Link Cut Tree (模板)
Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门
link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- Link Cut Tree学习笔记
从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...
- [CodeForces - 614A] A - Link/Cut Tree
A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...
随机推荐
- lucas定理 模板
lucas定理 (nm) mod p=(⌊np⌋⌊mp⌋)(n mod&VeryTh ...
- 纪中集训2020.02.09【NOIP提高组】模拟B 组总结反思
目录 JZOJ.1747[NOIP2014模拟11.5]无穷迷宫 比赛时 之后 总结 JZOJ1478.[NOIP2014模拟11.5]近似乘积 比赛时 之后 总结 JZOJ3926. [NOIP20 ...
- 洛谷题解 P1134 【阶乘问题】
原题传送门 题目描述 也许你早就知道阶乘的含义,N阶乘是由1到N相乘而产生,如: 12!=1×2×3×4×5×6×7×8×9×10×11×12=479,001,600 12的阶乘最右边的非零位为6. ...
- Chocolaty
原文是用markdown格式写的,稍微改了下发了博客,格式可能会很奇怪.. Chocolaty官网 Chocolaty是一款Windows平台的包管理工具,类似于centos的yum或ubuntu的a ...
- windows下web端测试环境搭建(tomcat+oracle)
一.安装oracle数据库 1.关闭防火墙.360安全卫士,运行安装程序:Setup.exe,然后下一步...... 2.安装完成后,检查服务是否已启动 3.cmd输入验证登录成功:sqlplus s ...
- jQuery中校验时间格式的正则表达式小结
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- hyper-v虚拟机不能访问外网的解决方案
直接说解决方案,将虚拟机的一张网卡改为旧版网络适配器即可.具体原因还不可知. 延申一下,一般应该使用的交换机,是“外部”类型即可.
- Java如何自定义注解
本文主要是记录所学,以供后续参考.注解是Java 1.5引入的,Java自定义注解是通过运行时靠反射获取注解,注解相当于是一种嵌入在程序中的元数据,可以使用注解解析工具或编译器对其进行解析,也可以指定 ...
- 原生js实现拖拽功能
1. 给个div,给定一些样式 <div class="drag" style="left:0;top:0;width:100px;height:100px&quo ...
- Win10安装4 —— 通过BIOS进入PE
本文内容皆为作者原创,如需转载,请注明出处:https://www.cnblogs.com/xuexianqi/p/12369367.html 一:"BIOS"与"PE& ...