博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu1213 How Many Tables
阅读量:6572 次
发布时间:2019-06-24

本文共 1924 字,大约阅读时间需要 6 分钟。

How Many Tables

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13476    Accepted Submission(s): 6608

Problem Description

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

Author

Ignatius.L

Source

 

思路:并查集,统计集合个数

 

#include
#include
#include
#include
#include
using namespace std;const int maxn=1000+5;int p[maxn];int n,m;int ans;void make_set(){ for(int i=0;i<=n;i++) p[i]=i;}int find_set(int x){ return p[x]!=x ? p[x]=find_set(p[x]) : x;}void union_set(int x, int y){ int fx=find_set(x), fy=find_set(y); if(fx!=fy) { p[fx]=fy; }}int main(){ int T; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); make_set(); for(int i=0;i

转载地址:http://bgojo.baihongyu.com/

你可能感兴趣的文章
Enhanced VMotion Compatibility (EVC) 功能介绍和实战设置
查看>>
RabbitMQ 流控制学习
查看>>
Ubuntu16.04 ssh安及root登录
查看>>
一个工程两个target
查看>>
linux 给文件夹权限
查看>>
用复制mysql/data 文件夹 下面的数据库的形式来复制数据库出现的问题
查看>>
C语言dos程序源代码分享(进制转换器)
查看>>
php项目中常用的log日志记录方法
查看>>
Android--实现点击一次返回键返回桌面而不是退出应用
查看>>
LogParser 导入MSSQL
查看>>
左侧固定导航栏
查看>>
linux安装go环境并编写第一个go程序
查看>>
解决:laravel出现Please provide a valid cache path.
查看>>
[JAVA] String常用方法
查看>>
oracle
查看>>
兼容IE浏览器样式的html上传文件控件
查看>>
直接插入排序
查看>>
fstab中mount错误导致不能启动
查看>>
OSPF转发地址深入解析
查看>>
SQLServer的Top功能
查看>>