[并查集] 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) ...
随机推荐
- 文库网站建设,文库网站PHP代码,TP开发文库网
专业定制仿百度文库网站系统,文库网站系统源码,文库网站建设开发,支持电脑版+手机版+微信版+小程序版+APP版,由10年的技术团队专业定制,需要的朋友可以联系我们.网站采用:PHP+MySQL+t ...
- 【四】搭建Nginx服务器
[任务4]搭建Nginx服务器 [任务4]搭建Nginx服务器 下载Nginx源码包 安装Nginx 解压Nginx安装包 安装Nginx依赖 启动Nginx 安装cgi 下载cgi并解压安装包 编译 ...
- python学习之路-基本数据类型1 变量的概念、数字、字符串
1 什么是数据类型? 每种编程语言都有自己的数据类型,用于标识计算机可以认识的数据,Python中主要的数据类型为字符串,整数,浮点数,列表,元祖,字典,集合七种主要的数据类型,其中以列表,字典为最主 ...
- excel中CTRL+E的用法
偶然发现excel中CTRL+E有按照例子填充的功能. 结果如下
- odoo学习之:在tree view中显示部分数据domain的使用
只要在window.action中他添加相应东domain即可,如: <!-- 树型列表 --> <record model="ir.actions.act_window& ...
- python 逆波兰式
逆波兰式,也叫后缀表达式 技巧:为简化代码,引入一个不存在的运算符#,优先级最低.置于堆栈底部 class Stack(object): '''堆栈''' def __init__(self): se ...
- Java——基于java自身包实现消息系统间的通信(TCP/IP+BIO)
最近看到阿里的一位童鞋写的一本关于分布式的书,感觉不错,准备把这本书上基础的代码都写一写. /** * Created by LiuHuiChao on 2016/11/15. * descripti ...
- HBase第一章 安装 HMaster 主备
1.集群环境 Hadoop HA 集群规划 hadoop1 cluster1 nameNode HMaster hadoop2 cluster1 nameNodeStandby ZooKeeper ...
- 探究linux设备驱动模型之——platform虚拟总线(一)
说在前面的话 : 设备驱动模型系列的文章主要依据的内核版本是2.6.32的,因为我装的Linux系统差不多就是这个版本的(实际上我用的fedora 14的内核版本是2.6.35.13的.) ...
- Django之视图系统
Django的View(视图) 一个视图函数(类),简称视图,是一个简单的python函数(类),它接受web请求并返回web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误,或者 ...