并查集思想,初始化每个元素的根节点为本身。

求解目标是求解存在几个集合。解决方案:查看有多少个根节点,表现在记忆数组上就是有多少个元素的根是它本身。

#include<stdio.h>
#define M 50005
int ji[M];
int findme(int a)
{
while(ji[a]!=a)
{
a=ji[a];
}
return a;
}
void link(int a,int b)
{
int tmp;
tmp=b;
a=findme(a);
b=findme(b);
ji[b]=a;
while(ji[tmp]!=tmp)
{
tmp=ji[tmp];
ji[tmp]=a;
}
}
int main()
{
int m,a,b,n,i,rel,ca;
ca=;
scanf("%d%d",&n,&m);
while(n!=)
{
ca++;
for(i=;i<=n;i++)
{
ji[i]=i;
}
rel=;
while(m--)
{
scanf("%d%d",&a,&b);
link(a,b);
}
for(i=;i<=n;i++)
{
rel+=(ji[i]==i);
}
printf("Case %d: %d\n",ca,rel);
scanf("%d%d",&n,&m);
}
return ;
}

POJ 2524的更多相关文章

  1. poj 2524 (并查集)

    http://poj.org/problem?id=2524 题意:在一所学校里面的人,都有宗教信仰,不过他们的宗教信仰有可能相同有可能不同,但你又不能直接去问他们,但你可以问他们和谁是同一个宗教.通 ...

  2. poj 2524 Ubiquitous Religions (并查集)

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

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

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

  4. POJ 2524 独一无二的宗教(裸并查集)

    题目链接: http://poj.org/problem?id=2524 Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K ...

  5. poj 2524 Ubiquitous Religions(宗教信仰)

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

  6. POJ 2524 :Ubiquitous Religions

    id=2524">Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 231 ...

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

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

  8. POJ 2524 Ubiquitous Religions

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

  9. poj 2524 并查集 Ubiquitous Religions

    //#include<bits/stdc++.h> #include<iostream> #include<stdio.h> #define max1 50005 ...

随机推荐

  1. Keepalived 双机web服务宕机检测切换系统软件

    简介 Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Kee ...

  2. DW(三):polybase基本理论

    PolyBase is a technology that accesses and combines(整合) both non-relational and relational data, all ...

  3. hadoop(二):hdfs HA原理及安装

    早期的hadoop版本,NN是HDFS集群的单点故障点,每一个集群只有一个NN,如果这个机器或进程不可用,整个集群就无法使用.为了解决这个问题,出现了一堆针对HDFS HA的解决方案(如:Linux ...

  4. Request Session生命周期及struts1 中service的编写

    现在接手的项目是一个早期的struts1框架的项目.同时也是刚开始接触web 以及struts1架构. 在处理多个action时,有一个tab子页面需要每5s自动刷新一次. 然后在测试过程中发现,点击 ...

  5. ABP系列文章总目录:

    转自:http://www.cnblogs.com/mienreal/p/4528470.html 1.ABP总体介绍 2.ASP.NET Boilerplate入门 3.ABP分层架构 4.ABP模 ...

  6. UI设计的重要性--避免二义性的输入提示

    昨天晚上发现了西安公路客运网上售票系统网站的密码找回系统存在安全漏洞,得出的结论是:密码找回页的漏洞:   1.用户名栏支持用户名.身份证.电话三种任意一种匹配.2.这一步是关键,密码找回问题提示栏居 ...

  7. HackerRank "Minimum Average Waiting Time" !

    Something to learn: http://blog.csdn.net/yuwenshi/article/details/36666453 Shortest Job First Algori ...

  8. 剑指offer系列18---顺时针打印矩阵

    [题目]定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数.要求时间复杂度是O(1).push pop min [思路]设计一个辅助栈,当新加入的原数据栈中的数小于辅助栈顶的数时就加 ...

  9. Python 通过print将数据保存到文件中

    1. Print them to screen man = [] other = [] try: data = open('sketch.txt') for each_line in data: tr ...

  10. android显示证书sha1

    创建证书可以用命令 keytool -genkey -v -keystore android.keystore -alias android -keyalg RSA -validity 或者andro ...