HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213
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
并查集的模板题。
#include<bits/stdc++.h>
using namespace std;
const int maxn=+; int n,m; int par[maxn],ran[maxn];
void init(int l,int r){for(int i=l;i<=r;i++) par[i]=i,ran[i]=;}
int find(int x){return (par[x]==x)?x:(par[x]=find(par[x]));}
void unite(int x,int y)
{
x=find(x), y=find(y);
if(x==y) return;
if(ran[x]<ran[y]) par[x]=y;
else par[y]=x, ran[x]+=(ran[x]==ran[y]);
}
bool isSame(int x,int y){return find(x)==find(y);} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m); init(,n);
for(int i=,a,b;i<=m;i++)
{
scanf("%d%d",&a,&b);
if(!isSame(a,b)) unite(a,b);
} set<int> S;
for(int i=;i<=n;i++) S.insert(find(i)); printf("%d\n",S.size());
}
}
整理一下并查集的两种模板吧:
int par[maxn],ran[maxn];
void init(int l,int r){for(int i=l;i<=r;i++) par[i]=i,ran[i]=;}
int find(int x){return (par[x]==x)?x:(par[x]=find(par[x]));}
void unite(int x,int y)
{
x=find(x), y=find(y);
if(x==y) return;
if(ran[x]<ran[y]) par[x]=y;
else par[y]=x, ran[x]+=(ran[x]==ran[y]);
}
bool isSame(int x,int y){return find(x)==find(y);}
int par[maxn];
void init(int l,int r){for(int i=l;i<=r;i++) par[i]=i;}
int find(int x){return (par[x]==x)?x:(par[x]=find(par[x]));} //这种简单的并查集的合并方式是:
int t1=find(u),t2=find(v);
if(t1!=t2) par[t1]=t2; //这种是把点u所在树并入点v所在树
if(t1!=t2) par[t2]=t1; //这种是把点v所在树并入点u所在树
HDU 1213 - How Many Tables - [并查集模板题]的更多相关文章
- hdu 1213 求连通分量(并查集模板题)
求连通分量 Sample Input2 //T5 3 //n m1 2// u v2 34 5 5 12 5 Sample Output24 # include <iostream> # ...
- HDU 1213 How Many Tables 并查集 水~
http://acm.hdu.edu.cn/showproblem.php?pid=1213 果然是需要我陪跑T T,禽兽工作人员还不让,哼,但还是陪跑了~ 啊,还有呀,明天校运会终于不用去了~耶耶耶 ...
- HDU 1213 How Many Tables(并查集,简单)
题解:1 2,2 3,4 5,是朋友,所以可以坐一起,求最小的桌子数,那就是2个,因为1 2 3坐一桌,4 5坐一桌.简单的并查集应用,但注意题意是从1到n的,所以要减1. 代码: #include ...
- HDU 1213 How Many Tables (并查集,常规)
并查集基本知识看:http://blog.csdn.net/dellaserss/article/details/7724401 题意:假设一张桌子可坐无限多人,小明准备邀请一些朋友来,所有有关系的朋 ...
- HDU 1213 How Many Tables 并查集 寻找不同集合的个数
题目大意:有n个人 m行数据,每行数据给出两个数A B,代表A-B认识,如果A-B B-C认识则A-C认识,认识的人可以做一个桌子,问最少需要多少个桌子. 题目思路:利用并查集对相互认识的人进行集合的 ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- 杭电ACM省赛集训队选拔赛之热身赛-How Many Tables,并查集模板题~~
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- PAT甲题题解-1114. Family Property (25)-(并查集模板题)
题意:给出每个人的家庭成员信息和自己的房产个数与房产总面积,让你统计出每个家庭的人口数.人均房产个数和人均房产面积.第一行输出家庭个数,随后每行输出家庭成员的最小编号.家庭人口数.人均房产个数.人均房 ...
- HDU-1232/NYOJ-608畅通工程,并查集模板题,,水过~~~
畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) http://acm. ...
随机推荐
- Spring boot配置log4j输出日志
1. pom.xml文件中配置parent,版本选定[1.2.5.RELEASE] 关于为什么要选这个版本:我尝试使用[1.4.1.RELEASE],但该版本库里没有[spring-boot-star ...
- 使用CountDownLatch模拟高并发场景
import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java ...
- C#编码习惯谈
1. 避免将多个类放在一个文件里面.2. 一个文件应该只有一个命名空间,避免将多个命名空间放在同一个文件里面.3. 一个文件最好不要超过500行的代码(不包括机器产生的代码).4. 一个方法的 ...
- Hadoop集群三种作业调度算法介绍
Hadoop集群中有三种作业调度算法,分别为FIFO,公平调度算法和计算能力调度算法 先来先服务(FIFO) Hadoop中默认的调度器FIFO,它先按照作业的优先级高低,再按照到达时间的先后选择被执 ...
- Kafka producer介绍
Kafka 0.9版本正式使用Java版本的producer替换了原Scala版本的producer.本文着重讨论新版本producer的设计原理以及基本的使用方法. 新版本Producer 首先明确 ...
- 深入浅出MongoDB应用实战开发
写在前面的话: 这篇文章会有点长,谨此记录自己昨天一整天看完<深入浅出MongoDB应用实战开发>视频时的笔记.只是在开始,得先抛出一个困扰自己很长时间的问题:“带双引号的和不带双引号的j ...
- java如何调用另一个包里面的类
我现在有两个包: 我想在Boss里面实现对Employee的调用, Employee.java: package payroll2; public class Employee { public vo ...
- 《转载》Eclipse项目上传码云
本文转载自http://blog.csdn.net/izzyliao/article/details/53074452 把Eclipse项目上传到码云的步骤: 1.登录码云:新建项目 2.输入项目名: ...
- C# GIF图片的分解以及合成
dll下载的地址 http://www.codeproject.com/Articles/11505/NGif-Animated-GIF-Encoder-for-NET 使用主要调用的是Compo ...
- springboot---->springboot中的格式化(一)
这里面我们简单的学习一下springboot中关于数据格式化的使用.我以为你不是个好人,没想到你连个坏人都不是. springboot中的格式化 我们的测试环境是springboot,一个将字符串格式 ...