POJ 1611 The Suspects(并查集,简单)
为什么ACM的题意都这么难懂,就不能说的直白点吗?还能不能好好的一起刷题了?
题意:你需要建一个n的并查集,有m个集合,最后要输出包含0的那个集合的元素的个数。
这是简单并查集应用,所以直接看代码吧!
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int INF=0x3f3f3f3f;
typedef long long ll;
typedef unsigned long long ull;
#define fi first
#define se second
#define prN printf("\n")
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define SIII(N,M,K) scanf("%d%d%d",&(N),&(M),&(K))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++) int father[30005],mrank[30005]; void init(int n)
{
int i;
for (i=0;i<n;i++)
{
father[i]=i;//并查集是从零开始的
mrank[i]=1;//mrank存的是集合中的个数
}
} int find_set(int x)
{
if (x!=father[x])
{
father[x]=find_set(father[x]);
}
return father[x];
} void myUnion(int x,int y)
{
x=find_set(x);
y=find_set(y);
if (x==y) return; if (mrank[x]>=mrank[y])
{
father[y]=x;
mrank[x]+=mrank[y];
}
else
{
father[x]=y;
mrank[y]+=mrank[x];
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("C:\\Users\\Zmy\\Desktop\\in.txt","r",stdin);
// freopen("C:\\Users\\Zmy\\Desktop\\out.txt","w",stdout);
#endif // ONLINE_JUDGE
int n,m;
while(cin>>n>>m,n||m)
{
init(n);
int k,fi,se;
for (int i=0;i<m;i++)
{
cin>>k>>fi;
for (int j=1;j<k;j++)
{
cin>>se;
myUnion(fi,se);
}
}
cout<<mrank[father[0]]<<endl;
} return 0;
}
POJ 1611 The Suspects(并查集,简单)的更多相关文章
- poj 1611 The Suspects(并查集输出集合个数)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- poj 1611 The Suspects 并查集变形题目
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 20596 Accepted: 9998 D ...
- POJ 1611 The Suspects (并查集+数组记录子孙个数 )
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 24134 Accepted: 11787 De ...
- POJ 1611 The Suspects (并查集求数量)
Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...
- POJ 1611 The Suspects 并查集 Union Find
本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...
- poj 1611 The Suspects 并查集
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 30522 Accepted: 14836 De ...
- [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21586 Accepted: 10456 De ...
- 并查集 (poj 1611 The Suspects)
原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...
- [并查集] POJ 1611 The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 35206 Accepted: 17097 De ...
随机推荐
- 新浪代码部署手册 git管理工具
目前新浪云上的应用支持通过Git和SVN来部署代码. Git仓库地址 https://git.sinacloud.com/YOUR_APP_NAME SVN仓库地址 https://svn.sinac ...
- 后台向前台传递entity的list然后构筑combobox的方法(easyui)
遇问题,莫着急,深呼吸. 后台写法:JsonUtil.toJson(new ArrayList<Entity>());此处jsonUtil是已封装的方法,即将entity的list转为js ...
- Python学习(3)——if语句
虽然在之前接触过C.C++.Java等,但是还是觉得Python写出来的好看o(≧v≦)o~,简洁明了! score = raw_input("score:") score=int ...
- 【转】NSString属性什么时候用copy,什么时候用strong?
原文网址:http://www.cocoachina.com/ios/20150512/11805.html 我们在声明一个NSString属性时,对于其内存相关特性,通常有两种选择(基于ARC环境) ...
- JavaScript-闭包注意事项
闭包允许内层函数引用父函数中的变量,但是该变量是最终值
- orzdba的安装与使用
参考:http://code.taobao.org/p/orzdba/src/ http://code.taobao.org/p/myrelay/wiki/index/ http://www.taob ...
- 在easyui datagrid中formatter数据后使用linkbutton
http://ntzrj513.blog.163.com/blog/static/2794561220139245411997/ formatter:function(value,rowData,ro ...
- Cobertura 代码覆盖率测试
Cobertura 是一种开源工具,它通过检测基本的代码,并观察在测试包运行时执行了哪些代码和没有执行哪些代码,来测量测试覆盖率.除了找出未测试到的代码并发现 bug 外,Cobertura 还可以通 ...
- 使用ExpandableListView——当有Group选项展开时,如何正确获取长按的Group选项。
当我们使用ExpandableListView时,实现点击一个GroupView则展开ChidView,那么这个时候,Adapter的大小前后是有变化的. 例如:假设有20个GroupView,每个G ...
- SQL常见的可优化点
# 索引相关 # ################################################### 1. 查询(或更新,删除,可以转换为查询)没有用到索引 这是最基础的步骤,需要 ...