解题思路:输入总人数 n,和m组数据;即和杭电畅通工程相类似,对这m组数据做合并操作后,求最后一共有多少块区域。

#include<stdio.h>
int pre[50001];
int find(int root)
{
if(root!=pre[root])
pre[root]=find(pre[root]);
return pre[root];
}
void unionroot(int x,int y)
{
int root1,root2;
root1=find(x);
root2=find(y);
if(x!=y)
{
pre[root1]=root2;
} } int main()
{
int n;
int i;
int x,y;
long int m;
int tag=1;
while(scanf("%d %d",&n,&m)!=EOF&&(n||m))
{
int sum=0; for(i=0;i<n;i++)
pre[i]=i;
while(m--)
{
scanf("%d %d",&x,&y);
unionroot(x,y);
}
for(i=0;i<n;i++)
{
if(find(i)==i)
sum++;
}
printf("Case %d: %d\n",tag,sum);
tag++; }
}

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. (转)基于MVC4+EasyUI的Web开发框架经验总结(6)--在页面中应用下拉列表的处理

    http://www.cnblogs.com/wuhuacong/p/3840321.html 在很多Web界面中,我们都可以看到很多下拉列表的元素,有些是固定的,有些是动态的:有些是字典内容,有些是 ...

  2. RabbitMQ学习之ConntectionFactory与Conntection的认知

    在发送和接收消息重要的类有:ConnectionFactory, Connection,Channel和 QueueingConsumer. ConntectionFactory类是方便创建与AMQP ...

  3. Java中的常量

    常量的概念 是指在Java程序中固定不变的数据.我们可以理解为是一种特殊的变量,它的值被设定后,在程序运行过程中不允许改变. 常量的分类 整数常量:  所有的整数   例如 100 -100 123 ...

  4. Javase范式

    package Xwxx; import java.util.ArrayList; import java.util.Iterator; import java.util.function.IntBi ...

  5. java 常用API 包装 数据

    package com.oracel.demo01; public class Sjzhhm { public static void main(String[] args) { method(); ...

  6. 【leecode】小练习(简单8题)

    def twoSum(nums, target): """ 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[ ...

  7. Oracle 知识积累

    1.oracle存储过程中is和as的区别 在存储过程(PROCEDURE)和函数(FUNCTION)中没有区别,在视图(VIEW)中只能用AS不能用IS,在游标(CURSOR)中只能用IS不能用AS ...

  8. npm run build 打包项目,图片等资源使用相对路径会出现路径错误的问题

    在build下的utils.js中,3使用 ‘vue-style-loader’ 依赖的地方添加 publicPath: '../../' , 如图:

  9. nyoj254-编号统计

    编号统计 时间限制:2000 ms  |  内存限制:65535 KB 难度:2 描述 zyc最近比较无聊,于是他想去做一次无聊的统计一下.他把全校同学的地址都统计了一下(zyc都将地址转化成了编码) ...

  10. 记录python爬取猫眼票房排行榜(带stonefont字体网页),保存到text文件,csv文件和MongoDB数据库中

    猫眼票房排行榜页面显示如下: 注意右边的票房数据显示,爬下来的数据是这样显示的: 网页源代码中是这样显示的: 这是因为网页中使用了某种字体的缘故,分析源代码可知: 亲测可行: 代码中获取的是国内票房榜 ...