Ubiquitous Religions(并查集的简单应用)
来源:互联网 发布:java _version 编辑:程序博客网 时间:2024/05/06 21:27
题目来源:[NWPU][2014][TRN][12]并查集 E 题
http://vjudge.net/contest/view.action?cid=50731#problem/E
作者:npufz
题目:
E - Ubiquitous Religions
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
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 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0
Sample Output
Case 1: 1Case 2: 7
Hint
Huge input, scanf is recommended.
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>using namespace std;int parent[50005];int sum[50005];int getparent(int a){ if(a==parent[a]) return a; int t; t=getparent(parent[a]); parent[a]=t; return parent[a];}int mergege(int a,int b){ int pre1,pre2; pre1=getparent(a); pre2=getparent(b); if(pre1==pre2) return 0; if(sum[pre1]>sum[pre2]) { parent[pre2]=pre1; sum[pre1]+=sum[pre2]; return 0; } parent[pre1]=pre2; sum[pre2]+=sum[pre1]; return 0;}int main(){ int n,cas,i,j,k,cnt=1,stu1,stu2,ma,pre,prea; while(scanf("%d%d",&n,&cas),n!=0&&cas!=0) { for(i=1;i<=n;i++) { parent[i]=i; sum[i]=1; } for(i=0;i<cas;i++) { scanf("%d%d",&stu1,&stu2); mergege(stu1,stu2); } ma=n; bool flag=true; //cout<<"ok"<<endl; for(i=1;i<=n;i++) { //cout<<"ok"<<endl; pre=getparent(i); if(flag) { ma=ma-sum[pre]+1; prea=pre; flag=false; } else { if(pre!=prea) { ma=ma-sum[pre]+1; parent[pre]=prea; } } } printf("Case %d: %d\n",cnt,ma); cnt++; }return 0;}反思:并查集可以把属于同一组的元素合并到一起,并可以通过两个集合的元素的关系,来把两个集合合并到一起
0 0
- Ubiquitous Religions(并查集的简单应用)
- Ubiquitous Religions 简单并查集
- POJ2524 Ubiquitous Religions(简单并查集)
- Ubiquitous Religions (简单并查集)
- 并查集的应用——Ubiquitous Religions
- POJ2524 - Ubiquitous Religions (并查集的基础应用)
- POJ 2524 Ubiquitous Religions(简单的并查集)
- ubiquitous religions 并查集
- Ubiquitous Religions 并查集
- 并查集--Ubiquitous Religions
- 并查集 Ubiquitous Religions
- (并查集)Ubiquitous Religions
- Ubiquitous Religions -- 并查集
- Poj 2524 Ubiquitous Religions[简单并查集]
- POJ 2524 Ubiquitous Religions //简单并查集
- POJ - 2524 Ubiquitous Religions(简单并查集)
- POJ 2524 Ubiquitous Religions(简单并查集)
- Ubiquitous Religions(简单并查集 poj 2524 )
- 如何卸载Wondershare流录音机
- czdsfgsdxcv
- HDU 2026 首字母变大写
- 如何使说唱节奏在七个简单的步骤
- Poj-1961 Period(next数组应用)
- Ubiquitous Religions(并查集的简单应用)
- 为什么要上大学?
- 分支-10. 计算个人所得税
- dsfedfcv
- HDU 2027 统计元音
- error LNK1117 : 选项“SUBSYSTEM:WINDOWS@QMAKE_SUBSYSTEM_SUFFIX@”中的语法错误
- HDU 1054——Strategic Game(树形DP)
- fwefgegege
- 如何以及何时使用一只耳朵唤起9例情况