当前位置:网站首页>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;
}边栏推荐
猜你喜欢

Graphic pipeline foundation (part outside)
![[C language] dynamic memory management](/img/bb/2ec65b38e85f53269dc03d885d70f4.png)
[C language] dynamic memory management

Redis implementation of distributed lock and analysis of the main process of redismission distributed lock

RayMarching实现体积光渲染
![[pta ---- traversal of tree]](/img/d8/260317b30d624f8e518f8758706ab9.png)
[pta ---- traversal of tree]

mongo ssl 配置实战

Explain in detail

Two dimensional array practice: spiral matrix

mysql索引优化

Graphic pipeline foundation (II)
随机推荐
费马小定理
NFT data storage blind box + mode system development
进程和线程的区别
[the beginning of self redemption]
Execjs call
[C language] dynamic memory management
Implementation of simple address book in [c language]
数组转链表
JS reverse question 100 - question 1
OJ 1089 Spring Festival travel
OJ 1089 春运
Skimming records -- sequence traversal of binary tree
Code neatness (2)
战疫杯--我的账本
Leetcode 刷题日记 剑指 Offer II 053. 二叉搜索树中的中序后继
[哈希表基础知识]
2021-11-10
图形管线基础(一)
Explain in detail
---栈&队列---