Hdoj 1213.How Many Tables 题解
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
思路
裸并查集题目,上模板就好了QAQ
代码
#include<bits/stdc++.h>
using namespace std;
int father[110];
void init(int n)
{
for(int i=1;i<=n;i++) father[i]=i;
}
int find(int x)
{
while(father[x]!=x) x=father[x];
return x;
}
void join(int a,int b)
{
int t1=find(a);
int t2=find(b);
if(t1!=t2) father[t1]=t2;
}
int getNum(int n)
{
int num = 0;
for(int i=1;i<=n;i++)
if(father[i]==i)
num++;
return num;
}
int main()
{
int t;
cin >> t;
while(t--)
{
int n,m;
cin >> n >> m;
init(n);
for(int i=1;i<=m;i++)
{
int a,b;
cin >> a >> b;
if(find(a) != find(b))
join(a,b);
}
cout << getNum(n) << endl;
}
return 0;
}
Hdoj 1213.How Many Tables 题解的更多相关文章
- hdoj 1213 How Many Tables
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1213 How Many Tables(并查集算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 How Many Tables Time Limit: 2000/1000 MS (Java/O ...
- Hdoj 1050.Moving Tables 题解
Problem Description The famous ACM (Advanced Computer Maker) Company has rented a floor of a buildin ...
- 题解报告:hdu 1213 How Many Tables
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...
- HDOJ并查集题目 HDOJ 1213 HDOJ 1242
Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...
- 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 ...
- 【HDUOJ】1213 How many tables
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 题意:Ignatius邀请了n个朋友来家里,朋友之间如果互相不认识的不想坐一起,所以至少要准备几 ...
随机推荐
- JavaScript动态修改html组件form的action属性
用javaScript动态修改html组件form的action属性,可以在提交时再决定处理表单的页面. <%--JavaScript部分--%><script language=& ...
- nginx 编译安装以及简单配置
前言 Nginx的大名如雷贯耳,资料太多了,网上一搜一大把,所以这里就不阐述nginx的工作原理了,只是简单的编译安装nginx,然后呢,简单配置一下下. 下载Nginx.安装 下载地址:http:/ ...
- shell脚本--操作MySQL数据库
其实就是一个很简单的套路,和其他语言差不多,首先连接数据库,然后在进行其他操作. 套路如下: #!/bin/bash mysql="mysql -uroot -proot" #连接 ...
- python3 Tkinter GUI 试水
from tkinter import * #导入tkinter下所有包,用于GUI开发#窗口创建tk=Tk()cans=Canvas(tk,width=400,height=400)#定义窗口规格c ...
- React-Native之截图组件view-shot的介绍与使用
React-Native之截图组件view-shot的介绍与使用 一,需求分析 1,需要将分享页生成图片,并分享到微信好友与朋友圈. 二,react-native-view-shot介绍 1,可以截取 ...
- Day 4-7 -configparser模块
此模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. 常用方法: import configparser conf = configpar ...
- liunx 运维知识二部分
Windows下的目录和Linux系统下的目录有什么区别? Windows目录下的文件一般都是分区(C盘,D盘...),C盘下面有什么目录,目录下面还有其他目录加上文件. Linux系统目录结构一切都 ...
- HTML5开发之meta标签的viewport使用说明
随着高端手机(Andriod,Iphone,Ipod,WinPhone等)的盛行,移动互联应用开发也越来越受到人们的重视,用html5开发移动应用是最好的选择.然而每一款手机有不同的分辨率,不同屏幕大 ...
- JDBC 初始。
package cn.zhouzhou; /* 一.JDBC? 1.(java date base connectivity,java)是一种用于执行SQL语句的java API . 2.jdbc本质 ...
- LODOP打印安装到win的特殊字体
LODOP能够打印的字体,来源于安装到本机windows里字体库的字体,如果需要打印特别的字体,需要在该操作系统安装.由于web网站的用户千差万别,字体库也有不同,但是一般常见的字体都是有的,因此做模 ...