并查集。

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 55555;
int fa[maxn];
int vis[maxn];
int n,m,t;
void init(){
for(int i = 0; i < n; i++) {fa[i] = i;}
memset(vis,0,sizeof(vis));
}
int find_father(int u){
return fa[u] == u ? u : fa[u] = find_father(fa[u]);
}
int main(){
int Case = 1;
while(scanf("%d%d",&n,&m)){
if(!m && !n) break;
init();
for(int i = 0; i < m; i++){
int x,y;
scanf("%d%d",&x,&y);
int fx = find_father(x);
int fy = find_father(y);
fa[fx] = fy;
}
int cnt = 0;
for(int i = 1; i <= n; i++){
int t = find_father(i);
if(!vis[t]){
vis[t] = 1;
cnt ++;
}
}
printf("Case %d: %d\n",Case++,cnt);
}
return 0;
}

【POJ-2524】Ubiquitous Religions(并查集)的更多相关文章

  1. [ACM] POJ 2524 Ubiquitous Religions (并查集)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23093   Accepted:  ...

  2. POJ 2524 Ubiquitous Religions (幷查集)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23090   Accepted:  ...

  3. poj 2524 Ubiquitous Religions (并查集)

    题目:http://poj.org/problem?id=2524 题意:问一个大学里学生的宗教,通过问一个学生可以知道另一个学生是不是跟他信仰同样的宗教.问学校里最多可能有多少个宗教. 也就是给定一 ...

  4. poj 2524:Ubiquitous Religions(并查集,入门题)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23997   Accepted:  ...

  5. poj 2524 Ubiquitous Religions 一简单并查集

    Ubiquitous Religions   Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 22389   Accepted ...

  6. poj 2524 Ubiquitous Religions(并查集)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23168   Accepted:  ...

  7. POJ 2524 Ubiquitous Religions (并查集)

    Description 当今世界有很多不同的宗教,很难通晓他们.你有兴趣找出在你的大学里有多少种不同的宗教信仰.你知道在你的大学里有n个学生(0 < n <= 50000).你无法询问每个 ...

  8. poj 2524 Ubiquitous Religions(简单并查集)

    对与知道并查集的人来说这题太水了,裸的并查集,如果你要给别人讲述并查集可以使用这个题当做例题,代码中我使用了路径压缩,还是有一定优化作用的. #include <stdio.h> #inc ...

  9. 【原创】poj ----- 2524 Ubiquitous Religions 解题报告

    题目地址: http://poj.org/problem?id=2524 题目内容: Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 6 ...

  10. POJ 2524 Ubiquitous Religions

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 20668   Accepted:  ...

随机推荐

  1. js中typeof的用法汇总

  2. iptables 中的SNAT 和MASQUWERADE

    NAT 是 network address translation 的缩写 网络地址转换 网络地址转换主要有两种:SNAT和DNAT,即源地址转换和目标地址转换 SNAT:源地址转换 eg:多台pc机 ...

  3. ylbtech-KeFuYunWei(服务运维考核系统)-数据库设计

    ylbtech-DatabaseDesgin:ylbtech-KeFuYunWei(服务运维考核系统)-数据库设计 DatabaseName:KEFUYUNWEI Model:Admin 用户后台管理 ...

  4. 关于container_of和list_for_each_entry 及其相关函数的分析

    Linux代码看的比较多了,经常会遇到container_of和list_for_each_entry,特别是 list_for_each_entry比较多,因为Linux经常用到链表,虽然知道这些函 ...

  5. log4j教程 9、HTMLLayout

    如果想生成一个HTML格式的文件,日志信息,那么可以使用 org.apache.log4j.HTMLLayout 格式化日志信息. HTMLLayout类扩展抽象org.apache.log4j.La ...

  6. Spring MVC 解读——<mvc:annotation-driven/>

    Spring MVC 解读——<mvc:annotation-driven/> 一.AnnotationDrivenBeanDefinitionParser 通常如果我们希望通过注解的方式 ...

  7. oracle 12C SYS,SYSTEM用户的密码都忘记或是丢失

    密码 conn / as sysdba alter user system identified by Abcd1234; manual script first -->manual_scrip ...

  8. functools.wraps

    我们在使用 Decorator 的过程中,难免会损失一些原本的功能信息.直接拿 stackoverflow 里面的栗子     1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  9. [LeetCode] Combinations——递归

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  10. STL学习笔记(排序算法)

    STL提供了好几种算法对区间内的元素排序.出来完全排序外,还支持局部排序. 对所有元素排序 void sort(RandomAccessIterator beg,RandomAccessIterato ...