【题解】Luogu SP8791 DYNALCA - Dynamic LCA
原题传送门
这题用Link-Cut-Tree解决,Link-Cut-Tree详解
这道题的难点就在如何求LCA:
我们珂以先对其中一个点进行access操作,然后对另一个点进行access操作,因为LCA到根的边一定都由第一次access变为实边了,在之后的这一次access操作的最后一条从虚边变为实边的边的父亲就是两点的LCA
回求LCA后,剩下的几乎是模板,但有两种做法
1.建立虚拟点n+1,保证一直是一颗有n+1个点的树,写起来比较无脑(lct部分珂以复制),常数比较大
#include <bits/stdc++.h>
#define N 100005
#define getchar nc
using namespace std;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
register int x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*f;
}
inline void write(register int x)
{
if(!x)putchar('0');if(x<0)x=-x,putchar('-');
static int sta[20];register int tot=0;
while(x)sta[tot++]=x%10,x/=10;
while(tot)putchar(sta[--tot]+48);
}
inline void Swap(register int &a,register int &b)
{
a^=b^=a^=b;
}
struct Link_Cut_Tree{
int c[N][2],fa[N],top,q[N],rev[N];
inline void pushdown(register int x)
{
if(rev[x])
{
register int l=c[x][0],r=c[x][1];
rev[l]^=1,rev[r]^=1,rev[x]^=1;
Swap(c[x][0],c[x][1]);
}
}
inline bool isroot(register int x)
{
return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
}
inline void rotate(register int x)
{
int y=fa[x],z=fa[y],l,r;
l=c[y][0]==x?0:1;
r=l^1;
if(!isroot(y))
c[z][c[z][0]==y?0:1]=x;
fa[x]=z;
fa[y]=x;
fa[c[x][r]]=y;
c[y][l]=c[x][r];
c[x][r]=y;
}
inline void splay(register int x)
{
top=1;
q[top]=x;
for(register int i=x;!isroot(i);i=fa[i])
q[++top]=fa[i];
for(register int i=top;i;--i)
pushdown(q[i]);
while(!isroot(x))
{
int y=fa[x],z=fa[y];
if(!isroot(y))
rotate((c[y][0]==x)^(c[z][0]==y)?(x):(y));
rotate(x);
}
}
inline void access(register int x)
{
for(register int t=0;x;t=x,x=fa[x])
{
splay(x);
c[x][1]=t;
}
}
inline void makeroot(register int x)
{
access(x);
splay(x);
rev[x]^=1;
}
inline void split(register int x,register int y)
{
makeroot(x);
access(y);
splay(y);
}
inline void cut(register int x,register int y)
{
split(x,y);
c[y][0]=0;
fa[x]=0;
}
inline void link(register int x,register int y)
{
makeroot(x);
fa[x]=y;
}
inline int LCA(register int x,register int y)
{
access(x);
int t;
for(t=0;y;t=y,y=fa[y])
{
splay(y);
c[y][1]=t;
}
return t;
}
}T;
int fa[N];
int main()
{
int n=read(),m=read();
for(register int i=1;i<=n;++i)
T.link(i,n+1),fa[i]=n+1;
while(m--)
{
char c=getchar();
while(c!='k'&&c!='t'&&c!='a')
c=getchar();
if(c=='k')
{
int x=read(),y=read();
T.cut(x,fa[x]);
fa[x]=y;
T.link(x,y);
}
else if(c=='t')
{
int x=read();
T.cut(x,fa[x]);
fa[x]=n+1;
T.link(x,n+1);
}
else
{
int x=read(),y=read();
T.makeroot(n+1);
write(T.LCA(x,y)),puts("");
}
}
return 0;
}
2.按照题目加边,删边(常数小,容易写错)
注意:cut和LCA操作是不能使用makeroot操作的,因为makeroot本身会修改树上的LCA关系,当然link操作使用makeroot是没有关系的
#include <bits/stdc++.h>
#define N 100005
#define getchar nc
using namespace std;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
register int x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*f;
}
inline void write(register int x)
{
if(!x)putchar('0');if(x<0)x=-x,putchar('-');
static int sta[20];register int tot=0;
while(x)sta[tot++]=x%10,x/=10;
while(tot)putchar(sta[--tot]+48);
}
inline void Swap(register int &a,register int &b)
{
a^=b^=a^=b;
}
struct Link_Cut_Tree{
int c[N][2],fa[N],top,q[N],rev[N];
inline void pushdown(register int x)
{
if(rev[x])
{
register int l=c[x][0],r=c[x][1];
rev[l]^=1,rev[r]^=1,rev[x]^=1;
Swap(c[x][0],c[x][1]);
}
}
inline bool isroot(register int x)
{
return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
}
inline void rotate(register int x)
{
int y=fa[x],z=fa[y],l,r;
l=c[y][0]==x?0:1;
r=l^1;
if(!isroot(y))
c[z][c[z][0]==y?0:1]=x;
fa[x]=z;
fa[y]=x;
fa[c[x][r]]=y;
c[y][l]=c[x][r];
c[x][r]=y;
}
inline void splay(register int x)
{
top=1;
q[top]=x;
for(register int i=x;!isroot(i);i=fa[i])
q[++top]=fa[i];
for(register int i=top;i;--i)
pushdown(q[i]);
while(!isroot(x))
{
int y=fa[x],z=fa[y];
if(!isroot(y))
rotate((c[y][0]==x)^(c[z][0]==y)?(x):(y));
rotate(x);
}
}
inline void access(register int x)
{
for(register int t=0;x;t=x,x=fa[x])
{
splay(x);
c[x][1]=t;
}
}
inline void makeroot(register int x)
{
access(x);
splay(x);
rev[x]^=1;
}
inline void cut(register int x)
{
access(x);
splay(x);
fa[c[x][0]]=0;
c[x][0]=0;
}
inline void link(register int x,register int y)
{
makeroot(x);
fa[x]=y;
}
inline int LCA(register int x,register int y)
{
access(x);
int t;
for(t=0;y;t=y,y=fa[y])
{
splay(y);
c[y][1]=t;
}
return t;
}
}T;
int fa[N];
int main()
{
int n=read(),m=read();
while(m--)
{
char c=getchar();
while(c!='k'&&c!='t'&&c!='a')
c=getchar();
if(c=='k')
{
int x=read(),y=read();
T.link(x,y);
}
else if(c=='t')
{
int x=read();
T.cut(x);
}
else
{
int x=read(),y=read();
write(T.LCA(x,y)),puts("");
}
}
return 0;
}
【题解】Luogu SP8791 DYNALCA - Dynamic LCA的更多相关文章
- SP8791 DYNALCA - Dynamic LCA 解题报告
SP8791 DYNALCA - Dynamic LCA 有一个森林最初由 \(n (1 \le n \le 100000)\) 个互不相连的点构成 你需要处理以下操作: link A B:添加从顶点 ...
- 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, ...
- CodeForcesGym 100512D Dynamic LCA
Dynamic LCA Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. ...
- [题解] Luogu P5446 [THUPC2018]绿绿和串串
[题解] Luogu P5446 [THUPC2018]绿绿和串串 ·题目大意 定义一个翻转操作\(f(S_n)\),表示对于一个字符串\(S_n\), 有\(f(S)= \{S_1,S_2,..., ...
- Bzoj 2286 & Luogu P2495 消耗战(LCA+虚树+欧拉序)
题面 洛谷 Bzoj 题解 很容易想到$O(nk)$的树形$dp$吧,设$f[i]$表示处理完这$i$颗子树的最小花费,同时再设一个$mi[i]$表示$i$到根节点$1$路径上的距离最小值.于是有: ...
- 题解 Luogu P2499: [SDOI2012]象棋
关于这道题, 我们可以发现移动顺序不会改变答案, 具体来说, 我们有以下引理成立: 对于一个移动过程中的任意一个移动, 若其到达的位置上有一个棋子, 则该方案要么不能将所有棋子移动到最终位置, 要么可 ...
- 题解 luogu P1144 【最短路计数】
本蒟蒻也来发一次题解第一篇请见谅 这个题有几个要点 1.无向无权图,建图的时候别忘记建来回的有向边[因此WA掉1次 2.无权嘛,那么边长建成1就好了2333333 3.最短路采用迪杰斯特拉(别忘用堆优 ...
- 题解 Luogu P1110 【[ZJOI2007]报表统计】
感谢 @cmy962085349 提供的hack数据,已经改对了. 先声明,我好像是题解里写双$fhq$ $treap$里唯一能过的...(最后两个点啊) 思路:首先看题目,$MIN_GAP_SORT ...
随机推荐
- js模拟链表
链表: 每个元素,都有一个指针,指向下一个元素 //链表 function LinkedList(){ var head = null; length = 0; this.append = funct ...
- ICSharpCode.TextEditor使用及扩展
SharpDevelop (#develop)有很多“副产品”,其中最出名的应算SharpZipLib (#ziplib),纯C#的ZIP类库,而在SharpDevelop (#develop)中,“ ...
- calibur处理ROSETTA输出的多个结构文件,clustering
下载网址:https://sourceforge.net/projects/calibur/ 安装: $ tar zxvf calibur.tar.gz $ cd calibur $ make 安装完 ...
- android 流程跟踪
#记录一下 Thread cur_thread = Thread.currentThread(); StackTraceElement stack[] = cur_thread.getStackTra ...
- css省...和div 内容过多,自动换行
1.shengluohao 就是这个... 加: overflow: hidden;/*超出部分隐藏*/ white-space: nowrap;/*不换行*/ text-overflow:ellip ...
- cocos2d JS-(JavaScript) 类型检测与判断
//检测类型 var str = "Hello World"; if (typeof str=="string") {//使用typeof来判断对象类型的一个例 ...
- 前端c标签foreach传值给后台
前端c标签foreach传值给后台 <div style="margin-bottom: 10px"> <c:forEach items="${good ...
- Visual Studio 2015 开发Android Cordova出现unsupported major minor version 52.0错误的解决方法
JDK版本的问题,需要JDK1.8版本,安装!VS2015做如下设置, 工具->选项->用于Apache Cordoba的工具->环境变量替代->JAVA_HOME设为1.8:
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- Nginx.代理MySQL
Nginx.代理MySQL 1. Nginx在安装的时候,需要加上一个参数:--with-stream 即Nginx安装指令为:./configure --prefix=/u01/app/nginx ...