题目链接:http://acm.tju.edu.cn/toj/showp2469.html

感觉这个题目有点问题,算了不管他了,反正A了。

这里要注意的是求这个集合有多少种,那么就是要剔除重复数后,再数一下有多少个。

难一点的算法我也不会,是帆哥教我的,就是用map<int,bool>,只要maps.find(p)要是没有,就是一个不重复的集合ans++,或者maps.size();

这个题目的问题在于,father[i]里面存的是根节点,但是,我直接maps.find(father[i])却WA了,我改成p=Find_Set(i),find(p)就AC了,很不懂。

#include <stdio.h>
#include <algorithm>
#include <map> using namespace std; int father[]; int Find_Set(int x)
{
if(x!=father[x])
father[x] = Find_Set(father[x]);
return father[x];
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
map<int,bool> maps;
int n,m;
scanf("%d%d",&n,&m);
for(int i=; i<=n; i++)
father[i] = i; int x,y;
for(int i=; i<m; i++)
{
scanf("%d%d",&x,&y);
int fx = Find_Set(x);
int fy = Find_Set(y);
if(fx!=fy)
{
father[fy] = fx;
}
} int ans=;
for(int i=; i<=n; i++)
{
int p = Find_Set(i);
if(maps.find(p)==maps.end())
{
maps[p] = true;
ans ++;
}
} printf("%d\n",ans);
}
return ;
}

数集合有多少个TOJ(2469)的更多相关文章

  1. COJ 0036 数数happy有多少个?

    数数happy有多少个? 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 图图是个爱动脑子.观察能力很强的好学生.近期他正学英语 ...

  2. [Leetcode 216]求给定和的数集合 Combination Sum III

    [题目] Find all possible combinations of k numbers that add up to a number n, given that only numbers ...

  3. 数长方形有多少个?POJ(1693)

    题目链接:http://poj.org/problem?id=1693 解题报告: 随机选两根横的,再找一下与这两根横线相交的竖线有多少根,m,那么就有(m-1)*m/2个长方形. #include ...

  4. 数黑格有多少个,模拟题,POJ(1656)

    题目链接:http://poj.org/problem?id=1656 #include <stdio.h> #include <iostream> #include < ...

  5. 【BZOJ2734】【HNOI2012】集合选数(状态压缩,动态规划)

    [BZOJ2734][HNOI2012]集合选数(状态压缩,动态规划) 题面 Description <集合论与图论>这门课程有一道作业题,要求同学们求出{1, 2, 3, 4, 5}的所 ...

  6. Frequent Values-线段树求解出现最多的数

    Frequent Values(poj 3368) 注意:以下答案为离线作答结果,并非能通过poj,若要通过poj,需要修改函数接口,因为以下程序接受半封闭区间(s,e],同时还需要修改输入数据的顺序 ...

  7. Redis------Set集合类型

    set是string类型的无序集合 类比:你的朋友不能超过2的32次方-1个元素 基本添加删除操作 取并集 取交集 取差集 注意:每个元素的各个元素不能重复 应用场合:qq好友推荐‘ TomFri 的 ...

  8. Java中的集合(Set,List,Map)

    ******************collections类总结*************************** JAVA集合主要分为三种类型:    Set(集)    List(列表)    ...

  9. .NET 基础 一步步 一幕幕[数组、集合、异常捕获]

    数组.集合.异常捕获 数组: 一次性存储多个相同类型的变量. 一维数组: 语法: 数组类型[] 数组名=new 数组类型[数组长度]; 声明数组的语法: A.数据类型 [] 数组名称= new 数据类 ...

随机推荐

  1. linux下重启php服务

    有时候修改了一些php配置或者进程满了需要重启php [root@snoopy :: bin]# service php-fpm restart Gracefully shutting down ph ...

  2. Python 15 I/O编程

    读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...

  3. ArrayList中进行删除操作引发的问题

    1.普通for遍历 for(int i=0;i<list.size();i++){ if(list.get(i).equals("a")) list.remove(i); } ...

  4. C# 序列化(Binary、Xml、Soap)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  5. Silverlight 中图片路径的设置

    在Silverlight中图片的设置方法有几种 如上图在一个工程中有个images文件夹,buttons.xaml页面中的Image控件要引用一张图片 第一种方法 xaml: <Image x: ...

  6. kmspico

    # process | 在这儿找到了原作者的地址 http://blog.nsfocus.net/kmspico/ | 下面是原作者地址 https://forums.mydigitallife.ne ...

  7. CentOS 7 Linux 卸载/安装 Mariadb MySQL mysql 5.7

    [root@localhost mysql]# ls mysql-community-client--.el7.x86_64.rpm mysql-community-embedded-compat-- ...

  8. 将应用图标添加到ubuntu dash中

    1 在appplications中添加一个desktop文件 sudo gedit /usr/share/applications/xdbe.desktop 2 在desktop文件中添加如下 [De ...

  9. C面向对象编程

    C语言面向对象编程 1. 定义一个SuperObject结构体, 里面包含最少的元素, 但是确实每一个对象都含有的, 这样可以实现多态 2. 每一个对象都是基于类的, 我们知道类都是单例对象, 所以我 ...

  10. C#中 计时器用法 运行时间

    有时候我们会需要计算某段代码运行的时间 比如一个sql查询,记录一段代码所花费的时间等等代码如下: System.Diagnostics.Stopwatch watch = new System.Di ...