UOJ #207. 共价大爷游长沙
2018-03-27 09:34
489 查看
UOJ #207. 共价大爷游长沙
题目传送门PS:Ab.Ever也做了这道题,有兴趣可以去看看(^。^ )/。
【问题描述】
给出一棵n个节点的树,有m种操作:1 x y u v,把xy之间的边删掉,加入uv的边,保证操作过后,仍是一棵树。
2 x y,表示x到y的路径加入集合S中。
3 x,删除集合S中第x个加入的路径。
4 x y,询问S中的所有路径是否全都经过边xy。
【解题思路】
加边,删边,仍是树的结构?看起来像是LCT,其实就是LCT(动态树)
①维护子树信息
若S中的所有路径全都经过边uv。那么,当以u为根节点时,所有路径的端点都恰好有一个在v的子树中。
(若有两个或没有,则证明路径不与边uv有交集。自个画图证明吧)
那怎么判断所有路径的端点都恰好有一个在某个子树中呢?
观察一下异或的运算。
A^B^B=A
A^0=A
可以给每一条路径随机一个权值。对路径的两个端点异或上路径的权值。
用动态树维护子树权值异或和,每次询问对应子树内的权值异或和是否是当前所有路径的权值异或和。
【代码】
#include<bits/stdc++.h> #define imax(a,b) ((a>b)?(a):(b)) #define imin(a,b) ((a<b)?(a):(b)) #define pa t[x].fa #define lc t[x].ch[0] #define rc t[x].ch[1] using namespace std; typedef long long ll; const int N=100500; int n,m,cnt,tot,now; struct pai { int x,y,val; } s[N<<2]; inline void read(int &x) { x=0; char ch=getchar(); int f=1; for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-1; for(; isdigit(ch);ch=getchar()) x=(x<<3)+(x<<1)+ch-'0'; x*=f; } int ran() { return ((rand()<<10)+rand()); } 4000 namespace lct { struct meow { int ch[2],fa,rev,val,sval; } t ; inline int wh(int x) { return t[pa].ch[1]==x; } inline bool isr(int x) { return t[pa].ch[0]!=x && t[pa].ch[1]!=x; } inline void rever(int x) { t[x].rev^=1; swap(lc,rc); } inline void paint(int x,int v) { t[x].val^=v; } inline void pushdn(int x) { if(t[x].rev) { if(lc) rever(lc); if(rc) rever(rc); t[x].rev=0; } } inline void pd(int x) { if(!isr(x)) pd(pa); pushdn(x); } inline void update(int x) { t[x].sval=t[x].val; if(lc) t[x].sval^=t[lc].sval; if(rc) t[x].sval^=t[rc].sval; } inline void rotate(int x) { int f=pa,g=t[pa].fa,c=wh(x); if(!isr(f)) t[g].ch[wh(f)]=x; t[x].fa=g; t[f].ch[c]=t[x].ch[c^1]; t[t[f].ch[c]].fa=f; t[x].ch[c^1]=f; t[f].fa=x; update(f); update(x); } inline void splay(int x) { pd(x); for(;!isr(x);rotate(x)) if(!isr(pa)) rotate(wh(x)^wh(pa)?x:pa); } inline void access(int x) { for(int y=0;x;y=x,x=pa) { splay(x); t[x].val^=t[rc].sval^t[y].sval; rc=y; update(x); } } inline void maker(int x) { access(x); splay(x); rever(x); } inline void link(int x,int y) { maker(x); maker(y); pa=y; t[y].val^=t[x].sval; update(y); } inline void split(int x,int y) { maker(x); access(y); splay(y); } inline void cut(int x,int y) { split(x,y); pa=t[y].ch[0]=0; update(y); } } using namespace lct; void rep() { int a,b,c,d; read(a); read(b); read(c); read(d); if(a>b) swap(a,b); if(c>d) swap(c,d); cut(a,b); link(c,d); } void add(int x,int y) { s[++tot]=(pai){x,y,ran()}; maker(x); paint(x,s[tot].val); update(x); maker(y); paint(y,s[tot].val); update(y); now^=s[tot].val; } void del(int id) { int x=s[id].x,y=s[id].y; maker(x); paint(x,s[id].val); update(x); maker(y); paint(y,s[id].val); update(y); now^=s[id].val; } void que(int x,int y) { if(x>y) swap(x,y); split(x,y); printf("%s\n",(t[x].sval==now)?"YES":"NO"); } int main() { srand(233); read(n); read(n); read(m); cnt=0; for(int i=1;i<n;++i) { int a,b; read(a); read(b); if(a>b) swap(a,b); link(a,b); } tot=now=0; for(int i=1;i<=m;++i) { int ty,a,b; read(ty); if(ty==1) rep(); else if(ty==2) read(a),read(b),add(a,b); else if(ty==3) read(a),del(a); else if(ty==4) read(a),read(b),que(a,b); } return 0; }
②维护路径的异或和
询问的是一条边uv,是否被所有的路径包含。那是否可以把标记打在边上呢?
每次加入或删除集合S的路径xy,将x到y的路径全部异或上路径的权值。
那删除树上的边xy,加入新边uv。则将边xy的权值记下来,记为val。cut掉该边,link新边之后,再将路径xy异或上val。
最后,对于询问,直接查询边的权值是否等于当前所有路径的权值异或和。
图文解释:
【代码】
#include<bits/stdc++.h> #define imax(a,b) ((a>b)?(a):(b)) #define imin(a,b) ((a<b)?(a):(b)) #define pa t[x].fa #define lc t[x].ch[0] #define rc t[x].ch[1] using namespace std; typedef long long ll; const int N=200500; int n,m,cnt,tot,now; map<int,int> eid ; struct pai { int x,y,val; } s ; inline void read(int &x) { x=0; char ch=getchar(); int f=1; for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-1; for(; isdigit(ch);ch=getchar()) x=(x<<3)+(x<<1)+ch-'0'; x*=f; } int ran() { return ((rand()<<10)+rand()); } namespace lct { struct meow { int ch[2],fa,rev,val,tag; } t ; inline int wh(int x) { return t[pa].ch[1]==x; } inline bool isr(int x) { return t[pa].ch[0]!=x && t[pa].ch[1]!=x; } inline void rever(int x) { t[x].rev^=1; swap(lc,rc); } inline void paint(int x,int v) { t[x].val^=v; t[x].tag^=v; } inline void pushdn(int x) { if(t[x].rev) { if(lc) rever(lc); if(rc) rever(rc); t[x].rev=0; } if(t[x].tag) { if(lc) paint(lc,t[x].tag); if(rc) paint(rc,t[x].tag); t[x].tag=0; } } inline void pd(int x) { if(!isr(x)) pd(pa); pushdn(x); } inline void update(int x) {} inline void rotate(int x) { int f=pa,g=t[pa].fa,c=wh(x); if(!isr(f)) t[g].ch[wh(f)]=x; t[x].fa=g; t[f].ch[c]=t[x].ch[c^1]; t[t[f].ch[c]].fa=f; t[x].ch[c^1]=f; t[f].fa=x; update(f); update(x); } inline void splay(int x) { pd(x); for(;!isr(x);rotate(x)) if(!isr(pa)) rotate(wh(x)^wh(pa)?x:pa); } inline void access(int x) { for(int y=0;x;y=x,x=pa) splay(x),rc=y,update(x); } inline void maker(int x) { access(x); splay(x); rever(x); } inline void link(int x,int y) { maker(x); pa=y; } inline void split(int x,int y) { maker(x); access(y); splay(y); } inline void cut(int x,int y) { split(x,y); pa=t[y].ch[0]=0; update(y); } } using namespace lct; void rep() { int a,b,c,d; read(a); read(b); read(c); read(d); if(a>b) swap(a,b); if(c>d) swap(c,d); int id=eid[a][b]; pd(id); int val=t[id].val; cut(id,a); cut(id,b); t[id].fa=t[id].ch[0]=t[id].ch[1]=t[id].rev=t[id].tag=t[id].val=0; eid[c][d]=id; link(c,id); link(d,id); split(a,b); paint(b,val); } void add(int x,int y) { s[++tot]=(pai){x,y,ran()}; split(x,y); paint(y,s[tot].val); now^=s[tot].val; } void del(int id) { int x=s[id].x,y=s[id].y; split(x,y); paint(y,s[id].val); now^=s[id].val; } void que(int x,int y) { if(x>y) swap(x,y); int id=eid[x][y]; pd(id); printf("%s\n",t[id].val==now?"YES":"NO"); } int main() { srand(233); read(n); read(n); read(m); cnt=n; for(int i=1;i<n;++i) { int a,b; read(a); read(b); if(a>b) swap(a,b); eid[a][b]=++cnt; link(a,cnt); link(b,cnt); } tot=now=0; for(int i=1;i<=m;++i) { int ty,a,b; read(ty); if(ty==1) rep(); else if(ty==2) read(a),read(b),add(a,b); else if(ty==3) read(a),del(a); else if(ty==4) read(a),read(b),que(a,b); } return 0; }
相关文章推荐
- UOJ #207. 共价大爷游长沙 [lct 异或]
- UOJ #207. 共价大爷游长沙
- uoj#207. 共价大爷游长沙
- [UOJ207]共价大爷游长沙(随机,动态树维护子树和)
- UOJ #207. 共价大爷游长沙(不用维护子树的LCT)
- UOJ207 共价大爷游长沙
- UOJ 207 共价大爷游长沙
- [UOJ207]共价大爷游长沙
- [UOJ207]共价大爷游长沙
- 【uoj#207】共价大爷游长沙 随机化+LCT维护子树信息
- UOJ#207 共价大爷游长沙
- 【uoj207】 共价大爷游长沙
- uoj#207 共价大爷游长沙
- [UOJ#207]共价大爷游长沙
- 【UOJ207】共价大爷游长沙(Link-Cut Tree,随机化)
- 共价大爷游长沙
- 【UOJ207】共价大爷游长沙(Link-Cut Tree,随机化)
- UOJ207:共价大爷游长沙
- UOJ207:共价大爷游长沙