目录

  • 题目描述
  • 思路分析

今天没有考试,那就再刷点题吧(难道就不怕c语言挂科吗?)。因为昨天写了道并查集,所以今天再来一道,还是有所收获的。

题目描述

题目:https://vjudge.net/problem/POJ-2524
There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in.
You know that there are n students in your university (0 < n <= 50000). It is infeasible for you to ask every student their religious beliefs. Furthermore, many students are not comfortable expressing their beliefs. One way to avoid these problems is to ask m (0 <= m <= n(n-1)/2) pairs of students and ask them whether they believe in the same religion (e.g. they may know if they both attend the same church). From this data, you may not know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. You may assume that each student subscribes to at most one religion.
Input
The input consists of a number of cases. Each case starts with a line specifying the integers n and m. The next m lines each consists of two integers i and j, specifying that students i and j believe in the same religion. The students are numbered 1 to n. The end of input is specified by a line in which n = m = 0.
Output
For each test case, print on a single line the case number (starting with 1) followed by the maximum number of different religions that the students in the university believe in.
Sample Input

10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0

Sample Output

Case 1: 1
Case 2: 7

Hint
Huge input, scanf is recommended.

翻译:(来自vjudge:clzls)
当今世界有很多不同的宗教,很难通晓他们。你有兴趣找出在你的大学里有多少种不同的宗教信仰。
你知道在你的大学里有n个学生(0 < n <= 50000) 。你无法询问每个学生的宗教信仰。此外,许多学生不想说出他们的信仰。避免这些问题的一个方法是问m(0 <= m <= n(n - 1)/ 2)对学生, 问他们是否信仰相同的宗教( 例如他们可能知道他们两个是否去了相同的教堂) 。在这个数据中,你可能不知道每个人信仰的宗教,但你可以知道校园里最多可能有多少个不同的宗教。假定每个学生最多信仰一个宗教。
Input
有多组数据。对于每组数据:
第一行:两个整数n和m。
以下m行:每行包含两个整数i和j,表示学生i和j信仰相同的宗教。学生编号从1到n。
输入的最后一行中,n = m = 0。
Output
对于每组测试数据,输出一行,输出数据序号( 从1开始) 和大学里不同宗教的最大数量。(参见样例)
Sample Input

10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0

Sample Output

Case 1: 1
Case 2: 7

Hint
输入巨大,推荐使用scanf。

思路分析

这道题很明显是一道查并集。那么,还是按照查并集的老套路:建立数组,令其中的a[i]=i初始化。然后,每读入一组输入,就分别找到这两个输入数据的根节点,选择其中一个为根节点,和另一个根节点相通的点的根节点全部改为新的根节点,达到压缩路径的效果。
但这道题和上一道不一样的是:上一道可以通过一个vis数组查找是否有输出重复的数据(比如 输入了1 2后又输入2 1,则2 1不运行直接跳过)。但是,这里要查重需要建立一个5001x5001的数组,会炸,所以采用另一种方法查重。
在分别找到两个数据的根节点之后,检测他们是不是同一个根节点的。若是,则这组数据是和前面的重复的,直接continue,这样,这道题就不会超时了。
下面,上代码:

#include <stdio.h>int main()
{int cases=0;int n,m;while(~scanf("%d %d",&n,&m)&&n!=0&&m!=0){int students[55000]={0};for(int i=1;i<=n;i++) students[i]=i;for(int k=0;k<m;k++){int i,j;scanf("%d %d",&i,&j);int r1=i;int r2=j;while(students[r1]!=r1) r1=students[r1];while(students[r2]!=r2) r2=students[r2];if(r1==r2) continue;students[r1]=r2;for(int l=1;l<=n;l++){if(students[l]==r1) students[l]=r2;}}int counter=0;for(int i=1;i<=n;i++) if(students[i]==i) counter++;printf("Case %d: %d\n",++cases,counter);}return 0;
}

