HDU 5967 小R与手机(动态树)
【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=5967
【题目大意】
给出一张图,每个点仅连一条有向边,或者不连,
要求查询在可更改有向边的情况每个点通过有向边最终能到的终点,
如果是个环则输出-1
【题解】
我们用lct维护,同时在每棵树根结点的位置标记环,
因为出现环一定在根节点,否则的话只是换直接父节点而不成环,
对于换直接父节点的操作,我们看该树的根是否有标记,
如果有则需要在cutf之后重新构建这棵树的根,
如果换的父节点为自己的子节点,那么就另成一棵树并标记环,
【代码】
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=200010;
namespace Link_Cut_Tree{
int f[N],son[N][2],val[N],sum[N],tmp[N],Xor[N];bool rev[N];
void Initialize(){
memset(f,0,sizeof(f));
memset(son,0,sizeof(son));
memset(val,0,sizeof(val));
memset(rev,0,sizeof(rev));
}
bool isroot(int x){return !f[x]||son[f[x]][0]!=x&&son[f[x]][1]!=x;}
void rev1(int x){if(!x)return;swap(son[x][0],son[x][1]);rev[x]^=1;}
void pb(int x){if(rev[x])rev1(son[x][0]),rev1(son[x][1]),rev[x]=0;}
void rotate(int x){
int y=f[x],w=son[y][1]==x;
son[y][w]=son[x][w^1];
if(son[x][w^1])f[son[x][w^1]]=y;
if(f[y]){
int z=f[y];
if(son[z][0]==y)son[z][0]=x;else if(son[z][1]==y)son[z][1]=x;
}f[x]=f[y];f[y]=x;son[x][w^1]=y;
}
void splay(int x){
int s=1,i=x,y;tmp[1]=i;
while(!isroot(i))tmp[++s]=i=f[i];
while(s)pb(tmp[s--]);
while(!isroot(x)){
y=f[x];
if(!isroot(y)){if((son[f[y]][0]==y)^(son[y][0]==x))rotate(x);else rotate(y);}
rotate(x);
}
}
void access(int x){for(int y=0;x;y=x,x=f[x])splay(x),son[x][1]=y;}
// 查询x所在的树的根
int root(int x){access(x);splay(x);while(son[x][0])x=son[x][0];return x;}
// 使x成为根
void makeroot(int x){access(x);splay(x);rev1(x);}
// 将x和y所属树合并
void link(int x,int y){makeroot(x);f[x]=y;access(x);}
// 将x和其父节点分开
void cutf(int x){access(x);splay(x);f[son[x][0]]=0;son[x][0]=0;}
// 将边x-y切断
void cut(int x,int y){makeroot(x);cutf(y);}
// 查询x到y的链和
int ask(int x,int y){makeroot(x);access(y);splay(y);return sum[y];}
// 计算x到y的xor和
int xorsum(int x,int y){makeroot(x);access(y);splay(y);return Xor[y];}
// 查询节点到根的距离
int query(int x){access(x);splay(x);return sum[x];}
// 将x为下标的值改为y
int change(int x,int y){makeroot(x);val[x]=y;}
// 将x的父亲改为y
int changef(int x,int y){cutf(x);f[x]=y;}
}
int mark[N];
void Link(int x,int y){
using namespace Link_Cut_Tree;
int rx=root(x);
if(rx==x)mark[x]=0;
else cutf(x);
if(mark[rx]&&root(mark[rx])!=rx){
changef(rx,mark[rx]);
mark[rx]=0;
}if(root(y)==x)mark[x]=y;
else changef(x,y);
}
int n,m,op,x;
int main(){
scanf("%d%d",&n,&m);
using namespace Link_Cut_Tree;
Initialize();
for(int i=1;i<=n;i++){
scanf("%d",&x);
if(x)Link(i,x);
}
while(m--){
scanf("%d%d",&op,&x);
if(op==2){
x=root(x);
printf("%d\n",mark[x]==0?x:-1);
}else{
int y;
scanf("%d",&y);
Link(x,y);
}
}return 0;
}
HDU 5967 小R与手机(动态树)的更多相关文章
- HDU 4010 Query on The Trees (动态树)(Link-Cut-Tree)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意; 先给你一棵树,有 \(4\) 种操作: 1.如果 \(x\) 和 \(y\) 不在同一 ...
- 小R与手机
Description 小R有n部手机,为了便于管理,他对一些手机设置了"呼叫转移"的功能. 具体来说,第 i(1≤i≤n) 部手机有个参数 ai(0≤ai≤n,ai≠i) .若 ...
- HDU 4010 Query on The Trees(动态树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意:一棵树,四种操作: (1)若x和y不在一棵树上,将x和y连边: (2)若x和y在一棵树上, ...
- hdu 4521 小明序列(线段树,DP思想)
题意: ①首先定义S为一个有序序列,S={ A1 , A2 , A3 , ... , An },n为元素个数 : ②然后定义Sub为S中取出的一个子序列,Sub={ Ai1 , Ai2 , Ai3 , ...
- Hdu 4010-Query on The Trees LCT,动态树
Query on The Trees Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Othe ...
- HDU 2475 BOX 动态树 Link-Cut Tree
Box Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) [Problem De ...
- hdu 5398 动态树LCT
GCD Tree Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- hdu 5002 (动态树lct)
Tree Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submi ...
- hdu 5314 动态树
Happy King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Tot ...
随机推荐
- 【HNOI】 c tree-dp
[题目描述]给定一个n个节点的树,每个节点有两个属性值a[i],b[i],我们可以在树中选取一个连通块G,这个连通块的值为(Σa[x])(Σb[x]) x∈G,求所有连通块的值的和,输出答案对1000 ...
- IE浏览器Bug总结
每每在网上搜索IE浏览器Bug时,总是骂声一片,特别是前端工程师,每天都要面对,IE浏览器特别是IE6,存在很多Bug,对Web标准的支持也拖后腿,但不可否认,IE浏览器是曾经的霸主,它的贡献也是巨大 ...
- deepin 快捷键
从此脱离鼠标
- 双内网渗透代理之reGeorg+Proxifier
由于这个工具第一次体验感觉还不错,很稳定.因此在这记录一下reGeorg+Proxifier的配置及其使用. 下载地址 :https://github.com/sensepost/reGeorg.gi ...
- Python脚本 - 常用单位转换
测试系统为:Centos 6.7 Python版本为: 3.6.4 脚本功能:常用单位的转换,这里用内存来模拟 import pstuil def bytes2human(n): symbols = ...
- 《gdb调试之基础篇》
<gdb调试之基础篇> http://blog.csdn.net/miss_acha/article/details/42346543
- Python 微信公众号发送消息
1. 公众号测试地址 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index 2. ...
- QT 中怎样使得控件与 界面等比例变化
转自:https://github.com/exoticknight/blog-post/blob/master/python-with-Qt-application-development/pyth ...
- AC日记——「SCOI2015」国旗计划 LiBreOJ 2007
#2007. 「SCOI2015」国旗计划 思路: 跪烂Claris 代码: #include <cstdio> #include <algorithm> #define ma ...
- AC日记——自然数和分解 codevs 2549
自然数和分解 思路: 水题: 代码: #include <bits/stdc++.h> using namespace std; ][]; int main() { cin>> ...