POJ2524(并查集)

2023-05-16

题目来源:http://poj.org/problem?id=2524

Description

There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in.

You know that there are n students in your university (0 < n <= 50000). It is infeasible for you to ask every student their religious beliefs. Furthermore, many students are not comfortable expressing their beliefs. One way to avoid these problems is to ask m (0 <= m <= n(n-1)/2) pairs of students and ask them whether they believe in the same religion (e.g. they may know if they both attend the same church). From this data, you may not know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. You may assume that each student subscribes to at most one religion.

Input

The input consists of a number of cases. Each case starts with a line specifying the integers n and m. The next m lines each consists of two integers i and j, specifying that students i and j believe in the same religion. The students are numbered 1 to n. The end of input is specified by a line in which n = m = 0.

Output

For each test case, print on a single line the case number (starting with 1) followed by the maximum number of different religions that the students in the university believe in.

Sample Input


10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0
  

Sample Output


Case 1: 1
Case 2: 7
  

Hint

Huge input, scanf is recommended
注意,题目提示用scanf,而我却用了cin,因此我的ac差一点点就超时了(用了4500ms),换成scanf,就只需要340ms了,速度提高了十几倍啊,看来IO速度差距不小啊。

 

下面是我的AC代码:

#include"iostream"
using namespace std;
const int MAX_SIZE = 50001;
class ufs
{
 public:
  int ancestor[MAX_SIZE];
  int degree[MAX_SIZE];
  int groups;
  void makeset();
  int findset(const int& x);
  void Union(const int& x,const int &y);
};
void ufs::makeset()
{
 for(int i =0;i<MAX_SIZE;++i)
 {
  ancestor[i] = i;
  degree[i] = 0;
 }
}
int ufs::findset(const int& x)
{
 if(ancestor[x] !=x)
 {
  ancestor[x] = findset(ancestor[x]);
 }
 return ancestor[x];
}
void ufs::Union(const int& x,const int& y)
{
 int xancestor = findset(x);
 int yancestor = findset(y);
 if(xancestor == yancestor)
 {
  return;
 }
 if(degree[xancestor] > degree[yancestor])
 {
  ancestor[yancestor] = xancestor;
  groups--;
 }
 else
 {
  if(degree[xancestor] == degree[yancestor])
  {
   degree[yancestor] += 1;
  }
  ancestor[xancestor] = yancestor;
  groups--;
 }
}

