Dynamic LCA

Time Limit: 2000ms
Memory Limit: 262144KB

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

解题:LCT
 #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的更多相关文章

  1. SP8791 DYNALCA - Dynamic LCA 解题报告

    SP8791 DYNALCA - Dynamic LCA 有一个森林最初由 \(n (1 \le n \le 100000)\) 个互不相连的点构成 你需要处理以下操作: link A B:添加从顶点 ...

  2. 【题解】Luogu SP8791 DYNALCA - Dynamic LCA

    原题传送门 这题用Link-Cut-Tree解决,Link-Cut-Tree详解 这道题的难点就在如何求LCA: 我们珂以先对其中一个点进行access操作,然后对另一个点进行access操作,因为L ...

  3. SP8791 DYNALCA - Dynamic LCA

    \(\color{#0066ff}{ 题目描述 }\) 有一个森林最初由 n (\(1 \le n \le 100000\))n(\(1\leq n\leq 100000\)) 个互不相连的点构成 你 ...

  4. spoj DYNALCA - Dynamic LCA

    http://www.spoj.com/problems/DYNALCA/ 此题link.cut要求不能换根,当然也保证link时其中一个点必定已经是根. 方法: void link(Node *x, ...

  5. 主席树[可持久化线段树](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 ...

  6. CodeForcesGym 100676G Training Camp

    G. Training Camp Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces ...

  7. P6845 [CEOI2019] Dynamic Diameter

    P6845 [CEOI2019] Dynamic Diameter 题意 一颗带权树,每次更改一条边的权,每次修改后求出最大直径.强制在线. 思路 \(O(n\log^2n)\) 的暴力做法. 根据经 ...

  8. var和dynamic的区别

    1.var 1.均是声明动态类型的变量. 2.在编译阶段已经确定类型,在初始化的时候必须提供初始化的值. 3.无法作为方法参数类型,也无法作为返回值类型. 2.dynamic 1.均是声明动态类型的变 ...

  9. BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]

    3083: 遥远的国度 Time Limit: 10 Sec  Memory Limit: 1280 MBSubmit: 3127  Solved: 795[Submit][Status][Discu ...

随机推荐

  1. _bzoj1015 [JSOI2008]星球大战starwar【并查集】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1015 倒过来做就ok了. #include <cstdio> #include ...

  2. Jumping Jack CodeForces - 11B

    Jumping Jack CodeForces - 11B 就是一个贪心. 基本思路: 正负没有关系,先取绝对值. 首先跳过头,然后考虑怎么回来. 设超过头的步数为kk.如果kk为偶数,那么直接在前面 ...

  3. A Simple Task CodeForces - 11D

    A Simple Task CodeForces - 11D 题意:输出一个无向图的简单环数量.简单环指无重复边的环.保证图无重边自环. ans[i][j]表示"包含i中的点,以i中第一个点 ...

  4. Environment中有大量访问目录的函数

    public class Environment { /** * Return root of the "system" partition holding the core An ...

  5. AJPFX解析成员变量和局部变量

    成员变量和局部变量 3.1.成员变量和局部变量 A:在类中的位置不同         * 成员变量:在类中方法外         * 局部变量:在方法定义中或者方法声明上 B:在内存中的位置不同   ...

  6. linux设置ssh连接时间

    相信大家经常遇到SSH连接闲置一会就断开需要重新连接的痛苦,为了使SSH连接保持足够长的时间,我们可以使用如下两种设置 1.sshd服务配置: #vi /etc/ssh/sshd_config Cli ...

  7. 网站推广--Html关键词代码解说

    html的meta标签 1. meta标签是内嵌在你网页中的特殊html标签,包含着你有关于你网页的一些隐藏信息.Meat标签的作用是向搜索引擎解释你的网页是有关哪方面信息的.对于高级的搜索引擎来说, ...

  8. vue2.0 动态切换组件

    组件标签是Vue框架自定义的标签,它的用途就是可以动态绑定我们的组件,根据数据的不同更换不同的组件. <!DOCTYPE html> <html lang="en" ...

  9. 按键精灵txt判断

      句子 = "度阿斯达娘阿婶是大的百度知道" 词 = "百度知道" MyPos = Instr(句子, 词) If MyPos > 0 Then Tra ...

  10. bat 时间 的运算与提取

    比如在系统中date这个环境变量的值为 -- 星期六 年------%date:~,% 表示从左向右指针向右偏0位,然后从指针偏移到的位置开始提取4位字符,结果是2011 月------%date:~ ...