当前位置:网站首页>PKU-2524-Ubiquitous Religions(并查集模板)
PKU-2524-Ubiquitous Religions(并查集模板)
2022-07-28 05:28: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
并查集模板。
#include <stdio.h>
#include <string.h>
#define MAX 50000
int pre[MAX+10];
int t[MAX+10];//t 用于标记独立块的根结点
int find(int x){//查找根节点
int r=x;
int i=x,j;
while(pre[r]!=r){
r=pre[r];
}
as
while(pre[i]!=r){//路径压缩
j=pre[i];
pre[i]=r;
i=j;
}
return r;
}
void mix(int x,int y){//连通两个集合
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++){//标记根节点
t[find(i)]=1;
}
for(i=0;i<n;i++)
if(t[i]==1)
s++;
printf("Case %d: %d\n",ans++,s);
}
return 0;
}边栏推荐
- feignclient @RequestMapping参数设置及请求头简易方式设置
- [C language] dynamic memory management
- OJ 1242 freshman's debut
- [dynamic planning -- the best period series for buying and selling stocks]
- Using C language to realize three piece chess games
- [pta---- output full arrangement]
- [哈希表基础知识]
- 遍历 二叉树
- js 变量等于0也等也' '问题
- Graphic pipeline foundation (II)
猜你喜欢

SSAO By Computer Shader(三)

explain详解

mongoDB复制集及分片集群

Water drop effect on umbrella

Dynamic memory management function of C language

What is hash? (development of Quantitative Trading Robot System)
![[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]

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

Explain in detail

Graphic pipeline foundation (I)
随机推荐
Leetcode brush questions diary sword finger offer II 047. Binary tree pruning
Leetcode 刷题日记 剑指 Offer II 055. 二叉搜索树迭代器
Dynamic programming -- simple question type of climbing stairs
Personal understanding of Chinese remainder theorem
网络——数据链路层
软件开发中常见模型
Pyppeter drop-down selenium drop-down
js 变量等于0也等也' '问题
Mysql-8.0.17-winx64 (additional Navicat) manual configuration version installation
Mongodb replica set and partitioned cluster
Code tidiness (I)
OJ 1131 beautiful number
Development of clip arbitrage / brick carrying arbitrage system
SSAO By Computer Shader(三)
OJ 1242 大一上之初出茅庐
Feignclient @requestmapping parameter setting and simple method setting of request header
Question brushing record -- binary tree
feignclient @RequestMapping参数设置及请求头简易方式设置
Analysis of the semaphore source code of AQS
SSAO By Computer Shader(一)