POJ 1611 The Suspects 并查集 Union Find
本题也是个标准的并查集题解。
操作完并查集之后,就是要找和0节点在同一个集合的元素有多少。
注意这个操作,须要先找到0的父母节点。然后查找有多少个节点的额父母节点和0的父母节点同样。
这个时候须要对每一个节点使用find parent操作。由于最后状态的时候,节点的parent不一定是本集合的根节点。
#include <stdio.h> const int MAX_N = 30001;
struct SubSet
{
int p, rank;
}sub[MAX_N]; int N, M; void initSub()
{
for (int i = 0; i < N; i++)
{
sub[i].p = i;
sub[i].rank = 0;
}
} int find(int x)
{
if (x != sub[x].p) sub[x].p = find(sub[x].p);
return sub[x].p;
} void unionTwo(int x, int y)
{
int xroot = find(x);
int yroot = find(y);
if (sub[xroot].rank < sub[yroot].rank) sub[xroot].p = yroot;
else
{
if (sub[xroot].rank == sub[yroot].rank) sub[xroot].rank++;
sub[yroot].p = xroot;
}
} int main()
{
int a, b, k;
while (scanf("%d %d", &N, &M) && (N || M))
{
initSub();
for (int i = 0; i < M; i++)
{
scanf("%d", &k);
if (k > 0) scanf("%d", &a);
for (int j = 1; j < k; j++)
{
scanf("%d", &b);
unionTwo(a, b);
}
}
int sus = 1, p = find(0);
for (int i = 1; i < N; i++)
{
if (find(i) == p) sus++;
}
printf("%d\n", sus);
}
return 0;
}
POJ 1611 The Suspects 并查集 Union Find的更多相关文章
- 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 并查集
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(并查集)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21598 Accepted: 10461 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 ...
随机推荐
- luogu P1516 青蛙的约会(线性同余方程扩展欧几里德)
题意 题解 做了这道题,发现扩欧快忘了. 根据题意可以很快地列出线性同余方程. 设跳了k次 x+mkΞy+nk(mod l) (m-n)kΞ-(x-y)(mod l) 然后化一下 (m-n)k+(x- ...
- [NOIP2015普及组]推销员
题目:洛谷P2672.codevs5126.Vijos P1977 题目大意:有个推销员要去推销,要你求他推销1~n户人家分别最多花多少“疲劳值”.具体见题目. 解题思路:如果用$O(n^2)$做的话 ...
- BZOJ 5254 [Fjwc2018]红绿灯 (线段树)
题目大意:一个wly从家走到学校要经过n个红绿灯,绿灯持续时间是$g$,红灯是$r$,所有红绿灯同时变红变绿,交通规则和现实中一样,不能抢红灯,两个红绿灯之间道路的长度是$di$,一共$Q$个询问,求 ...
- java 获取config 配置文件
static ResourceBundle PropertiesUtil = ResourceBundle.getBundle("config"); public static S ...
- 紫书 习题8-4 UVa 11491 (贪心)
题意:给你一个数, 要求删去一些数字, 使得剩下的数字最大. 这道题用贪心解决. 大家想一想, 两个数比较大小, 肯定先比较第一位的数,然后依次比较第二位,以此类推. 既然我们要保证最后的数字最大, ...
- How to enable wire logging for a java HttpURLConnection traffic?
https://stackoverflow.com/questions/1445919/how-to-enable-wire-logging-for-a-java-httpurlconnection- ...
- 国庆 day 2 下午
最大值(max) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK有一本书,上面有很多有趣的OI问题.今天LYK看到了这么一道题目: 这里有一个长度为n的 ...
- Java数据库訪问小结
</pre>1.JDBC訪问方法</p><p></p><p>DBHelper类訪问数据库.Dao类写数据訪问,View类进行应用,初学实例图 ...
- xcode5. 安装cocos2d-x 学习中。。。
找了一些帖子 没搞出来,后来找到原因了 如今的cocos2d版本号在xcode.5上 没右模版了. 用命令行 来运行.看了官方的文档.最终攻克了--- 对于自己解决的问题都会感到点兴奋. .. ...
- MooseFS源代码分析(二)
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...