CodeForcesGym 100512D Dynamic LCA
Dynamic LCA
This problem will be judged on CodeForcesGym. Original ID: 100512D
64-bit integer IO format: %I64d Java class name: (Any)


#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct arc {
int to,next;
arc(int x = ,int y = -) {
to = x;
next = y;
}
} e[maxn<<];
int head[maxn],tot;
void add(int u,int v) {
e[tot] = arc(v,head[u]);
head[u] = tot++;
}
struct LCT {
int fa[maxn],ch[maxn][],parent[maxn],flip[maxn];
inline void pushdown(int x) {
if(flip[x]) {
flip[ch[x][]] ^= ;
flip[ch[x][]] ^= ;
swap(ch[x][],ch[x][]);
flip[x] = ;
}
}
void build(int u,int v) {
fa[v] = ch[v][] = ch[v][] = ;
flip[v] = ;
parent[v] = u;
}
void rotate(int x,int kd) {
int y = fa[x];
pushdown(y);
pushdown(x);
ch[y][kd^] = ch[x][kd];
fa[ch[x][kd]] = y;
fa[x] = fa[y];
ch[x][kd] = y;
fa[y] = x;
if(fa[x]) ch[fa[x]][y == ch[fa[x]][]] = x;
}
void splay(int x,int goal = ) {
int y = x;
while(fa[y]) y = fa[y];
pushdown(x);
if(x != y) {
parent[x] = parent[y];
parent[y] = ;
while(fa[x] != goal) {
pushdown(fa[fa[x]]);
pushdown(fa[x]);
pushdown(x);
if(fa[fa[x]] == goal) rotate(x,x == ch[fa[x]][]);
else {
int y = fa[x],z = fa[y],s = (y == ch[z][]);
if(x == ch[y][s]) {
rotate(x,s^);
rotate(x,s);
} else {
rotate(y,s);
rotate(x,s);
}
}
}
}
}
void access(int x) {
for(int y = ; x; x = parent[x]) {
splay(x);
fa[ch[x][]] = ;
parent[ch[x][]] = x;
ch[x][] = y;
fa[y] = x;
parent[y] = ;
y = x;
}
}
int LCA(int x,int y) {
access(y);
splay(y);
for(int z = x; z; z = parent[z]) {
splay(z);
if(!parent[z]) return z;
}
return -;
}
void makeroot(int x) {
access(x);
splay(x);
flip[x] ^= ;
}
void init(){
memset(flip,,sizeof flip);
memset(ch,,sizeof ch);
memset(parent,,sizeof parent);
memset(fa,,sizeof fa);
}
} lct;
void dfs(int u,int fa) {
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].to == fa) continue;
lct.build(u,e[i].to);
dfs(e[i].to,u);
}
}
int main() {
#define NAME "dynamic"
freopen(NAME".in","r",stdin);
freopen(NAME".out","w",stdout);
int n,m,u,v;
char op[];
while(scanf("%d",&n),n) {
memset(head,-,sizeof head);
tot = ;
lct.init();
for(int i = ; i < n; ++i) {
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
lct.build(,);
dfs(,);
scanf("%d",&m);
while(m--) {
scanf("%s%d",op,&u);
if(op[] == '!') lct.makeroot(u);
else if(op[] == '?') {
scanf("%d",&v);
printf("%d\n",lct.LCA(u,v));
}
}
}
return ;
}
/*
9
1 2
1 3
2 4
2 5
3 6
3 7
6 8
6 9
10
? 4 5
? 5 6
*/
CodeForcesGym 100512D Dynamic LCA的更多相关文章
- SP8791 DYNALCA - Dynamic LCA 解题报告
SP8791 DYNALCA - Dynamic LCA 有一个森林最初由 \(n (1 \le n \le 100000)\) 个互不相连的点构成 你需要处理以下操作: link A B:添加从顶点 ...
- 【题解】Luogu SP8791 DYNALCA - Dynamic LCA
原题传送门 这题用Link-Cut-Tree解决,Link-Cut-Tree详解 这道题的难点就在如何求LCA: 我们珂以先对其中一个点进行access操作,然后对另一个点进行access操作,因为L ...
- SP8791 DYNALCA - Dynamic LCA
\(\color{#0066ff}{ 题目描述 }\) 有一个森林最初由 n (\(1 \le n \le 100000\))n(\(1\leq n\leq 100000\)) 个互不相连的点构成 你 ...
- spoj DYNALCA - Dynamic LCA
http://www.spoj.com/problems/DYNALCA/ 此题link.cut要求不能换根,当然也保证link时其中一个点必定已经是根. 方法: void link(Node *x, ...
- 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )
在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...
- CodeForcesGym 100676G Training Camp
G. Training Camp Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...
- P6845 [CEOI2019] Dynamic Diameter
P6845 [CEOI2019] Dynamic Diameter 题意 一颗带权树,每次更改一条边的权,每次修改后求出最大直径.强制在线. 思路 \(O(n\log^2n)\) 的暴力做法. 根据经 ...
- var和dynamic的区别
1.var 1.均是声明动态类型的变量. 2.在编译阶段已经确定类型,在初始化的时候必须提供初始化的值. 3.无法作为方法参数类型,也无法作为返回值类型. 2.dynamic 1.均是声明动态类型的变 ...
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
随机推荐
- 【Visual Studio Code 】使用Visual Studio Code + Node.js搭建TypeScript开发环境
1.准备工作 Node.js Node.js - Official Site Visual Studio Code Visual Studio Code - Official Site 安装Node. ...
- static属性
static 属于全局,也就是类的属性 和方法,换句话说 一个类,不管有多少个实例,却只有一个全局变量 用static修饰的属性和方法称为静态属性和方法 需要注意的是 静态属性和方法属于类方法,加载类 ...
- E. Comments dfs模拟
http://codeforces.com/contest/747/problem/E 首先,把字符串变成这个样子. hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0 ...
- Oracle中默认创建的表
安装Oracle数据库后,会自动创建几个表.分别是emp.dept.bonus(也有可能不一样),这些表都在scott账户中.
- ftp 上传与下载
//上传 ftpmg.Upload("", DateTime.Now.ToString("yyyyMMddhhmmss")); //下载 ftpmg.Downl ...
- html5表单新增元素与属性2
1.标签的control属性 在html5中,可以在标签内部放置一个表单元素,并且通过该标签的control属性来访问该表单元素. <script> function setValue() ...
- 虚拟机下安装 CentOS 7 的几个小问题
※ 网络问题(Destination Host Unreachable) 安装时网络选择的"桥接"模式, 安装完毕,并配置IP地址后,发现只能ping通自己,局域网内的其他IP无法 ...
- java 对sql格式化
public class SqlFormat{ public static void main(String[] args){ String sql=""; sqlFormat(s ...
- elasticsearch学习笔记-倒排索引以及中文分词
我们使用数据库的时候,如果查询条件太复杂,则会涉及到很多问题 1.无法维护,各种嵌套查询,各种复杂的查询,想要优化都无从下手 2.效率低下,一般语句复杂了之后,比如使用or,like %,,%查询之后 ...
- 原生jsonp跨域
<script> // jsonp跨域原生写法 var script = document.createElement('script'); script.src = 'http://19 ...