【题解】Luogu P3950 部落冲突
原题传送门
这题用Link-Cut-Tree解决,Link-Cut-Tree详解
我们用Link-Cut-Tree维护连通性(十分无脑)
一开始先把树中每条边的两端连接
U操作:把u,v两个点连起来
C操作:把u,v两个点分开来
Q操作:判断在这个森林里u的根和v的根是否相等(是否连通)
#include <bits/stdc++.h>
#define N 300005
#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 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 bool isroot(register int x)
{
return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
}
inline void pushdown(register int x)
{
if(rev[x])
{
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 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 int findroot(register int x)
{
access(x);
splay(x);
while(c[x][0])
x=c[x][0];
return x;
}
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;
}
}T;
int n,m,cnt;
int a[N],b[N];
int main()
{
n=read(),m=read();
for(register int i=1;i<n;++i)
{
int u=read(),v=read();
T.link(u,v);
}
while(m--)
{
char ch=getchar();
while(ch!='Q'&&ch!='C'&&ch!='U')
ch=getchar();
if(ch=='Q')
{
int x=read(),y=read();
puts(T.findroot(x)==T.findroot(y)?"Yes":"No");
}
else if(ch=='C')
{
a[++cnt]=read(),b[cnt]=read();
T.cut(a[cnt],b[cnt]);
}
else
{
int x=read();
T.link(a[x],b[x]);
}
}
}
【题解】Luogu P3950 部落冲突的更多相关文章
- 【luogu P3950 部落冲突】 题解
题目连接:https://www.luogu.org/problemnew/show/P3950 1.像我这种学数据结构学傻了的 2.边权化点权 所有点权初始化0 3.对于战争 将深度较深的-1,对于 ...
- luogu P3950 部落冲突
嘟嘟嘟 树剖板子题. #include<cstdio> #include<iostream> #include<algorithm> #include<cma ...
- lupgu P3950 部落冲突
题目链接 luogu P3950 部落冲突 题解 树剖线段树可以 lct还行 代码 #include<cstdio> #include<algorithm> inline in ...
- 洛谷 P3950 部落冲突 树链剖分
目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例1 输出样例1 输入样例2 输出样例2 输入样例3 输出样例3 说明 思路 AC代码 总结 题面 题目链接 P3 ...
- 洛谷P3950 部落冲突 [LCT]
题目传送门 部落冲突 格式难调,体面就不放了. 分析: julao们应该都看得出来就是个$LCT$板子,战争就$cut$,结束就$link$,询问就$find$.没了... 太久没打$LCT$,然后发 ...
- luogu题解 P3950部落冲突--树链剖分
题目链接 https://www.luogu.org/problemnew/show/P3950 分析 大佬都用LCT,我太弱只会树链剖分 一个很裸的维护边权树链剖分题.按照套路,对于一条边\(< ...
- 【Luogu】P3950部落冲突(树链剖分)
题目链接 状态奇差无比,sbt都能错一遍. 不动笔光想没有想到怎么做,画图之后发现一个很明显的性质…… 那就是两个开战的部落,其中一个是另一个的父亲. 所以在儿子那里加个权值.查询的时候树链剖分查询链 ...
- [题解] 洛谷P3950 部落冲突
传送门 拿到题目,一看 裸LCT (其实是我懒得打,splay又臭又长) 首先,这道题的意思就是删掉一些边 所以常规操作 点权转边权 之后对于战争操作,在对应的边上+1 对于和平操作,在对应的边上-1 ...
- 【刷题】洛谷 P3950 部落冲突
题目背景 在一个叫做Travian的世界里,生活着各个大大小小的部落.其中最为强大的是罗马.高卢和日耳曼.他们之间为了争夺资源和土地,进行了无数次的战斗.期间诞生了众多家喻户晓的英雄人物,也留下了许多 ...
随机推荐
- [LeetCode] 367. Valid Perfect Square_Easy tag:Math
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- python使用cx_Oracle在Linux和Windows下的一点差异
1. 主要是线程方面的差异. Windows下,把cx_Oracle.connect(connectedId)得到的handle传给定时器线程,主线程和和定时器可以用同一个handle. 但Linux ...
- python的赋值,深拷贝和浅拷贝的区别
原文地址https://www.cnblogs.com/xueli/p/4952063.html 赋值:a = [1,2,3,["a","b"]] b=a,那 ...
- Visual Studio 2015 开发Android Cordova出现unsupported major minor version 52.0错误的解决方法
JDK版本的问题,需要JDK1.8版本,安装!VS2015做如下设置, 工具->选项->用于Apache Cordoba的工具->环境变量替代->JAVA_HOME设为1.8:
- opencv之模糊处理
初学OpenCV的开发者很容易被OpenCV中各种滤波方法所困扰,不知道到底该用哪里一个来做滤波.表面原因看起来是因为OpenCV中各种滤波方式实在是太多太杂, 其背后原因是对各种滤波方法的应用场景认 ...
- is_readable() 函数检查指定的文件是否可读。
定义和用法 is_readable() 函数判断指定文件名是否可读. 语法 is_readable(file) 参数 描述 file 必需.规定要检查的文件. 说明 如果由 file 指定的文件或目录 ...
- Exception in Spark
1: Exception in thread "main" org.apache.spark.SparkException: org.apache.spark.streaming. ...
- Springboot杂七杂八
1 加载配置文件 private static ResourceBundle resb = ResourceBundle.getBundle("serverInfo"); 然后在r ...
- 【转】推荐4个不错的Python自动化测试框架
之前,开发团队接手一个项目并开始开发时,除了项目模块的实际开发之外,他们不得不为这个项目构建一个自动化测试框架.一个测试框架应该具有最佳的测试用例.假设(assumptions).脚本和技术来运行每一 ...
- 即时通讯(I)
网络通讯三要素: 网络七层协议划分: 网络五层协议的划分: 要记网络层的5层协议,可以把它想像为一枚洋葱.学过计算机网络的,看到这个网络协议的套接字,大概就会明白了!它是一层一层的进行包裹的,然后交由 ...