说来惭愧啊。。现在才会并查集。我竟然给我妈妈讲明白并查集怎么回事了- -

#define  _CRT_SECURE_NO_WARNINGS

#include <iostream>

using namespace std;

#define maxx 50010

int set[maxx];

int find(int x)
{
return x == set[x] ? x : (set[x] = find(set[x]));
} int main()
{
int n, m;
int kase = 0;
while (cin >> n >> m && (n || m))
{
for (int i = 1; i <= n; i++)
{
set[i] = i;
}
for (int i = 0; i < m; i++)
{
int a, b;
scanf("%d %d", &a, &b); int c = find(a), d = find(b);
if (c != d){ set[c] = d; } }
int cnt = 0;
for (int i = 1; i <= n; i++)
{
if (set[i] == i) cnt++;
}
cout << "Case " << ++kase << ": " << cnt << endl;
}
}

poj2524的更多相关文章

  1. poj2524 Ubiquitous Religions(并查集)

    题目链接 http://poj.org/problem?id=2524 题意 有n个学生,编号1~n,每个学生最多有1个宗教信仰,输入m组数据,每组数据包含a.b,表示同学a和同学b有相同的信仰,求在 ...

  2. POJ1611 && POJ2524 并查集入门

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 28293   Accepted: 13787 De ...

  3. [原]poj-2524(裸并查集)

    题目链接: http://poj.org/problem?id=2524 题意: n个人,m对人宗教相同,输出一共有多少个不同的宗教. 代码如下: #include<iostream> # ...

  4. poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)

    http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...

  5. POJ2524+并查集

    题意简单. 询问n个人的宗教关系. #include<stdio.h> ; int fa[ maxn ]; int vis[ maxn ]; void init( int n ){ ;i& ...

  6. POJ2524——Ubiquitous Religions

    Ubiquitous Religions Description There are so many different religions in the world today that it is ...

  7. POJ2524 Ubiquitous Religions(并查集)

    题目链接. 分析: 给定 n 个点和 m 条无项边,求连通分量的数量.用并查集很简单. #include <iostream> #include <cstdio> #inclu ...

  8. POJ2524:Ubiquitous Religions (并查集模板)

    Description There are so many different religions in the world today that it is difficult to keep tr ...

  9. POJ2524并查集水题

    Description There are so many different religions in the world today that it is difficult to keep tr ...

随机推荐

  1. (转)ikvmc的使用

    IKVM.NET是一个针对Mono和微软.net框架的java实现,其设计目的是在.NET平台上运行java程序.本文将比较详细的介绍这个工具的原理.使用入门(如何java应用转换为.NET应用.), ...

  2. Android网络编程http派/申请服务

    最近的研究Android网络编程知识,这里有一些想法,今晚学习.与您分享. 在实际的应用程序的开发非常需要时间appserver请求数据,那么app怎样发送请求呢?以下的代码就是当中的一种情况.使用H ...

  3. VMware vSphere 服务器虚拟化之二十六 桌面虚拟化之View Persona Management

    VMware vSphere 服务器虚拟化之二十六 桌面虚拟化之View Persona Management 实验失败告终,启动VMware View Persona Management服务报10 ...

  4. CF552E 字符串 表达式求值

    http://codeforces.com/contest/552/problem/E E. Vanya and Brackets time limit per test 1 second memor ...

  5. linux动态库编译和使用

    linux动态库编译和使用详细剖析 引言 重点讲述linux上使用gcc编译动态库的一些操作.并且对其深入的案例分析.最后介绍一下动态库插件技术, 让代码向后兼容.关于linux上使用gcc基础编译, ...

  6. Trie图

    AC自动机是KMP的多串形式,当文本串失配时,AC自动机的fail指针告诉我们应该跳到哪里去继续匹配(跳到当前匹配串的最长后缀去),所以AC自动机的状态是有限的 但是AC自动机具有不确定性, 比如要求 ...

  7. C++第11周(春)项目1 - 存储班长信息的学生类

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759,内有完整教学方案及资源链接 [项目1 - 存储班长信息的学生类] clas ...

  8. struts2 Session拦截器

    一:struts2简介 二:拦截器

  9. Net程序员学习Linux

    Net程序员学习Linux 本次知识点:Linux系统的多终端切换,linux下的用户,linux远程访问工具使用,linux下重要的目录,命令的组成,通配符,linux的路径问题,文件操作的综合运用 ...

  10. c# ThreadPoold使用心得

    于c#多线程编程经常使用的线程,但是,因为线程的创建和销毁是非常资源 - 成本非常大.因此,我们使用线程池来解决问题, 在线程池的开始是申请一定数量的线程系统.和维护,有任务时间,假设你有空闲的线程, ...