POJ1611——The Suspects
来源:互联网 发布:tensorflow lrelu 编辑:程序博客网 时间:2024/06/03 11:17
标准的并查集应用,需要路径压缩。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>using namespace std;const int maxN=30001;int fa[maxN],rank[maxN];void init(int n){ for(int i=0;i<n;i++) { fa[i]=i; rank[i]=1; }}int find_father(int x){ return fa[x]==x?x:find_father(fa[x]);}void Merge(int x,int y){ int ra=find_father(x); int rb=find_father(y); if(ra==rb) return; if(rank[ra]>rank[rb]) { fa[rb]=ra; rank[ra]+=rank[rb]; } else { fa[ra]=rb; rank[rb]+=rank[ra]; }}int Query(int x){ int r=find_father(x); return rank[r];}int main(){ int n,m; //freopen("stdin.txt","r",stdin); while(scanf("%d %d",&n,&m) && (n+m)) { int k; int i,j; int first,next; init(n); for(i=0;i<m;i++) { scanf("%d %d",&k,&first); for(j=1;j<k;j++) { scanf(" %d",&next); Merge(first,next); } } printf("%d\n",Query(0)); } return 0;}
0 0
- POJ1611——The Suspects
- POJ1611——The Suspects
- poj1611——The Suspects
- POJ1611——The Suspects 并查集基础
- poj1611——The Suspects(并查集)
- The Suspects POJ1611
- POJ1611:The Suspects
- POJ1611 The Suspects
- poj1611 The Suspects
- POJ1611 The Suspects
- POJ1611:The Suspects
- POJ1611-The Suspects-ACM
- POJ1611---The Suspects
- poj1611 - The Suspects
- poj1611 The Suspects
- 【POJ1611】The Suspects
- poj1611 The Suspects
- POJ1611 The Suspects
- OC基础9:预处理程序
- 关于Tomcat的点点滴滴(体系架构、处理http请求的过程、安装和配置、目录结构、设置压缩和对中文文件名的支持、以及Catalina这个名字的由来……等)
- parsing restarts here after previous syntax error错误
- java中 map的循环遍历方式
- glew, glee与 gl glu glut glx glext的区别和关系
- POJ1611——The Suspects
- 链表的相关操作
- C2_分支结构
- jquery ajax实现省市二级联动
- JavaScript实现对象数组按不同字段排序
- codeforce 7A. Kalevitch and Chess(粉刷)
- 12步轻松搞定python装饰器
- CSAPP LAB————二进制炸弹(bomblab)
- 欢迎使用CSDN-markdown编辑器