Hdu 5967 小R与手机
2016-12-04 11:06
344 查看
LCT模板题
如果某一次加边会成环,我们可以保证这个点一定是某一个根,在根上记录一下后继
每一次切断某条边的之后同时查询根的是否有后继,后继是否可以连接
具体见代码
如果某一次加边会成环,我们可以保证这个点一定是某一个根,在根上记录一下后继
每一次切断某条边的之后同时查询根的是否有后继,后继是否可以连接
具体见代码
#include<bits/stdc++.h> using namespace std; const int maxn = 212345; struct Node{ int fa,son[2]; int vedg; void init(){ fa = son[0] = son[1] = 0; vedg = 0; } }node[maxn]; int ori(int st){ if(node[st].fa < 0) return 0; return st == node[node[st].fa].son[1]; } void setc(int st,int sn,int d){ if(st > 0) node[st].son[d] = sn; if(sn > 0) node[sn].fa = st; } void zg(int x){ int st = node[x].fa; int p = node[st].fa; int d = ori(x) , dst=ori(st); setc(st,node[x].son[d^1],d); setc(x,st,d^1); setc(p,x,dst); } void splay(int x){ #define f(x) (node[x].fa) while(f(x) > 0) { if(f(f(x)) <= 0) zg(x); else{ if(ori(x) == ori(f(x))) zg(f(x)); else zg(x); zg(x); } } } void access(int x){ splay(x); while(node[x].fa < 0){ int fa = -node[x].fa; splay(fa); node[node[fa].son[1]].fa *= -1; setc(fa,x,1); splay(x); } } int root(int x){ access(x); while(node[x].son[0]) { x = node[x].son[0]; } splay(x); return x; } void link(int st,int x){ access(x); node[x].fa = -st; access(x); } void cut(int x){ access(x); node[node[x].son[0]].fa = 0; node[x].son[0] = 0; } void Link(int st,int x){ int rot = root(x); if(rot == x) node[rot].vedg = 0; else cut(x); int rv = node[rot].vedg; if(rv && root(rv) != rot){ link(rv,rot); node[rot].vedg = 0; } if(root(st) == x) node[x].vedg = st; else link(st,x); } int main(){ int n,m; scanf("%d %d",&n,&m); for(int i = 0 ; i <= n ; i ++) node[i].init(); int x; for(int i = 1; i <= n; i ++){ scanf("%d",&x); if(x) Link(x,i); } int ord,st; while(m--){ scanf("%d %d",&ord,&x); if(ord == 2){ x = root(x); printf("%d\n",node[x].vedg == 0 ? x : -1); } else{ scanf("%d",&st); Link(st,x); } } return 0; }
相关文章推荐
- HDU 5967 小R与手机 (LCT)
- HDU-5967 小R与手机(LCT)
- hdu 5967 小R与手机(LCT裸题)
- HDU 5967 小R与手机(动态树)
- HDU 5967 小R与手机 CDQ分治+回滚并查集
- HDU 5967 小R与手机(Link Cut Tree 基环树森林)
- HDU 2081 手机短号
- HDU 2081 手机短号
- hdu-(1298手机相关 dfs+字典树)
- HDU 2081 -- 手机短号
- HDU 2081 手机短号
- hdu 2081 手机短号(水)
- HDU 2081 手机短号
- HDU 2081 手机短号
- HDU 5641 King's Phone 手机解锁 (模拟)
- HDU 2081 手机短号
- HDOJ 2526 HDU 2526 浪漫手机 ACM 2526 IN HDU
- hdu 2526 浪漫手机
- hdu-(1298手机相关 dfs+字典树)
- HDU 5641 国王的手机 king's phone