首页 > 代码库 > 【可持久化数组】【rope】bzoj3673 bzoj3674 可持久化并查集 by zky
【可持久化数组】【rope】bzoj3673 bzoj3674 可持久化并查集 by zky
rope教程:http://blog.csdn.net/iamzky/article/details/38348653
Code(bzoj3673):
1 #include<cstdio> 2 #include<ext/rope> 3 using namespace std; 4 using namespace __gnu_cxx; 5 rope<int> *fa[20001]; 6 int a,b,n,m,A[20001],op; 7 int Root(int num,int x) 8 { 9 if(fa[num]->at(x)==x)10 return x;11 int rt=Root(num,fa[num]->at(x));12 // if(rt==fa[num]->at(x))若不加这两行,bzoj3674会爆内存。13 14 // return rt;15 fa[num]->replace(x,rt);16 return rt;17 }18 void Union(int num,int x,int y)19 {20 int U=Root(num,x),V=Root(num,y);21 fa[num]->replace(V,U);22 }23 int main()24 {25 scanf("%d%d",&n,&m);26 for(int i=1;i<=n;i++)27 A[i]=i;28 fa[0]=new rope<int>(A,A+n+1);29 for(int i=1;i<=m;i++)30 {31 fa[i]=new rope<int>(*fa[i-1]);32 scanf("%d",&op);33 if(op==1){scanf("%d%d",&a,&b);Union(i,a,b);}34 else if(op==2){scanf("%d",&a);fa[i]=fa[a];}35 else{scanf("%d%d",&a,&b);printf("%d\n",Root(i,a)==Root(i,b));}36 }37 return 0;38 }
【可持久化数组】【rope】bzoj3673 bzoj3674 可持久化并查集 by zky
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。