The Suspects
来源:互联网 发布:北京网络监管局电话 编辑:程序博客网 时间:2024/05/21 19:30
#include <stdio.h>int stack[300000];int find(int x){ int r,k,j; r=x; while(r!=stack[r]) r=stack[r]; k=x; while(k!=r) { j=stack[k]; stack[k]=r; k=j; } return r;}void merge(int x,int y){ int fx=find(x); int fy=find(y); if(fx!=fy) stack[fx]=fy;}int main(){ int n,m,k,i,a,b,j,fx,fy; while(scanf("%d %d",&n,&m)) { if(n==0&&m==0) break; for(i=0; i<n; i++) stack[i]=i; for(i=0; i<m; i++) { scanf("%d",&k); scanf("%d",&a); for(j=0; j<k-1; j++) { scanf("%d",&b); merge(a,b); } } int c=1; fx=find(0); //先找到编号为0的人的根节点 for(i=1; i<n; i++) { fy=find(i); //不能直接用stack[0]与stack[i]的关系判断它俩的根节点是否为一个,因为有的依然是父节点 if(fx==fy) c++; } printf("%d\n",c); }}
0 0
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- The Suspects
- 《算法导论》笔记(1)排序 含部分习题
- 在Mac系统上安装redis服务
- Android 自定义控件onMeasure()方法详解
- ios8 tableView设置滑动删除时显示多个按钮
- Linux学习之CentOS(二十六)--Linux磁盘管理:LVM逻辑卷的创建及使用
- The Suspects
- Android 网络编程——socket failed: EACES (Permission denied)
- 2015年1月总结和2月份计划
- Android Service完全解析,关于服务你所需知道的一切(上)
- PostgreSQL pg_dump&pg_restore 数据的导出和导入
- MFC小笔记之窗口
- 短信 通讯录备份APP(针对android)
- webview用法
- IO性能最重要的三个指标