POJ-2524 Ubiquitous Religions(无处不在的宗教)解题报告(并查集)相关推荐

  1. 【并查集】POJ 2524 Ubiquitous Religions

    POJ 2524 Ubiquitous Religions 就是看集合的数量是多少,将所有节点的祖先结点放进set,输出size就行了 #include <iostream> #inclu ...

  2. SSL-ZYC POJ 2524 Ubiquitous Religions

    题目大意: 你知道你的大学里有N个学生.每个人都信仰宗教,你向每个学生请教他们的宗教信仰是不可行的.避免这些问题的一种方法是问M对学生,问他们是否相信同一宗教.从这些数据中,你可能不知道每个人都相信什 ...

  3. poj 2524 Ubiquitous Religions (简单并查集)

    题目链接:http://poj.org/problem?id=2524 There are so many different religions in the world today that it ...

  4. poj 2524 Ubiquitous Religions (并查集)

    题目:http://poj.org/problem?id=2524 题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教.问学校里最多可能有多少个宗教. 也就是给定一 ...

  5. [JZOJ3385] [NOIP2013模拟] 黑魔法师之门 解题报告(并查集)

    Description 经过了16个工作日的紧张忙碌,未来的人类终于收集到了足够的能源.然而在与Violet星球的战争中,由于Z副官的愚蠢,地球的领袖applepi被邪恶的黑魔法师Vani囚禁在了Vi ...

  6. 【POJ】1308 Is It A Tree?((并查集 + set)or (map))

    http://poj.org/problem?id=1308 这个题数组开到200就可以了,但题目中貌似没有说呢? 读入每一对顶点,看看他们是否在同一个集合中,如果是的话,肯定成环,不是一棵树. 用s ...

  7. 【POJ - 1456】Supermarket (贪心,优先队列 或并查集)

    题干: A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod s ...

  8. 【POJ - 2377】Bad Cowtractors (最大生成树,并查集)

    题干: Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1 ...

  9. poj 3728 The merchant// lca(倍增实现) + dp || tarjan+并查集路径上dp

    poj 3728 The merchant// lca(倍增实现) + dp Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: ...

  10. 1526:宗教信仰——简单并查集

    描述 世界上有许多宗教,你感兴趣的是你学校里的同学信仰多少种宗教. 你的学校有n名学生(0 < n <= 50000),你不太可能询问每个人的宗教信仰,因为他们不太愿意透露.但是当你同时找 ...

最新文章

  1. DCMTK:dcmseg模块的辅助功能
  2. 【AngularJS学习笔记】Java Script use strict 严格模式
  3. 伺服驱动器生产文件_直流伺服系统的组成和控制原理详解
  4. 分享Silverlight/WPF/Windows Phone/HTML5一周学习导读(1月9日-1月15日)
  5. 【概率论】深度学习必懂的13种概率分布
  6. Linux系统编程之进程间通信
  7. 苹果七绕过基带激活2020_【最新!】绕过苹果ID可激活
  8. js中运算符的优先级
  9. 计算机音乐《讲真的》,他凭借一首《讲真的》红遍大江南北,希望他不忘初心纯粹做音乐...
  10. 【C# 练习】C# 程序设计实用教程(第2版)黄兴荣
  11. Redis中的事务和三特性
  12. android开发找不到模拟器(PANIC: Could not open:)解决办法
  13. 手机定向root,指定APP获取root权限
  14. 安卓APP注册登录+Tomcat服务器搭建+MySQL数据库建立+加密传输+servlet后端内容编写及部署到Tomcat服务器
  15. python数据分析用到的库_用python进行数据分析的五个最常用库
  16. (1)定义圆Circle类,包含radius半径属性,求面积方法,求周长方法,返返回圆对象的详细信息的方法(2)在测试类中创建长度为5的Circle[]数组,用来装5个圆对象
  17. TensorFlow 2 和 Keras 高级深度学习:11~13
  18. 安装 ubuntu 18.04登录界面卡死解决
  19. 小知识(1):关于端口的复用及重映射
  20. 《路人甲》 很适合喜欢动脑的人!

热门文章

  1. ashx 获取上传的文件_使用jQuery Post从.ashx获取文件
  2. Rust学习笔记(15)——struct、Option和Box组合应用实现单向链表之三
  3. Mybatis-plus LambdaQueryWrapper 模糊查询 like方法使用记录
  4. 俩个装满水的8斤桶和一个空的3斤的桶分给4个人每人4斤
  5. 暴雪吸金如土,魔兽年总收入达10亿美元
  6. 正则表达式中圆括号的作用
  7. Puppeteer 操作
  8. 【关于创业】我要说1
  9. uniapp去掉页面导航左上角的返回按钮
  10. SpringBoot接收Xml格式参数并转换为POJO对象