hdu5967
看到合肥赛区的题目都是泪啊,期末考完了来补几道
公正来说,这道题我考场确实写不出来,因为我的lct模板不够完美……
我在学习lct的时候不知道为什么代码里加边、删边都是用了一个makeroot的操作
这样我的lct就只能维护无根树而不能维护有根树
但事实上,lct是完全可以维护像这道题有向基环外向树一类有根树
那么怎么加边删边呢?
加边x和x的父亲y:只要先access(x) 再把x所属的Auxiliary Tree的Path parent更改y即可(即splay(x),fa[x]=y)
删除x和x的父亲y:先access(x),splay(x),这时断开Auxiliary Tree上根与左子树的连接即可
当然如果涉及到换根的操作,那还是要用makeroot的,这样lct就能维护有根树了
这道题,我们就是维护森林,成环的边不加(根如果有后继先不加),当遇到修改后继的时候再考虑根的后继是否可以加入,查询即找根是否有后继即可
#include<bits/stdc++.h> using namespace std;
int son[][],a[],fa[];
int n,m,pre;
bool rev[],can[];
bool root(int x)
{
return (son[fa[x]][]!=x&&son[fa [x]][]!=x);
} void rot(int x,int w)
{
int y=fa[x];
if (!root(y))
{
if (son[fa[y]][]==y) son[fa [y]][]=x;
else son[fa[y]][]=x;
}
fa[x]=fa[y];
son[y][w^]=son[x][w];
if (son[x][w]) fa[son[x][w]]=y;
son[x][w]=y; fa[y]=x;
} void splay(int x)
{
while (!root(x))
{
int y=fa[x];
if (root(y))
{
if (son[y][]==x) rot(x,); else rot(x,);
}
else {
if (son[fa[y]][]==y)
{
if (son[y][]==x) rot (y,); else rot(x,);
rot(x,);
}
else {
if (son[y][]==x) rot (y,); else rot(x,);
rot(x,);
}
}
}
} void access(int x)
{
int y=;
while (x)
{
splay(x);
son[x][]=y;
y=x; x=fa[x];
}
} int ask(int x)
{
access(x);
splay(x);
while (son[x][]) x=son[x][];
return x;
} void link(int x,int y)
{
access(x);
splay(x);
fa[x]=y;
} void cut(int x)
{
access(x);
splay(x);
int y=son[x][];
if (!y) return;
fa[y]=son[x][]=;
} void change(int x,int y)
{
if (y==a[x]) return;
int rt=ask(x);
if (can[x]) cut(x);
if (x!=rt&&a[rt]&&ask(a[rt])!=ask (rt))
{
link(rt,a[rt]);
can[rt]=;
}
if (!y) can[x]=;
else {
if (ask(x)!=ask(y))
{
link(x,y);
can[x]=;
}
else can[x]=;
}
a[x]=y;
} int main()
{
scanf("%d%d",&n,&m);
for (int i=; i<=n; i++)
{
scanf("%d",&a[i]);
can[i]=;
if (a[i])
{
if (ask(a[i])!=ask(i))
{
fa[i]=a[i];
can[i]=;
}
else can[i]=;
}
}
int ch,x,y;
for (int i=; i<=m; i++)
{
scanf("%d",&ch);
if (ch==)
{
scanf("%d%d",&x,&y);
change(x,y);
}
else {
scanf("%d",&x);
y=ask(x);
printf("%d\n",a[y]?-:y);
}
}
}
hdu5967的更多相关文章
- hdu5967数学找规律+逆元
Detachment Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- LCT专题练习
[bzoj2049]洞穴勘测 http://www.cnblogs.com/Sdchr/p/6188628.html 小结 (1)LCT可以方便维护树的连通性,但是图的连通性的维护貌似很麻烦. [bz ...
随机推荐
- [LeetCode] 56. Merge Intervals(vector sort)
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0 ...
- Nginx 学习笔记之安装篇
在windows下安装Nginx其实非常简单,只需如下几个步骤: 1. 在Nginx官网下载相应版本的安装程序,上面有最新版.稳定版等各种版本,正式运营的项目建议下载最新的稳定版 2.将下载后的压缩包 ...
- JavaScript实现键盘操作页面跳转
对于使用笔记本的同学来说,鼠标操作比较费劲,键盘操作比较方便,下面是一段JavaScript写的,用键盘来实现页面跳转.把location后面的改成你要跳转的地址即可,示例是用方向键实现日志页面的前一 ...
- [Leetcode] Merge two sorted lists 合并两已排序的链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- 【CF MEMSQL 3.0 B. Lazy Security Guard】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- oracle数据库导入导出方法
Oracle Database 10g以后引入了最新的数据泵(Data Dump)技术,使DBA或开发人员可以将数据库元数据(对象定义)和数据快速移动到另一个oracle数据库中. 数据泵导出导入(E ...
- Educational Codeforces Round 54 (Rated for Div. 2) ABCD
A. Minimizing the String time limit per test 1 second memory limit per test 256 megabytes Descriptio ...
- PropertiesConfiguration的用法
PropertiesConfiguration是一个配置文件的加载工具类,封装了从配置文件里获取值并转化为基本数据类型的方法. 使用org.apache.commons.configuration2中 ...
- 移动端list布局,左边固定,右边自适应
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- js 内置对象属性及方法
1.Date 属性(1): constructor 所建立对象的函数参考 prototype 能够为对象加入的属性和方法 方法(43): getDay() 返回一周 ...