当前位置:网站首页>Pku-2524-ubiquitous relations (parallel search template)
Pku-2524-ubiquitous relations (parallel search template)
2022-07-28 06:51:00 【__ Simon】
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 32067 | Accepted: 15530 |
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
Hint
Source
Alberta Collegiate Programming Contest 2003.10.18
And look up the template .
#include <stdio.h>
#include <string.h>
#define MAX 50000
int pre[MAX+10];
int t[MAX+10];//t Used to mark the root node of independent blocks
int find(int x){// Find the root node
int r=x;
int i=x,j;
while(pre[r]!=r){
r=pre[r];
}
as
while(pre[i]!=r){// Path compression
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
void mix(int x,int y){// Connect two sets
int fx=find(x),fy=find(y);
if(fx!=fy)
pre[fx]=fy;
}
int main(){
int n,m,i;
int x,y,s,ans=1;
while(scanf("%d %d",&n,&m),n+m){
s=0;
for(i=0;i<n;i++)
pre[i]=i;
for(i=0;i<m;i++){
scanf("%d %d",&x,&y);
mix(x,y);
}
memset(t,0,sizeof(t));
for(i=0;i<n;i++){// Mark the root node
t[find(i)]=1;
}
for(i=0;i<n;i++)
if(t[i]==1)
s++;
printf("Case %d: %d\n",ans++,s);
}
return 0;
}边栏推荐
猜你喜欢
![[pta-- use queues to solve the problem of monkeys choosing kings]](/img/54/94359fb3557ac07f7786ecf61a5409.png)
[pta-- use queues to solve the problem of monkeys choosing kings]

redis缓存设计与性能优化
![[explain in detail how to realize Sanzi chess step by step]](/img/17/68ef51ec2be0c86019461116ecaa82.png)
[explain in detail how to realize Sanzi chess step by step]

Redis cache design and performance optimization

Mysql-8.0.17-winx64 (additional Navicat) manual configuration version installation

mongoDB复制集及分片集群

Yapi vulnerability hanging horse program chongfu.sh processing

What kind of air conduction Bluetooth headset with good configuration is recommended

How about air conduction Bluetooth headset? It's the most worthwhile air conduction headset to start with

SSAO by computer shader (II)
随机推荐
JS variable is equal to 0, etc. '
单元测试框架Jest搭配TypeScript的安装与配置
技术分享 | 实战详解接口测试请求方式Get、post
Fermat's theorem
单项链表的创建、遍历以及按要求查找结点
mongo ssl 配置实战
测试面试题集锦(五)| 自动化测试与性能测试篇(附答案)
AQS之ReentrantLock源码解析
Mongodb quick start
Redis implementation of distributed lock and analysis of the main process of redismission distributed lock
网络——网络层
Gerapy use
JS四则运算重新封装,解决精度丢失问题
链表中结点的插入和删除
How about air conduction Bluetooth headset? It's the most worthwhile air conduction headset to start with
如何描述一个BUG以及BUG级别的定义、生命周期
JS逆向100题——第1题
SSAO by computer shader (II)
Yapi vulnerability hanging horse program chongfu.sh processing
Leetcode brush question diary sword finger offer II 055. binary search tree iterator