POJ 2524 (简答并查集) Ubiquitous Religions

题意:有编号为1到n的学生,然后有m组调查,每组调查中有a和b,表示该两个学生有同样的宗教信仰,问最多有多少种不同的宗教信仰

简单并查集

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6
 7 const int maxn = 50000 + 10;
 8 int parent[maxn], n, m;
 9
10 int GetParent(int x)
11 {
12     return parent[x] == x ? x : parent[x] = GetParent(parent[x]);
13 }
14
15 void Init(void)
16 {
17     for(int i = 0; i <= n; ++i)
18         parent[i] = i;
19 }
20
21 int main(void)
22 {
23     #ifdef LOCAL
24         freopen("2524in.txt", "r", stdin);
25     #endif
26
27     int kase = 0;
28     while(scanf("%d%d", &n, &m) == 2 && (m + n))
29     {
30         Init();
31         for(int i = 0; i < m; ++i)
32         {
33             int a, b;
34             scanf("%d%d", &a, &b);
35             int pa = GetParent(a);
36             int pb = GetParent(b);
37             if(pa != pb)
38                 parent[pa] = pb;
39         }
40         int sum = 0;
41         for(int i = 1; i <= n; ++i)
42             if(parent[i] == i)
43                 ++sum;
44         printf("Case %d: %d\n", ++kase, sum);
45     }
46     return 0;
47 }

代码君

时间: 2024-08-06 11:55:30

POJ 2524 (简答并查集) Ubiquitous Religions的相关文章

poj 1456 Supermarket (贪心+并查集)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int fa[10010]; struct node { int p; int d; }; struct node a[10010]; bool cmp(node a1,node a2)//利润从大到小 { return a1.p>a2.p; } int find(int x) { if(fa[x]

[ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21586   Accepted: 10456 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

poj 2513 Colored Sticks 并查集 字典树 欧拉回路判断

点击打开链接题目链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 30273   Accepted: 8002 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sti

poj 1733 Parity game 并查集 离散化

点击打开链接题目链接 Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6249   Accepted: 2413 Description Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You cho

POJ 1182 食物链【并查集】

题目链接:http://poj.org/problem?id=1182 此题利用并查集解决. 对于每只动物i创建3个元素i-A,i-B,i-C,并用这3*N个元素建立并查集. 1·i-x表示"i属于种类x" 2·并查集你的每一组表示组内所有元素代表的情况同时发生或不发生. 对于每一条信息,只需要按照下列操作即可: 1.第一种:x,y同类,合并x-A和y-A.x-B和y-B.x-C和y-C. 2.第二种:x吃y,,,合并x-A和y-B.x-B和y-C.x-C和y-A. 当然,在合并之前,

poj 1182:食物链(并查集,食物链问题)

食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44168   Accepted: 12878 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是"1 X Y",表示X和Y是同

POJ 1182 食物链(并查集拆点)

[题目链接] http://poj.org/problem?id=1182 [题目大意] 草原上有三种物种,分别为A,B,C A吃B,B吃C,C吃A. 1 x y表示x和y是同类,2 x y表示x吃y 问给出的信息有几条是和前面相违背的 [题解] 之前这道题是用加权并查集做的,做的有些晕晕乎乎,现在换了种思路做就清晰很多了 将每个点拆点,比如x拆为,x-A,x-B,x-C 表示x属于A类,x属于B类,和x属于C类, 如果y和x属于同类,那么合并x-A和y-A,x-B和y-B,x-C和y-C 如果

POJ 1417 True Liars 并查集+背包

题目链接:http://poj.org/problem?id=1417 解题思路:比较容易想到的是并查集,然后把第三组数据测试一下之后发现这并不是简单的并查集,而是需要合并之后然后判断的.并且鉴于题目要求输出数据,因此还要记录数据,可以说是非常有意思的题目. 首先,如果a b yes,那么a与b一定都是圣人或者恶人:反之,如果a b no,那么两者一个圣人,一个恶人.因此可以将所有元素分为若干个集合,每个集合中放两个小集合,表示两种不一样的人,当然并不知道到底哪个里面是圣人,恶人. 另一个比较棘

poj 2236 Wireless Network (并查集)

链接:http://poj.org/problem?id=2236 题意: 有一个计算机网络,n台计算机全部坏了,给你两种操作: 1.O x 修复第x台计算机 2.S x,y 判断两台计算机是否联通 联通的条件: 两台都修复好了,求距离小于d 思路: 数据很小,我们可以没修复一台就直接枚举已经修复的计算机找到距离小于d的,放到一个并查集里,查询的话直接查询是否再同一个并查集里就好了 实现代码: //while(scanf("\n%c", &ope) != EOF) #inclu