int Case =0;
int n,m;
int main(void)
{
 ufs UFS;
 int x,y;
 while(cin>>n>>m && !(n==0&&m==0))
 {
  UFS.groups = n;
  Case++;
  UFS.makeset();
  for(int i =0;i<m;++i)
  {
   cin>>x>>y;
   UFS.Union(x,y);
  }
  cout<<"Case "<<Case<<": "<<UFS.groups<<endl;
 }
 return 0;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

POJ2524(并查集) 的相关文章

  • 收藏一个架构博客

    http phl iteye com category 110171 http blog s135 com post 385
  • VM(虚拟机)部署自动化

    http www ibm com developerworks cn linux l auto deploy vm index html
  • 设计下一代自动化测试系统

    http www ni com white paper 7483 zhs
  • unity学习资料汇总

    xfeff xfeff unity这两年一直很火 xff0c 之前简单了解了一下 xff0c 个人认为它就像一个精简版的3ds max 为什么这样打比方呢 xff0c 因为我之前比较熟悉3ds max xff0c 初看unity的界面和功能
  • Java内部类的作用

    推荐一 定义 放在一个类的内部的类我们就叫内部类 二 作用 1 内部类可以很好的实现隐藏 一般的非内部类 xff0c 是不允许有 private 与protected权限的 xff0c 但内部类可以 2 xff0e 内部类拥有外围类的所有元
  • Dom4J解析xml文档

    1 DOM4J简介 DOM4J是 dom4j org 出品的一个开源 XML 解析包 DOM4J应用于 Java 平台 xff0c 采用了 Java 集合框架并完全支持 DOM xff0c SAX 和 JAXP DOM4J 使用起来非常简单
  • Eclipse 汉化插件

    汉化Eclipse xff08 1 xff09 下载Eclipse 对应版本的汉化包 下载链接 xff1a http download csdn net detail w57w57w57 7768469 xff08 2 xff09 在ecl
  • 使用java语言,利用多线程调用WebService进行数据处理

    http blog chinaunix net uid 20680669 id 3447319 html
  • excel VBA编程之:单元格保护

    ActiveSheet Unprotect Password 61 34 123 34 Cells 1 4 61 i 此处放上需要处理的代码 ActiveSheet Protect DrawingObjects 61 True Conten
  • vbs宏:excel读取多个文件并合并为一个文件

    Sub MergeFiles Dim p f m amp sh As Worksheet Set sh 61 ActiveSheet Application ScreenUpdating 61 False sh UsedRange Clea
  • vba日期和时间函数汇总和代码

    第一 xff0c vba日期和时间函数的基本用法 Excel中vba日期函数和时间函数分别是DATE和TIME VBA提供了三个无参数函数 xff1a Date Time Now xff0c 分别返回当前电脑系统的日期 时间 日期 43 时
  • 管道过滤器模式(Pipe and Filter)与组合模式(出处:http://haolloyin.blog.51cto.com/1177454/348277)

    之前在 benjielin 前辈的博客中看到 管道过滤器 xff08 Pipe And Filter xff09 模式 xff08 http bj007 blog 51cto com 1701577 345677 xff09 xff0c 当
  • 管道过滤器(Pipe-And-Filter)模式(出处:http://bj007.blog.51cto.com/1701577/345677)

    按照 POSA 面向模式的软件架构 里的说法 xff0c 管道过滤器 xff08 Pipe And Filter xff09 应该属于架构模式 xff0c 因为它通常决定了一个系统的基本架构 管道过滤器和生产流水线类似 xff0c 在生产流
  • 数据库架构的演变

    最近看了很多公司架构的演变的文章 xff0c 发现其中的基本思路和架构演变都很类似 xff0c 这里也总结一下数据库架构的演变以及演变背后的思路 单主机 最开始网站一般都是由典型的LAMP架构演变而来的 xff0c 一般都是一台linux主
  • 如何下载Android源代码

    Android已经火了很长时间了 xff0c 虽然做手机开发也有两年了 xff0c 但是一直没有深入接触到Android 前些天想下载Android源代码来看看 xff0c 没想到http android git kernel org九月初
  • Web数据库

    http baike baidu com link url 61 Tib3flBuOBsLy4IoMAxXt2z36Ms77 mQe85MBq7kJh0XfG7oluhlEinX3Maomb2mboXIcedxDEWvGPIDtNQfxa
  • 大型网站系统架构的演化

    前言 一个成熟的大型网站 xff08 如淘宝 京东等 xff09 的系统架构并不是开始设计就具备完整的高性能 高可用 安全等特性 xff0c 它总是随着用户量的增加 xff0c 业务功能的扩展逐渐演变完善的 xff0c 在这个过程中 xff
  • 架构设计案例分析-高速公路收费运营管理平台

    本文旨在通过对某省高速公路联网收费运营管理平台的架构设计过程进行案例分析 xff0c 描述架构设计的决策过程 1 业务背景 某省的高速公路分为近百个路段 xff0c 不同的路段归属不同的公司建设与运营 xff0c 造成了车辆在跨越不同路段时
  • 图片服务架构演进

    现在几乎任何一个网站 Web App以及移动APP等应用都需要有图片展示的功能 xff0c 对于图片功能从下至上都是很重要的 必须要具有前瞻性的规划好图片服务器 xff0c 图片的上传和下载速度至关重要 xff0c 当然这并不是说一上来就搞
  • 应用系统架构设计

    我们在做着表面上看似是对于各种不同应用的开发 xff0c 其实背后所对应的架构设计都是相对稳定的 在一个好的架构下编程 xff0c 不仅对于开发人员是一件赏心悦目的事情 xff0c 更重要的是软件能够表现出一个健康的姿态 xff1b 而架构

随机推荐

  • 用三层架构与设计模式思想部署企业级数据库业务系统开发

    1 1关于架构 架构这个词从它的出现后 就有许许多多的程序员 架构师们激烈地讨论着它的发展 xff0c 但是架构一词的出现 xff0c 却是随着三层架构的出现才出现的 当然 xff0c 目前应用三层架构开发也正是业界最关注的主题 那么这里我
  • KickStart安装教程

    KickStart安装教程 PXE概念介绍 xff1a PXE技术与RPL技术不同之处为RPL是静态路由 xff0c PXE是动态路由 RPL是根据网卡上的ID号加上其他记录组成的一个Frame xff08 帧 xff09 向服务器发出请求
  • DHCP的基本实现原理

    DHCP是一个局域网的网络协议 xff0c 使用UDP协议工作 xff0c 主要有两个用途 xff1a 给内部网络或网络服务供应商自动分配IP地址 xff0c 给用户或者内部网络管理员作为对所有计算机作中央管理的手段 xff0c 在RFC
  • 详解Windows PE(Windows预安装环境)

    Windows PE Windows PreInstallation Environment Windows PE 直接从字面上翻译就是 Windows预安装环境 xff0c 微软在2002年7月22日发布 xff0c 它的原文解释是 xf
  • Kickstart的高级应用

    Pre 和Postinstall 脚本 kickstart本身提供了一些对系统的基本调整和设置 xff0c 例如设置root密码 xff0c 设置时区等等 但是它不能做某些更细致的调整 xff0c 比如通过chkconfig禁止某些服务 x
  • SMTP错误码/建议解决方法

    SMTP错误码 建议解决方法 错误总表 420 1 Timeout Communication Problem Encountered During Transmission Thie Is a Novell Groupwise Smtp
  • Kickstart+NFS+DHCP+TFTP+PXElinux实现CentOS的网络自动安装

    Linux Kickstart无人值守安装 一 基本原理 PXE Pre boot Execution Environment 是由Intel设计的协议 xff0c 它可以使计算机通过网络启动 协议分为client和server两端 xff
  • 流程制造行业信息系统 架构

    流程制造行业信息系统 架构 执笔人 xff1a 郑玉堂 一 流程制造业信息技术应用的重要性 经济全球化趋势已经给各国经济发展带来越来越深刻的影响 xff0c 各国制造企业在世界市场上进行着日益激烈和残酷的竞争 剧烈的和不可测的市场环境变化
  • OpenGL纹理映射的几个问题

    今天在绘制颜色表的时候 xff0c 出现两个小问题 xff1a 目的是根据一个特定的颜色表 xff0c 用图像方式将颜色表绘制出来 xff0c 根据给定的颜色表 xff0c 我应该绘制出如下的图像才对 xff1a 1 我的颜色表绘制出来的图
  • 将自己写的经常复用的类封装成dll/lib的方法

    如果你的工作长期与某个领域相关 xff0c 比如说长期做直接体绘制 DVR 方面的开发 xff0c 那么你可能经常使用自己的传递函数类 xff0c 如果每一个工程你都把传递函数类的 h和 cpp文件添加进去会比较麻烦 xff0c 其实 xf
  • 从体数据分割谈解决问题之方法

    从体数据分割谈解决问题之方法 一 艰辛历程 xff1a 由于最近在做基于分割信息的体数据可视化时需要得到体数据的分割信息 每个体素的标识数据 xff0c 标识了每个体素属于什么组织 xff0c 为了得到体数据的分割信息我费了不找周折 下面是
  • 求大数的阶乘的位数:PKU :1423:Big Number

    题目描述 xff1a Description In many applications very large integers numbers are required Some of these applications are usin
  • 某人的ACM经历

    ACM经历总结 转帖 首先 xff0c 我想说的就是 xff0c 我是一个很普通的ACMer xff0c 高中没有参加过任何计算机和数学竞赛的经历 xff0c 也没有ben那样过人的天资 xff0c 努力至今也未能取得什么成绩 xff0c
  • PKU2051(优先队列求法)

    题意参见 xff1a http acm pku edu cn JudgeOnline problem id 61 2051 其实这个题目可以理解成os上的进程调度 xff1a 有一些进程 xff0c 每个进程有一个唯一的id和一个执行周期
  • 花钱要区分“投资”行为或“消费”行为(转载)

    在著名的美国第一学府哈佛大学 xff0c 经济学的第一堂课 xff0c 只教二个概念 第一概念 xff1a 花钱要区分 投资 行为或 消费 行为 xff1b 第二概念 xff1a 每月先储蓄30 工资 xff0c 剩下来才消费 有关专家做了
  • 时间管理也要区分“投资行为”与“消费行为”(转载)

    10 年前甲和乙是本科的同学 xff0c 在社会工作5 年后 xff0c 不约而同积蓄了30 万元人民币 5 年前 xff0c 他们都花掉了这30 万元 甲去通州购买了一套房 乙去买了一辆 奥迪 5 年后的今天 xff1a 甲的房子 xff
  • ZooKeeper的选举机制详解

    1 xff09 半数机制 xff1a 集群中半数以上机器存活 xff0c 集群可用 所以 Z ookeeper适合安装奇数台服务器 2 xff09 Zookeeper虽然在配置文件中并没有指定M aster和 S lave 但是 xff0c
  • 写了个幷查集的模板类...

    下面的概念介绍主要参考了 xff1a http www cnblogs com cherish yimi archive 2009 10 11 1580839 html xff0c 根据这个介绍 xff0c 自己写了个稍微通用一点的模板 x
  • PKU_1611(幷查集解法)

    题目来源 xff1a http poj org problem id 61 1611 采用幷查集的思路 xff0c 将同一个组的的学生合并为一个集合 xff0c 最后看那些学生跟student0属于同一个集合即可 下面是我的AC代码 xff
  • POJ2524(并查集)

    题目来源 xff1a http poj org problem id 61 2524 Description There are so many different religions in the world today that it