F - Ubiquitous Religions

文章目录

  • F - Ubiquitous Religions
  • 题目描述:
  • Input:
  • Output:
  • Sample Input:
  • Sample Output:
  • 题目给的提示:
  • 题目大意:
  • 思路分析:
  • 代码块:

题目描述:

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:

Sample Output:

题目给的提示:

题目大意:

学校调查学生中有多少个宗教群体,但是不好意思直接去问(这有什么不好意思的)吐槽一下,所以他们想到一个间接问法,如果A同学信佛教,问B同学是不是和A同学是一样的宗教,然后依次类推,问C同学。
最后得出有多少个宗教群体。当输入为0 0的时候,就退出程序。

思路分析:

这道题是很典型的查并行,直接将每一个同学创一个父子集,如果遇到相同的,例如A与B,就将B改成A的父子集,依次类推,这里可以右变化也可以左变化,右变化就是A改成B的父子集。

代码块:

#include<stdio.h>
int f[50005];
int lemon2(int b);
void lemon(int b)//为每一个单位(同学)创造父子集
{int c;for(c=1;c<=b;c++){f[c]=c;}
}
void lemon1(int d,int e)//开始改变子集
{int t1,t2;t1=lemon2(d);//返回d的父子集 t2=lemon2(e);//返回e的父子集 if(t1!=t2)//如果他们不相同,就改变他们其中一者的父子集,这里是左变化。 {f[t2]=t1;}
}
int lemon2(int b)//寻找父子集
{if(f[b]==b)//如果等于本身,说明还未改变父子集 {return f[b];}return lemon2(f[b]);//如果不等于本身,说明已经改变,继续递归找出父子集
}
int main()
{int a,b,c,d,e,g=1;while(scanf("%d %d",&b,&c)!=EOF){int num=0;if(b==0 && c==0)return 0;lemon(b);for(a=0;a<c;a++){scanf("%d %d",&d,&e);lemon1(d,e);}for(a=1;a<=b;a++)//划分子集群体。 {if(f[a]==a)num++;    }printf("Case %d: %d\n",g++,num);}
}

F - Ubiquitous Religions相关推荐

  1. Ubiquitous Religions (并查集)

    Ubiquitous Religions POJ - 2524 当今世界有许多不同的宗教,很难跟踪它们.你有兴趣找出你大学里有多少不同的宗教学生相信. 你知道你的大学里有 n 学生 (0 < n ...

  2. 【并查集】POJ 2524 Ubiquitous Religions

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

  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(无处不在的宗教)解题报告(并查集)

    目录 题目描述 思路分析 今天没有考试,那就再刷点题吧(难道就不怕c语言挂科吗?).因为昨天写了道并查集,所以今天再来一道,还是有所收获的. 题目描述 题目:https://vjudge.net/pr ...

  5. poj 2524 Ubiquitous Religions (并查集)

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

  6. POJ2524——宗教(Ubiquitous Religions)【图论,并查集】

    正题 题目链接: http://poj.org/problem?id=2524 大意 有n个学生,告诉你哪两个学生的宗教相等,求校园里有多少个宗教. 解题思路 并查集链接就好了 代码 #include ...

  7. SSL-ZYC POJ 2524 Ubiquitous Religions

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

  8. NKU 专题一 题解

    A - Flip Game 总的情况数只有2^16次方种,显然直接bfs就可以了 1 #include<iostream> 2 #include<queue> 3 #inclu ...

  9. 并查集入门+初级专题训练

    介绍   摘自罗勇军,郭卫斌的<算法竞赛入门到进阶>上的说明:   并查集(Disjoint Set)是一种非常精巧而且食用的数据结构,它主要用于处理一些不相交集合的合并问题.经典的例子有 ...

  10. ACM比赛经验、刷题记录及模板库总结(更新中)

    前言 本文所提及的部分题目代码,可以在我的Github上找到 第一部分 经验分享及感受 第二部分 刷题记录 一.基础算法&程序语言 //strlen()函数的复杂度是O(n)要小心 //截取字 ...

最新文章

  1. SAP托管在Github上的ABAP编程规范
  2. pytorch 图像分割的交并比_级联多对抗的LAPGAN(二)pytorch实现
  3. 小程序 - 学习笔记
  4. [蓝桥杯][2019年第十届真题c/c++B组]后缀表达式(解释sum -= 2*a[i])
  5. 观电台大神弹吉他有感
  6. python数据序列题库_Python题库系列分享一(17道)
  7. mysql事务四个特性_事务的四个特性
  8. java 单元测试 异步_java - 如何使用CountdownLatch对异步代码进行单元测试同步 - 堆栈内存溢出...
  9. stvd使用中的一些问题
  10. 计算机考试多少个小时,考驾照科一电脑刷几个小时
  11. android 远吗编译 刷机 小米,小米10/小米10 Pro系统源代码现已开源,能刷机才是为发烧而生...
  12. 1790D Matryoshkas
  13. 数据库的范式,第一、二、三、四、五范式、BC范式,为什么分不清
  14. 华为 BGP协议基础配置与总结
  15. Git远程仓库配置SSH(以github为例)
  16. 通讯录——java课程设计
  17. 锐捷交换机系统版本升级11.X平台
  18. 机器学习领域 几种距离度量方法【3】
  19. Java字符串转数组,数组转字符串
  20. 树莓派系列-5-4B配置关闭休眠、去掉滚动代码,彩虹瓶,增加开机动画

热门文章

  1. 【C语言】通讯录管理系统
  2. linux css压缩工具下载,推荐15个最好用的JavaScript代码压缩工具
  3. 构造非支配解集(Python)
  4. Linux回到桌面快捷键
  5. R语言中dim函数_R语言入门:函数介绍(3)—— %gt;%
  6. kafka seek方法
  7. 谈谈架构 -- architect
  8. Windows 10系统【之】内置应用
  9. Deep Mind用AlphaZero开发国际象棋新规则-3!
  10. 雷军的博客分享- 这局棋,我站在人工智能这边