[并查集] 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字符转码
最近手里面有一个新的项目,下载的程序用的是 gbk, 可是我需要UTF8的格式,因为只有这个的格式才可以加入百度的MIP项目. 来此学习了解php编码的一些内容,还请多多指教.
- A1048
给n个正整数,再给一个m,如果n个数中存在a+b=m(a<=b),则输出a,b. 如果有两组以上,则输出a最小的一组. #include<cstdio> #include<al ...
- ubuntu下tensorflow安装
1,安装驱动,cuda,cudnn,参考本人上一篇博客http://www.cnblogs.com/zxg-DL/p/9023601.html 2,安装tensorflow 接下来是关于Tenso ...
- express添加权限拦截
express通过中间件的方式添加权限拦截 示例代码如下 app.get('/logout', checkLogin); app.get('/logout', function(req, res) { ...
- PhpStorm2016.2版本 安装与破解
1.PhpStorm2016简介以及下载地址 1.1.PhpStorm介绍 PhpStorm是一个轻量级且便捷的PHP IDE,其旨在提高用户效率,可深刻理解用户的编码,提供智能代码补全 快速导 ...
- # 2016-2017-2 20155319 《Java程序设计》实验四Android程序开发实验报告
2016-2017-2 20155319 <Java程序设计>实验四Android程序开发实验报告 实验一 实验内容 Android Stuidio的安装测试: 参考<Java和An ...
- 20155322 2016-2017-2 《Java程序设计》 第一周学习总结
20155322 2016-2017-2 <Java程序设计> 第一周学习总结 教材学习内容总结 本周学习内容的主要是: 一.浏览教材,根据自己的理解每章提出一个问题. 在浏览教材后,我提 ...
- tkinter菜单图标,工具栏
所用的图片: import tkinter as tk from tkinter import messagebox, filedialog, simpledialog, colorchooser f ...
- Zabbix学习之路(十)之分布式监控zabbix_proxy及交换机监控
1.Zabbix分布式监控 zabbix proxy 可以代替 zabbix server 检索客户端的数据,然后把数据汇报给 zabbix server,并且在一定程度上分担了zabbix serv ...
- datax源代码编译安装
what is DataX ? DataX是阿里巴巴集团内被广泛使用的离线数据同步工具/平台. 实现包括MySQL,SQL Server,Oracle,PostgreSQL,HDFS,Hive,HBa ...