[并查集] How Many Tables
题目描述
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.
输入
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.
输出
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
样例输入
样例输出
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; int f[]; void init()
{
for(int i=;i<;i++) f[i]=i;
} int findFather(int x)
{
int a=x;
while(x!=f[x])
{
x=f[x];
}
while(a!=f[a])
{
int z=a;
a=f[a];
f[z]=x;
}
return x;
} int cnt=; void unionf(int a,int b)
{
int fa=findFather(a);
int fb=findFather(b);
if(fa!=fb)
{
cnt-=;
f[fa]=fb;
}
} int main()
{
int t;
cin>>t;
for(int i=;i<t;i++)
{
init();
int n,m;
cin>>n>>m;
cnt=n;
for(int j=;j<m;j++)
{
int a,b;
cin>>a>>b;
unionf(a,b);
}
cout<<cnt<<endl;
}
}
[并查集] How Many Tables的更多相关文章
- HDU 1213 How Many Tables (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
- HDU 1213 How Many Tables(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- hdu 1213 (How Many Tables)(简单的并查集,纯模板)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU1213:How Many Tables(并查集)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 并查集:HDU1213-How Many Tables(并查集最简单的应用)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- HDU——1213How Many Tables(并查集按秩合并)
J - How Many Tables Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- 杭电 1213 How Many Tables (并查集求团体数)
Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...
- 杭电ACM省赛集训队选拔赛之热身赛-How Many Tables,并查集模板题~~
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
随机推荐
- 07.centos7构建 IntelliJ IDEA(简称IDEA)开发环境
一.安装IDEA 进入官网下载linux版的社区便,IDEA分为社区版和旗舰版,社区版免费,并且基本满足spark开发需求. 解压安装 目录为/opt/idea 注意:centos命令行界面下是无法安 ...
- iOS开发者证书-详解/生成/使用
本文假设你已经有一些基本的Xcode开发经验, 并注册了iOS开发者账号. 相关基础 加密算法 现代密码学中, 主要有两种加密算法: 对称密钥加密 和 公开密钥加密. 对称密钥加密 对称密钥加密(Sy ...
- STM32F4寄存器编写跑马灯例程
最近由于在学习STM32看到别人用寄存器编程控制跑马灯,于是自己也想试一试.可是试了好久终究弄不出来.回头看了下库函数的调用关系才搞明白.首先通过查看GPIOA的设置函数发现设置如下: void GP ...
- MQTT入门2 -- “Error: Invalid password hash for user nick.”和“Connection Refused: not authorised.”
原文地址:https://www.cnblogs.com/NickQ/p/9277315.html 问题描述: 搭建好mosqitto环境后,利用无密码验证方式,成功通过测试. 但修改配置文件将匿名访 ...
- python爬虫同时输出两个列表(zip函数)
简介:在做爬虫时,xpath返回的是列表格式,我们又需要将列表中的元素一一对应并存放至字典中,这是就可以用zip函数. zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组, ...
- C语言中的if与else if
今天发现一个比较不理解的代码,是关于else if这个判断语句的代码.代码如下 #include<stdio.h> ; int main(void) { ) { printf(" ...
- 关于C/C++语言的部分BUG
目录 scanf格式匹配引发的错误 局部变量被释放引发的bug 数组写入超出索引维度 指针的指针引发的思考 未定义赋值的变量引发的bug 题外话 scanf格式匹配引发的错误 运行如下程序时,出现 ...
- 加分项——C语言实现Linux的pwd命令
加分项--C语言实现Linux的pwd命令 实现要求 1 学习pwd命令 2 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3 实现mypwd 4 测试mypwd pwd pw ...
- 半个小时教你写一个装(bi)逼(she)之地图搜租房
半个小时教你写一个装(bi)逼(she)之地图搜租房 首先需要一个Python3环境,怎么准备我就不多说了,实在不会的出门右转看一下廖雪峰老师的博客. HTML部分 代码来自:高德API+Python ...
- DSP6748学习笔记-190116
1. PSC是什么?电源和睡眠控制器.一个PSC模块由一个全局PSC和一组本地PSC组成,那么又是什么? PSCModuleControl(SOC_PSC_0_REGS, , PSC_POWERDOM ...