SOJ 1824 并查集
来源:互联网 发布:java subtract 编辑:程序博客网 时间:2024/06/07 10:52
并查集是对不想交集合上的操作,应用很多,思想比较简单,主要是2个操作,寻找父结点,合并两个集合
#include<stdio.h>#include<string.h>int a[30010];int fa[30010];int p[30010];int find(int x){ if(x!=fa[x]) fa[x]=find(fa[x]); return fa[x];}void uion(int x,int y){ int fx=find(x); int fy=find(y); if(fx!=fy) fa[fx]=fy;}int main(){ int n,m; while(scanf("%d%d",&n,&m)==2&&(n||m)) { int i,j; int k,x; for(i=0;i<n;i++) { fa[i]=i; p[i]=0; } for(i=1;i<=m;i++) { scanf("%d",&k); for(j=0;j<k;j++) { scanf("%d",&x); p[x]=i; } for(j=0;j<n;j++) { if(p[j]==p[x]) { uion(j,x); } } } int count=0; for(i=0;i<n;i++) { if(find(i)==find(0)) count++; } printf("%d\n",count); } return 0;}
- SOJ 1824 并查集
- SOJ 1824 并查集
- soj 1824 并查集
- 【并查集】SOJ并查集篇
- 并查集 poj soj 1824: The Suspects
- SOJ-1953(填充数组,并查集)
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- 并查集
- 并查集总结
- objective-c中对象复制的几种情况
- 树叶1:欢迎进入软件构建的世界
- Do you think the high school should teach music and art as other basic science
- 黑马程序员_day07 面向对象(继承,抽象类,接口)
- What would you do when you are free?
- SOJ 1824 并查集
- Story about Apple
- 扩展Ext的Combobox实现多选下拉列表
- 黑马程序员_day08 面向对象(多态,object类和equals()和toString())
- 用隐喻理解软件开发
- 不同数据库中substring的差异
- Knowledge and experience
- 出现频率最高的笔试题strcpy写法及注意事项
- 计算几何题目列表——第二章