poj1611 - The Suspects
来源:互联网 发布:淘宝代购海外直邮真假 编辑:程序博客网 时间:2024/04/28 05:08
裸并查集
题意:n个学生,加入了m个团体,分别编号0到n-1,若一个人感染了,他所在的团队的所有人都会感染,要求多组数据感染的人数,规定0号已感染。
并查集思想:初始化,查,并;此题可以让每组的节点合并到第一个,并用另一个数组son[]统计这个团体中的人数。0所在的连通图的根节点的son[]就是结果
#include <iostream>using namespace std;int n,m,k,f,t,s[30005],son[30005];int find(int x){//找到x的根节点 if(s[x]!=x) return s[x]=find(s[x]); return x;}void un(int a,int b){//将这组其他的人合并到第一个人的根节点的连通图上 int x=find(a); int y=find(b); if(x!=y)//根节点不相同,则合并,让y(自然b也是)的根节点只想x,且x的孩子增加; {s[y]=x; son[x]+=son[y];}}int main(){ while(cin>>n>>m&&n+m) { for(int i=0;i<n;i++) { s[i]=i; son[i]=1; }//初始化 for(int i=0;i<m;i++) { cin>>k; if(k>=1) { cin>>f; } for(int j=1;j<k;j++) { cin>>t; un(f,t); } } int ans=son[find(0)]; cout<<ans<<endl; } return 0;}
0 0
- 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
- The Suspects<poj1611>
- poj1611 -- The Suspects
- POJ1611-The Suspects
- poj1611 - The Suspects
- POJ1611:The Suspects
- 关于LeetCode中Implement strStr()一题的理解
- 动手写一个Android数据加载时界面状态切换的开源库
- {模板}网络流Dinic
- 分布式系统的思考及CAP理论
- 在Windows操作系统中,如何终止占有的8080端口的tomcat进程
- poj1611 - The Suspects
- Mockito mock类部分方法
- android+eclipse+tracker相关
- Hdu 4052 Adding New Machine(给你W*H大小的矩形,其中有N个地区不能使用(给出了这个地区的两个顶点的坐标即(x1,y1)和(x2,y2)),问能下多少个1*M的矩形)
- 积分竞猜系统开发
- java 接口
- solr5.3.1 集成IK中文分词器
- 51Nod-1350-斐波那契表示
- Centos7 安装opencv