Ubiquitous Religions
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 34293 | Accepted: 16540 |
Description
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.
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.
Source
Alberta Collegiate Programming Contest 2003.10.18
1. 数据解读:传入n,m代表n个人,m组相同信仰的同学
2. 思路:运用并查集,对每个人进行分组(刚开始假设每个人都有自己的信仰,自己各为一组),然后输入数据,判断是否需要合并,如果需要n--,组数减少,所有的数据输入判断完,最后输出n共有几组
3. 代码方法解析:
I.定义find方法,返回组的编号(根节点) ==》并查集(查)
int find(int p)//传入一个人,返回组号(树形结构,最后返回根节点)
{
if(f[p] == p){//如果坐标所存位置的数据等于自身,则代表找到根节点
return p;//返回组号
}
return find(f[p]);//不等于自身,则所存位置记录着他的队友(父结点),递归往回找根节点
};
find方法优化:(返回的同时覆盖现在所存的值,从而减少时间) ==>蓝桥杯 历届试题 国王的烦恼 find方法未优化无法全部AC
int find(int p)//传入一个人,返回组号(树形结构,最后返回根节点)
{
if(f[p] == p){//如果坐标所存位置的数据等于自身,则代表找到根节点
return p;//返回组号
}
return f[p] = find(f[p]);//不等于自身,则所存位置记录着他的队友(父结点),递归往回找根节点
};
II.定义 Union方法,合并相同的组别 ==》并查集( 并)
bool Union(int a,int b){//传入a,b两个人,判断是否需要合并
int fa = find(a);//返回a的组号(根节点)
int fb = find(b);//返回b的组号(根节点)
if(fa == fb){//组号(根节点)编号相同,同一组,不合并,返回false,不需要合并
return false;
}
//如果不一样,把a组的根节点的编号改为b组的根节点编号,实现两
//个组别合并(把a树的根节点志向b树的根节点,实现两棵树的合并)
f[fa] = fb;
return true;//返回true 合并成功
}
4. 画表格分析:
10人分组 第一组数据 第二组数据 第三组数据 第四组数据
(2 3) (4 5) (4 8) (5 8)
f[0] = 0 f[0] = 0 f[0] = 0 f[0] = 0 f[0] = 0
f[1] = 1 f[1] = 1 f[1] = 1 f[1] = 1 f[1] = 1
f[2] = 2 f[2] = 3 f[2] = 3 f[2] = 3 f[2] = 3
f[3] = 3 f[3] = 3 f[3] = 3 f[3] = 3 f[3] = 3
f[4] = 4 f[4] = 4 f[4] = 5 f[4] = 5 f[4] = 5
f[5] = 5 f[5] = 5 f[5] = 5 f[5] = 8 f[5] = 8
f[6] = 6 f[6] = 6 f[6] = 6 f[6] = 6 f[6] = 6
f[7] = 7 f[7] = 7 f[7] = 7 f[7] = 7 f[7] = 7
f[8] = 8 f[8] = 8 f[8] = 8 f[8] =8 f[8] = 8
f[9] = 9 f[9] = 9 f[9] = 9 f[9] = 9 f[9] = 9
组数 n = 10 n = 9 n = 8 n = 7 n= 7
5. 代码实现(已AC):
#include"iostream"
using namespace std;
int n,m,f[50000 + 5];
int find(int p)//传入一个人,返回组号(树形结构,最后返回根节点)
{
if(f[p] == p){//如果坐标所存位置的数据等于自身,则代表找到根节点
return p;//返回组号
}
return find(f[p]);//不等于自身,则所存位置记录着他的队友(父结点),递归往回找根节点
};
bool Union(int a,int b){//传入a,b两个人,判断是否需要合并
int fa = find(a);//返回a的组号(根节点)
int fb = find(b);//返回b的组号(根节点)
if(fa == fb){//组号(根节点)编号相同,同一组,不合并,返回false,不需要合并
return false;
}
//如果不一样,把a组的根节点的编号改为b组的根节点编号,实现两
//个组别合并(把a树的根节点志向b树的根节点,实现两棵树的合并)
f[fa] = fb;
return true;//返回true 合并成功
}
int main(){
int a,b,id = 1;
while(cin >> n >> m){
if(n == 0 && m == 0)break;
for(int i = 0;i < n;i++){//初始化分组
f[i] = i;
}
for(int i = 0;i < m;i++)
{
cin >> a >> b;
if(Union(a,b)){//判断是否需要合并
n --;
}
}
cout << "Case "<<id++<<": " << n << endl;;
}
return 0;
}
6. 数据代入分析:
第四组数据 find(5) == find(8) 根节点相同,同一组
7.总结:题目意思不难,主要是要考点是并查集