当前位置:网站首页>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;
}边栏推荐
- Question skimming record - hash table
- Redis implementation of distributed lock and analysis of the main process of redismission distributed lock
- Personal understanding of Chinese remainder theorem
- Analysis of reentrantlock source code of AQS
- 测试面试题集锦(一)| 软件测试常见必考问题与流程篇(附答案)
- Mongodb replica set and partitioned cluster
- How to calculate the size of structure, segment and Consortium (common body)
- HDU-5806-NanoApeLovesSequenceⅡ(尺取法)
- Analysis of the semaphore source code of AQS
- PKU-2524-Ubiquitous Religions(并查集模板)
猜你喜欢
![[dynamic planning -- the best period series for buying and selling stocks]](/img/90/9060eabc9b9e7f660504806fde282d.png)
[dynamic planning -- the best period series for buying and selling stocks]

Graphic pipeline foundation (II)

JS逆向100题——第1题

explain详解

KVM热迁移

How to calculate the size of structure, segment and Consortium (common body)

Dynamic memory management function of C language

AQS之countDownLatch源码分析

What's a good gift for Tanabata? Niche and advanced product gift recommendation

项目编译NoSuch***Error问题
随机推荐
HDU-1159-CommonSubsequence(LCS最长公共子序列)
订单交易简析
TCP/IP五层模型
Array solution script
Redis cache design and performance optimization
HDU-2036-改革春风吹满地(多边形面积模板)
InitializingBean接口及示例
网络——传输层(详细版)
mongo ssl 配置实战
测试面试题集锦(二)| 测试工具篇(附答案)
Prometheus monitoring Nacos
单元测试框架Jest搭配TypeScript的安装与配置
代码整洁之道(二)
redis实现分布式锁思路及redission分布式锁主流程分析
PKU-2524-Ubiquitous Religions(并查集模板)
[pta-- use queues to solve the problem of monkeys choosing kings]
NIO示例
Graphic pipeline foundation (I)
技术分享 | 实战详解接口测试请求方式Get、post
mongoDB复制集及分片集群