POJ2524并查集水题
Description
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
Output
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 题意:学校有n个同学,每个同学有且只有一个信仰,给出m对有同一信仰的同学,问存在多少种不同的信仰?
输入0 0时结束程序。 思路:初始有n位同学,有n个集合,有n个信仰,对于给出的m对同学:若原来不知他们有同一信仰,则合并他们各自所在集合`信仰数减一。
#include <cstdio>
using namespace std;
#define max_n 50050
int n,m;
int par[max_n];
void init(int n)
{
for(int i=1;i<=n;i++)
{
par[i]=i;
}
}
int find(int x)
{
if(par[x]==x)
return x;
else
{
return par[x]=find(par[x]);
}
}
bool same(int x,int y)
{
return find(x)==find(y);
}
void unit(int x,int y)
{
int fx=find(x);
int fy=find(y);
par[fx]=fy;
}
int main()
{
int d=1;
while(scanf("%d%d",&n,&m))
{
if(n==0&&m==0)
break;
init(n);
int ans=n;
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
if(!same(x,y))
{
ans--;
unit(x,y);
}
}
printf("Case %d: %d\n",d,ans);
d++;
}
return 0;
}
POJ2524并查集水题的更多相关文章
- poj2524(并查集水题)
题目链接:http://poj.org/problem?id=2524 题目大意:学校共有n个同学,告诉你m对同学信仰同一宗教,问这个学校学生信仰宗教的数目最多为多少. 例: Sample Input ...
- Brain Network (easy)(并查集水题)
G - Brain Network (easy) Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- 【PAT-并查集-水题】L2-007-家庭房产
L2-007. 家庭房产 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下 ...
- HDU1863(Kruskal+并查集水题)
https://cn.vjudge.net/problem/HDU-1863 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可). ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- POJ1611 && POJ2524 并查集入门
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 28293 Accepted: 13787 De ...
- 【HDU1231】How Many Tables(并查集基础题)
什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...
- poj1182 食物链(并查集 好题)
https://vjudge.net/problem/POJ-1182 并查集经典题 对于每只动物创建3个元素,x, x+N, x+2*N(分别表示x属于A类,B类和C类). 把两个元素放在一个组代表 ...
- PAT甲级 并查集 相关题_C++题解
并查集 PAT (Advanced Level) Practice 并查集 相关题 <算法笔记> 重点摘要 1034 Head of a Gang (30) 1107 Social Clu ...
随机推荐
- nginx 入门配置
这个星期公司的定期分享内容是Nginx,于是就要写作业了. 一.动静分离 1.下载Windows 版本的Nginx,解压,放到C盘下.进入目录,然后按然shift键右键,打开命令行,输入: start ...
- python3基础之整数常用的方法整理
希望对大家学习或者使用python3能具有一定的参考价值. __abs__ #返回一个数的绝对值 >>> num3=-22 >>> num3.__abs__ ...
- [SinGuLaRiTy] 最短路计算代码库
[SinGuLaRiTy-1002] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. Dijkstra: 题目描述 有向图的单源点最短路问题( ...
- 设置ssh只允许用户从指定的IP登陆
假设 我们公司的固定IP是 183.21.89.249 连接上我们自己进行管理的服务器 然后编辑ssh的配置文件默认 /etc/ssh/sshd_config 在文件最后面另起一行添加 ...
- hiveF 函数解析时间问题
#!/bin/bashsource /etc/profileupdatetime=`date --date='0 days ago' +"%Y-%m-%d %H:%M:%S"`ec ...
- Android IPC机制全解析<一>
概要 多进程概念及多进程常见注意事项 IPC基础:Android序列化和Binder 跨进程常见的几种通信方式:Bundle通过Intent传递数据,文件共享,ContentProvider,基于Bi ...
- DataTables源码分析(一)
DataTables源码分析 写在前面 作为一名常年奋战在java世界中的程序猿,当我接触到现在所谓的前端技术时,内心其实是崩溃的.因为,前端的技术给我的第一个感觉就是"乱",这里 ...
- webx request注入单例增强实现
由于在spring中request对象的scope限制导致了request对象无法直接注入到单例bean中,所以webx对其进行了增强实现,达到单例注入的目的. 实现原理大致如下: 1 启动时注册全局 ...
- JDK8-十大新特性-附demo
JDK原计划17年上半年就发版,但未成功发版.才发现JDK8的特性还没总结过,特此总结. 一.十大特性. 1.Lambda表达式 2.Stream函数式操作流元素集合 3.接口新增:默认方法与静态方法 ...
- 【Linux配置】vim配置文件内容
vim的配置 文件:~/.vimrc 在自己的家目录中的.vimrc文件进行编辑配置 设置如下: set nu "序号 set tabstop= "tab键的大小 set show ...