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

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

#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. Linux 下SVN自动更新

    1.找到svn的所在目录: 我的目录在/usr/local/svn/myproject 2.新建post-commit vim hooks/post-commit #!/bin/shexport LA ...

  2. SDWebImage使用详解

    这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理.异步下载.同一个URL下载次数控制和优化等特征.使用示范的代码:UITableView使用UIImageView+ ...

  3. java读写文件大全

     java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...

  4. mysql 获取一个表中缺失的最小编号

    select count(*),t1.`name` from test_id t1INNER JOIN test_id t2on t1.id >= t2.idgroup by t1.id,t1. ...

  5. 【linux】bash常用快捷键

    Ctrl + r:逆向搜索命令历史 Ctrl + l:清屏 Ctrl + c:终止命令 Ctrl + u:删除光标前的指令 Ctrl + k:删除光标后的指令 Ctrl + d:退出登陆

  6. html5外包—长年承接html5外包业务:《Sencha Touch权威指南》下载

    <Sencha Touch权威指南>内容简介:如何才能全面而透彻地理解和掌握移动应用开发框架Sencha Touch并开发出令人心动的移动应用?<Sencha Touch权威指南&g ...

  7. 利用maven的filter和profile实现不同环境使用不同的配制

    在我们平常的java开发中,会经常使用到很多配制文件(xxx.properties,xxx.xml),而当我们在本地开发(dev),测试环境测试(test),线上生产使用(product)时,需要不停 ...

  8. IntelliJ IDEA安装lombok插件

    Settings→Plugins→Browse repositories 输入lom后选择Install Plugin 按照提示重启IDEA   来自为知笔记(Wiz)

  9. Nginx做反向代理总是被系统kill

    公司使用Nginx做反向代理,以前都挺正常的,最近不知怎么回事总是无端被系统kill,而在nginx错误日志中也没有信息输出. 网上查了很多资料,也没什么靠谱的回答,唯一觉得有点关联的就是linux ...

  10. html5 中meta中 content=width=device-width注意

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...