[关闭]
@Codewzs 2019-04-14T08:28:44.000000Z 字数 282 阅读 736

并查集

C++模板


  1. struct bcj
  2. {
  3. int father[100010];
  4. void start(int n)//初始化
  5. {for(int i=0;i<=n;i++)father[i]=i;}
  6. int find(int x)//寻找父亲
  7. {if(father[x]!=x)father[x]=find(father[x]);return father[x];}
  8. void unionn(int x,int y)//合并
  9. {x=find(x);y=find(y);if(x!=y)father[y]=x;}
  10. };

使用方法:

  1. bcj uf;
  2. uf.start(...);
  3. uf.unionn(...,...);
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注