基于并查集的一道简单题目
Ubiquitous Religions
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 22334   Accepted: 10996

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.

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 本题要计算一个小组中最多有多少种不同的信仰,只需用小组的人数减去已有小组内人数的总数,再加上小组的数目,就得到了结果。
如果两个节点不在一个小组,则要合并他们,用MaxNum-2+1,MaxNum减去小组内两个人,还要加1的小组数目,所以只需要在合并两个不同节点时用MaxNum--即可。 代码:
 #include <iostream>
using namespace std; int father[], num[];
int count = ;
int MaxNum; void MakeSet(int x)
{
father[x] = x;
num[x] = ;
} int FindSet(int x)
{
if(x != father[x])
father[x] = FindSet(father[x]);
return father[x];
} void Union(int x, int y)
{
int GrandX = FindSet(x);
int GrandY = FindSet(y); if(GrandX == GrandY)
return;
if(num[GrandX] < num[GrandY])
{
father[GrandX] = GrandY;
num[GrandY] += num[GrandX];
}
else
{
father[GrandY] = GrandX;
num[GrandX] += num[GrandY];
}
MaxNum--;
} int main()
{
int n, m;
int Case = ; while((cin >> n, cin >> m) && !(m== && n==))
{
MaxNum = n;
for(int i = ; i < n; i++)
MakeSet(i);
for(int i = ; i < m; i++)
{
int x, y;
cin >> x >> y;
Union(x, y);
}
cout << "Case " << ++Case << ": " << MaxNum << endl;
}
}

poj2524 解题报告的更多相关文章

  1. CH Round #56 - 国庆节欢乐赛解题报告

    最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树 ...

  2. 二模13day1解题报告

    二模13day1解题报告 T1.发射站(station) N个发射站,每个发射站有高度hi,发射信号强度vi,每个发射站的信号只会被左和右第一个比他高的收到.现在求收到信号最强的发射站. 我用了时间复 ...

  3. BZOJ 1051 最受欢迎的牛 解题报告

    题目直接摆在这里! 1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4438  Solved: 2353[S ...

  4. 习题:codevs 2822 爱在心中 解题报告

    这次的解题报告是有关tarjan算法的一道思维量比较大的题目(真的是原创文章,希望管理员不要再把文章移出首页). 这道题蒟蒻以前做过,但是今天由于要复习tarjan算法,于是就看到codevs分类强联 ...

  5. 习题:codevs 1035 火车停留解题报告

    本蒟蒻又来写解题报告了.这次的题目是codevs 1035 火车停留. 题目大意就是给m个火车的到达时间.停留时间和车载货物的价值,车站有n个车道,而火车停留一次车站就会从车载货物价值中获得1%的利润 ...

  6. 习题: codevs 2492 上帝造题的七分钟2 解题报告

    这道题是受到大犇MagHSK的启发我才得以想出来的,蒟蒻觉得自己的代码跟MagHSK大犇的代码完全比不上,所以这里蒟蒻就套用了MagHSK大犇的代码(大家可以关注下我的博客,友情链接就是大犇MagHS ...

  7. 习题:codevs 1519 过路费 解题报告

    今天拿了这道题目练练手,感觉自己代码能力又增强了不少: 我的思路跟别人可能不一样. 首先我们很容易就能看出,我们需要的边就是最小生成树算法kruskal算法求出来的边,其余的边都可以删掉,于是就有了这 ...

  8. NOIP2016提高组解题报告

    NOIP2016提高组解题报告 更正:NOIP day1 T2天天爱跑步 解题思路见代码. NOIP2016代码整合

  9. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

随机推荐

  1. LeetCode 73. Set Matrix Zeros(矩阵赋零)

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click ...

  2. SQL语句查询表中的所有约束

    select * from sysobjects where parent_obj in(select id from sysobjects where name='表名') 或者 exec sp_h ...

  3. linux学习(一)认识、安装Linux

    一.什么是Linux linux是一种操作系统,我们用的android和ios就是分别是linux操作系统和类unix操作系统. linux也是我们经常说的服务器.我们看的网站,游戏,app背后都是服 ...

  4. HQL语法

    HQL:Hibernate Query Language HQL是完全面向对象的查询语言,因此可以支持继承和多态等特征. $下面介绍HQL语句的语法 1.from子句 from Person 表明从P ...

  5. 获取 修改 CSS 样式

    内联(style里的)样式 element.style.color element.style.getPropertyValue("color")   非内联样式 window.g ...

  6. Python爬虫入门:综述

    大家好哈,最近博主在学习Python,学习期间也遇到一些问题,获得了一些经验,在此将自己的学习系统地整理下来,如果大家有兴趣学习爬虫的话,可以将这些文章作为参考,也欢迎大家一共分享学习经验. Pyth ...

  7. Linux上MySQL的安装

    a.检测是否安装了mysql rpm  -qa | grep mysql b.卸载系统自带的mysql rpm -e --nodeps 卸载的包 rpm -e --nodeps mysql-libs- ...

  8. wx:for修改样式

    在获取文字识别数据之后,对数据进行wx:for循环加了边框如图效果: 需求:点击不同边框获取不同文字,再次点击取消选中:选中背景为#999: <view wx:for="{{img_d ...

  9. 《Github入门与实践》读书笔记 蟲咋先生的追求之旅(上)

    <Github入门与实践>作者: [日] 大塚弘记 译者:支鹏浩/刘斌   简介 本书从Git的基本知识和操作方法入手,详细介绍了GitHub的各种功能,GitHub与其他工具或服务的协作 ...

  10. canvas画布实现手写签名效果

    最近项目中涉及到移动端手写签名的功能需求,将实现代码记录于此,供小伙伴们参考指摘哦~ HTML代码: <!--手写区--> <div class="mSign_signMa ...