Wireless Network (并查集)
来源:互联网 发布:马鞍山网络大学招聘 编辑:程序博客网 时间:2024/06/05 15:20
Ubiquitous Religions
Time limit 10000 ms Memory limit 65536 kB
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.
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.
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.
10 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0
Case 1: 1Case 2: 7
Huge input, scanf is recommended.
题解:给你n,m,然后给你m行a,b,表示a,b有共同的宗教信仰,求最大的宗教数量,(最大为n)……
#include<cstdio>#include<iostream>#include<algorithm>#include<string.h>#include<stdlib.h>#include<time.h>#include<string>#include<math.h>#include<map>#include<queue>#include<stack>#define INF 0x3f3f3f3f#define ll __int64#define For(i,a,b) for(int i=a;i<b;i++)#define sf(a) scanf("%d",&a)#define sfs(a) scanf("%s",a)#define sff(a,b) scanf("%d%d",&a,&b)#define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c)#define pf(a) printf("%d\n",a)#define mem(a,b) memset(a,b,sizeof(a))using namespace std;#define maxn 105#define maxv 10005int pre[50005],x[50005];int find(int x) // 查找根节点{ int s=x; while(s!=pre[s]) // 查找根节点 { s=pre[s]; } int i=x,j; while(pre[i]!=s) //路径压缩,节省查询时间 { j=pre[i]; pre[i]=s; i=j; } return s;}void join(int x,int y) // 判断x,y是否拥有共同的宗教信仰,若有就不管了,若没有,让两人的信仰相同{ int xx=find(x);int yy=find(y); if(xx!=yy) pre[xx]=yy;}int main(){ int n,M,flag=0; while(~sff(n,M)&&(n||M)) { flag++; for(int i=1;i<=n;i++) // 初始化各点的根节点为自己本身 pre[i]=i; int a,b; For(i,0,M) { sff(a,b); join(a,b); // 处理数据 } mem(x,0); for(int i=1;i<=n;i++) { x[find(i)]=1; // 标记根节点 } int s=0; for(int i=1;i<=n;i++) // 记录根节点的数目,即最大宗教的数量 if(x[i]) { s++; } printf("Case %d: ",flag); printf("%d\n",s); }}
0 0
- POJ2236 Wireless Network(并查集)
- POJ2236 Wireless Network(并查集)
- 【hdu】 Wireless Network (并查集)
- poj2236 Wireless Network(并查集)
- POJ Wireless Network (并查集)
- POJ2236 Wireless Network(并查集)
- Wireless Network (并查集)
- POJ2366 Wireless Network(并查集)
- Wireless Network (并查集)
- Wireless Network(并查集)
- POJ2236 Wireless Network(并查集)
- 【并查集】Wireless Network
- 并查集----Wireless Network
- Wireless Network 并查集
- Wireless Network 并查集
- Wireless Network --并查集
- Wireless Network(并查集(裸))
- PKU 2236 Wireless Network(并查集)
- python | gtts 将文字转化为语音内容
- ThreadLocal实现线程范围的共享变量-笔记整理4
- mysql导入导出sql文件
- LINQ To SQL 语法及实例大全
- 利用For循环创建二维数组
- Wireless Network (并查集)
- linux基本语法3
- 福州大学 FOJ有奖月赛-2017年4月(校赛热身赛)(DP)
- 学习博客
- Struts2的工作流程
- 统计学习方法(3)——KNN,KD树及其Python实现
- oracle视图、列转行、前几个月、相差秒数、自增主键
- 【JavaScript】3.语法
- uboot的几种启动方式的配置方法。