HDU1213 (并查集)
题意描述
Today is Ignatius’ birthday. He invites a lot of friends. Now it’s dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers. One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table. For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
Input The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
Output For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
Sample Input 2 5 3 1 2 2 3 4 5
5 1 2 5
Sample Output 2 4
思路
这道题最终的目的是求出有几个不同的集合,所以在合并完集合以后,我们将每个祖宗结点都放进set里面,最后直接输出set的size值即可
AC代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<map>
#include<set>
#define IOS ios::sync_with_stdio(false); cin.tie(0);
using namespace std;
typedef pair<string,int> PII;
typedef long long ll;
const int N=1010;
int p[N];
int n,m;
int find(int x){
if(p[x]!=x) p[x]=find(p[x]);
return p[x];
}
int main(){
IOS;
int T;
cin>>T;
while(T--){
cin>>n>>m;
memset(p,0,sizeof p);
for(int i=1;i<=n;i++){
p[i]=i;
}
while(m--){
int a,b;
cin>>a>>b;
p[find(a)]=find(b);
}
set<int> ans;
for(int i=1;i<=n;i++) ans.insert(p[find(i)]);
cout<<ans.size()<<endl;
}
return 0;
}
- C#神奇的扩展方法
- 使用Python制作一个简单的刷博器
- 零基础学编程031:Python与其它语言最不同的一条语法规则
- dedecms批量导出新增文章url和标题
- 明理知意:复合索引优化及索引访问原理
- WCF配置文件与文件下载之坎坷路
- 零基础学编程030:像黑客般玩玩字符艺术
- Android获取SD卡总容量,可用大小,机身内存总容量及可用大小
- 零基础学编程029:程序员作图不用笔
- 零基础学编程028:面向对象编程OOP
- 如何修改帝国cms文章点击量默认值和成倍增加
- 深入内核:监听器的工作原理与故障诊断分析
- 零基础学编程027:站在巨人的肩膀上
- 群分享:Markdown + CSS 实现微信公众号排版
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 聊聊dubbo-go的RPCInvocation
- 程序员是怎么记住一堆密码的?
- 腾讯大牛教你MySQL 8.0 PFS histogram解析与优化
- Python __init__.py 作用详解
- Python创建包,导入包
- Python查看模块(变量、函数、类)方法
- Python __doc__属性:查看文档
- Python __file__属性:查看模块的源文件路径
- python第三方库的安装pip的使用与换源
- 表白必备:C语言实现会变色的心
- 人心易变,这段有趣的C代码也一样,来看一下?
- 一个简单但能考察C语言基础的题目,试试?
- 这绝对是C语言的一个经典例题了!
- 你见过最美的C语言代码是什么?
- C语言直接实现开机密码修改!