bzoj 2959: 长跑 lct+并查集
2017-09-13 21:10
316 查看
题意
某校开展了同学们喜闻乐见的阳光长跑活动。为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动。一时间操场上熙熙攘攘,摩肩接踵,盛况空前。为了让同学们更好地监督自己,学校推行了刷卡机制。
学校中有n个地点,用1到n的整数表示,每个地点设有若干个刷卡机。
有以下三类事件:
1、修建了一条连接A地点和B地点的跑道。
2、A点的刷卡机台数变为了B。
3、进行了一次长跑。问一个同学从A出发,最后到达B最多可以刷卡多少次。具体的要求如下:
当同学到达一个地点时,他可以在这里的每一台刷卡机上都刷卡。但每台刷卡机只能刷卡一次,即使多次到达同一地点也不能多次刷卡。
为了安全起见,每条跑道都需要设定一个方向,这条跑道只能按照这个方向单向通行。最多的刷卡次数即为在任意设定跑道方向,按照任意路径从A地点到B地点能刷卡的最多次数。
n<=150000
分析
显然如果有一个边双,那么显然如果我经过这个边双的任意一个点,则我必然可以得到整个边双的价值。那么我们只要资瓷加边和缩点操作就好了。这显然是可以用lct+并查集维护的。
也就是说,如果加入边的两个点不连通,则加入该边,否则就把这条路径上的点用并查集直接缩在一起就好了。
然后每次找父亲的时候要先把父亲变为其代表元。
代码
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<stack> using namespace std; const int N=150005; int n,m,f ,bel ,a ; struct tree{int rev,s,val,l,r,fa;}t ; stack<int> sta; int read() { int x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int findf(int x) { if (f[x]==x) return x; else return f[x]=findf(f[x]); } int findbel(int x) { if (bel[x]==x) return x; else return bel[x]=findbel(bel[x]); } bool is_root(int x) { return !t[x].fa||x!=t[t[x].fa].l&&x!=t[t[x].fa].r; } void updata(int d) { t[d].s=t[t[d].l].s+t[t[d].r].s+t[d].val; } void rttl(int x) { int y=t[x].r; t[x].fa=findbel(t[x].fa); t[x].r=t[y].l;t[t[y].l].fa=x; if (x==t[t[x].fa].l) t[t[x].fa].l=y; else if (x==t[t[x].fa].r) t[t[x].fa].r=y; t[y].fa=t[x].fa; t[y].l=x;t[x].fa=y; updata(x);updata(y); } void rttr(int x) { int y=findbel(t[x].l); t[x].fa=findbel(t[x].fa); t[x].l=t[y].r;t[t[y].r].fa=x; if (x==t[t[x].fa].l) t[t[x].fa].l=y; else if (x==t[t[x].fa].r) t[t[x].fa].r=y; t[y].fa=t[x].fa; t[y].r=x;t[x].fa=y; updata(x);updata(y); } void pushdown(int x) { if (!t[x].rev) return; swap(t[x].l,t[x].r);t[x].rev^=1; if (t[x].l) t[t[x].l].rev^=1; if (t[x].r) t[t[x].r].rev^=1; } void remove(int x) { while (!is_root(x)) sta.push(x),t[x].fa=findbel(t[x].fa),x=t[x].fa; pushdown(x); while (!sta.empty()) x=sta.top(),sta.pop(),pushdown(x); } void splay(int x) { remove(x); while (!is_root(x)) { int p=findbel(t[x].fa),g=findbel(t[p].fa); if (is_root(p)) { if (x==t[p].l) rttr(p); else rttl(p); break; } if (x==t[p].l) if (p==t[g].l) rttr(g),rttr(p); else rttr(p),rttl(g); else if (p==t[g].r) rttl(g),rttl(p); else rttl(p),rttr(g); } } void access(int x) { int p=0; while (x) { t[x].fa=findbel(t[x].fa); splay(x);t[x].r=p; updata(x); p=x;x=t[x].fa; } } void make_root(int x) { access(x);splay(x);t[x].rev^=1; } void link(int x,int y) { make_root(y);splay(y);t[y].fa=x; } void dfs(int x) { if (t[x].l) t[x].l=findbel(t[x].l),dfs(t[x].l),t[x].val+=t[t[x].l].val,bel[t[x].l]=x; if (t[x].r) t[x].r=findbel(t[x].r),dfs(t[x].r),t[x].val+=t[t[x].r].val,bel[t[x].r]=x; } void merge(int x,int y) { if (x==y) return; make_root(x);access(y);splay(x); dfs(x); t[x].l=t[x].r=0; } int main() { n=read();m=read(); for (int i=1;i<=n;i++) t[i].s=t[i].val=a[i]=read(),bel[i]=f[i]=i; while (m--) { int op=read(),x=read(),y=read(); if (op==1) { x=findbel(x);y=findbel(y); if (findf(x)==findf(y)) merge(x,y); else f[findf(x)]=findf(y),link(x,y); } else if (op==2) { int belx=findbel(x); splay(belx);t[belx].val+=y-a[x];a[x]=y;t[belx].s=t[belx].val; } else { x=findbel(x);y=findbel(y); if (findf(x)!=findf(y)) puts("-1"); else make_root(x),access(y),splay(y),printf("%d\n",t[y].s); } } return 0; }
相关文章推荐
- 【BZOJ2959】长跑 (LCT+并查集)
- 【BZOJ2959】长跑 LCT+并查集
- bzoj2959 长跑(LCT+并查集)
- BZOJ 2959: 长跑(LCT+并查集)
- bzoj 2959: 长跑(LCT+并查集)
- [BZOJ2959]长跑(LCT+并查集)
- [BZOJ2959]长跑(lct+并查集)
- 【bzoj2959】长跑【LCT+并查集】
- BZOJ2959: 长跑
- bzoj2959: 长跑
- [BZOJ2959]长跑
- bzoj 2959 长跑(LCT+BCC+并查集)
- [BZOJ2959][清橙1379]长跑-LCT-并查集
- [BZOJ2959]长跑(LCT)
- [BZOJ2959]长跑 LCT+双联通分量+并查集
- Bzoj2959: 长跑
- 【BZOJ2959】长跑(Link-Cut Tree,并查集)
- 【BZOJ2959】长跑(Link-Cut Tree,并查集)
- BZOJ 2959: 长跑 [lct 双连通分量 并查集]
- bzoj 2959: 长跑