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. _bzoj1061 [Noi2008]志愿者招募【最小费用最大流】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1061 尽管不是mcmf的裸题,但还是保存一下模版叭~ 很好的一道建模的题,把变量间的加加减减 ...

  2. greater()和less()的使用

    greater和less是头文件<xfunctional>中定义的两个结构.下面看它们 的定义,greater和less都重载了操作符(). // TEMPLATE STRUCT grea ...

  3. 题解报告:hdu 2062 Subset sequence

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2062 Problem Description 考虑集合An = {1,2,...,n}. 例如,A1 ...

  4. [C#基础知识系列]专题十:全面解析可空类型[转]

    原文链接 主要内容: 1:空合并操作符(?? 操作符) ??操作符也就是"空合并操作符",它代表的意思是两个操作数,如果左边的数不为null时,就返回左边的数,如果左边的数为nul ...

  5. D. Arpa's weak amphitheater and Mehrdad's valuable Hoses 分组背包模板题

    http://codeforces.com/problemset/problem/742/D 并查集预处理出所有关系. 一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判 ...

  6. Sql Server把本地数据库传到服务器数据库

    上一篇文章我们已经把网站布署到服务器中了,如果是动态网站肯定是有数据库的,接下来通过Sql Server把本地数据库上传到服务器数据库中. 打开Sql Server连接本地数据库,选中要导出的数据库, ...

  7. Spring注解驱动开发之Ioc容器篇

    前言:现今SpringBoot.SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了Spring的一些底层注解.原理,比如@Conditional.@Import.@Ena ...

  8. Android开源项目:GifView——Android显示GIF动画

    下载:http://code.google.com/p/gifview/downloads/list 简介:android中现在没有直接显示gif的view,只能通过mediaplay来显示,且还常常 ...

  9. InChatter系统开源聊天模块前奏曲

    最近在研究WCF,又因为工作中的项目需要,要为现有的系统增加一个聊天模块以及系统消息提醒等,因此就使用WCF做服务器端开发了一个简单的系统. 开发最初学习了东邪孤独大哥的<传说的WCF系列> ...

  10. xamarin 学习笔记02- IOS Simulator for windows 安装

    微软发布了在window下的ios模拟器 下载 ios模拟器 并安装在windows系统上. Xamarin for Visual Studio 和 网络上的 Mac 中的 Xamarin.iOS 开 ...