\(\color{#0066ff}{ 题目描述 }\)

有一个森林最初由 n (\(1 \le n \le 100000\))n(\(1\leq n\leq 100000\)) 个互不相连的点构成

你需要处理以下操作:

link A B:添加从顶点A到B的边,使A成为B的子节点,其中保证A是一个根顶点,A和B在不同的树中。

cut A:切断点A到其父节点的边,保证A是一个非根节点。

lca A B:输出A和B的最近共同祖先,保证A和B在同一棵树中。

\(\color{#0066ff}{输入格式}\)

第一行包含三个正整数N、M,分别表示树的结点个数、询问的个数

接下来M行表示询问。

\(\color{#0066ff}{输出格式}\)

对于每个LCA询问输出答案

\(\color{#0066ff}{输入样例}\)

5 9
lca 1 1
link 1 2
link 3 2
link 4 3
lca 1 4
lca 3 4
cut 4
link 5 3
lca 1 5

\(\color{#0066ff}{输出样例}\)

1
2
3
2

\(\color{#0066ff}{数据范围与提示}\)

none

\(\color{#0066ff}{ 题解 }\)

LCT求LCA

cut的时候,因为删的是父子边,暴力找前驱就行了

#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL in() {
char ch; int x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 1e5 + 5;
struct LCT {
protected:
struct node {
node *ch[2], *fa;
int rev;
node(int rev = 0): rev(rev) { ch[0] = ch[1] = fa = NULL; }
bool ntr() { return fa && (fa->ch[1] == this || fa->ch[0] == this); }
bool isr() { return fa->ch[1] == this; }
void trn() { std::swap(ch[0], ch[1]); rev ^= 1; }
void dwn() { if(rev) { if(ch[0]) ch[0]->trn(); if(ch[1]) ch[1]->trn(); rev = 0; } }
}s[maxn], *lst, *t[maxn];
int top, fa[maxn];
void rot(node *x) {
node *y = x->fa, *z = y->fa; int k = x->isr(); node *w = x->ch[!k];
if(y->ntr()) z->ch[y->isr()] = x;
x->ch[!k] = y, y->ch[k] = w;
y->fa = x, x->fa = z;
if(w) w->fa = y;
}
void splay(node *o) {
t[top = 1] = o;
while(t[top]->ntr()) t[top + 1] = t[top]->fa, top++;
while(top) t[top--]->dwn();
while(o->ntr()) { if(o->fa->ntr()) rot(o->isr() ^ o->fa->isr()? o : o->fa); rot(o); }
}
void access(node *x) { for(node *y = NULL; x; x = (y = x)->fa) splay(x), x->ch[1] = y, lst = x; }
void makeroot(node *x) { access(x), splay(x), x->trn(); }
node *findroot(node *x) {
access(x), splay(x);
while(x->dwn(), x->ch[0]) x = x->ch[0];
return splay(x), x;
}
void link(node *x, node *y) { makeroot(x), x->fa = y; }
node *pre(node *x) {
access(x), splay(x); x->dwn(), x = x->ch[0];
while(x->dwn(), x->ch[1]) x = x->ch[1];
return splay(x), x;
}
void cut(node *x) {
access(x), splay(x), x->dwn();
node *y = x->ch[0];
while(y->dwn(), y->ch[1]) y = y->ch[1];
splay(y);
y->ch[1] = x->fa = NULL;
}
public:
int LCA(int x, int y) { return access(s + x), access(s + y), lst - s; }
void link(int x, int y) { link(s + x, s + y); }
void cut(int x) { cut(s + x); }
}v;
char getch() {
char ch;
while(!isalpha(ch = getchar()));
return ch;
}
int main() {
int n = in(), m = in();
int x, y;
while(m --> 0) {
if(getch() == 'l') {
if(getch() == 'c') x = in(), y = in(), printf("%d\n", v.LCA(x, y));
else x = in(), y = in(), v.link(x, y);
}
else v.cut(in());
}
return 0;
}

SP8791 DYNALCA - 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. spoj DYNALCA - Dynamic LCA

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

  4. CodeForcesGym 100512D Dynamic LCA

    Dynamic LCA Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. ...

  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. P6845 [CEOI2019] Dynamic Diameter

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

  7. var和dynamic的区别

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

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

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

  9. BZOJ 3626: [LNOI2014]LCA [树链剖分 离线|主席树]

    3626: [LNOI2014]LCA Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2050  Solved: 817[Submit][Status ...

随机推荐

  1. 任意输入3个数,判断能否组成三角形(python)

    任意输入3个数,判断能否组成三角形. 三角形:两边之和大于第三边 直角三角形:勾股定理 代码如下: # 输入合法性检查,必须输入正数,不支持科学计数法'''try: <语句>except ...

  2. Vue开发模板简介

    1.    传统发开模式的问题 用传统模式引用vue.js以及其他的js文件的开发方式,会产生一些问题. 基于页面的开发模式:传统的引用vue.js以及其他的js文件的开发方式,限定了我们的开发模式是 ...

  3. 五颜六色的记事本 Notepad2.cn

    这是一款五颜六色的记事本,支持同时五种颜色的标签录入,可随意切换. 考虑到使用者的用眼舒适度,特意采用颜色对比明显并且色调柔和的配色方案,选择通用的微软雅黑字体作为编辑字体,字体工整便于识别. 针对使 ...

  4. myeclipse与eclipse的web项目部署区别

    一.myeclipse之web项目的部署(发布)流程 web项目的部署(发布)流程2008-01-18 14:35 在myeclipse下新建web工程abc.系统设置默认如下: 项目保存位置:wor ...

  5. ASP.NET——配置文件——连接字符串

    一.连接字符串 1.通过<connectionStrings>方式: 方式一:SqlServer身份验证 <connectionStrings> <add name=&q ...

  6. LNMP 1.5 php-fpm配置文件

    php-fpm配置文件: /usr/local/php/etc/php-fpm.conf    :php-fpm服务的配置文件 /usr/local/php/etc/php.ini       :ph ...

  7. Elasticsearch之curl创建索引

    前提,是 Elasticsearch之curl创建索引库 [hadoop@djt002 elasticsearch-2.4.3]$ curl -XPUT 'http://192.168.80.200: ...

  8. IDEA创建的Maven项目中 解决编写pom.xml没有提示

    问题如下 没有提示信息 解决方案 把Repositories中的配置更新成本地仓库 问题解决

  9. PagerSlidingTabStrip(viewPage滑动菜单)

    Github地址:https://github.com/astuetz/PagerSlidingTabStrip 1,Include the library dependencies { compil ...

  10. powerdesigner自动将name填充到注释的脚本

    我在建模的时候,希望在生成脚本的时候有注释,所以才会看到Comment列,实际上,只要你的表中的Name列不为空,运行下面的VBScript,PD会帮你自动填充注释的Comment列值. '把pd中那 ...