HDU1213How Many Tables(基础并查集)
HDU1213How Many Tables
Problem Description
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
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
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

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=20000;
int pre[maxn],height[maxn];
void init_set(int n){
for (int i = 1; i <= n; i++){
pre[i]=i;
}
memset(height,0,sizeof(height));
} int find_set(int x){
return x==pre[x]?x:pre[x]=find_set(pre[x]);
}
void union_set(int x,int y){
x= find_set(x);
y= find_set(y);
if(x==y)return;
if(height[x]==height[y]){
height[x]=height[x]+1;
pre[y]=x;
}else{
if(height[x]<height[y]) pre[x]=y;
else{
pre[y]=x;
}
}
} int main(){
int T;
cin>>T;
int n,m,a,b;
while (T--){
cin>>n>>m;
init_set(n);
int sum=0;
while (m--){
cin>>a>>b;
union_set(a,b);
}
for (int i = 1; i <=n; i++)
{
if(i==pre[i])sum++;
}
cout<<sum<<endl;
}
}
HDU1213How Many Tables(基础并查集)的更多相关文章
- 并查集:HDU1213-How Many Tables(并查集最简单的应用)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- hdu 1829 基础并查集,查同性恋
A Bug's Life Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- 【HDU1231】How Many Tables(并查集基础题)
什么也不用说,并查集裸题,直接盲敲即可. #include <iostream> #include <cstring> #include <cstdlib> #in ...
- HDU1213:How Many Tables(并查集)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)
http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...
- HDU 1213 How Many Tables (并查集)
How Many Tables 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/C Description Today is Ig ...
- HDU1213 How Many Tables (并查集)
题目大意: 有一个人要过生日了,请他的朋友来吃饭,但是他的朋友互相认识的才能坐在一起,朋友的编号从1 ~ n,输入的两个数代表着这两个人互相认识(如果1和2认识,2和3认识,那么1和3也就认识了).问 ...
- 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 ...
随机推荐
- EL表达式和JSTL标签
什么是 EL 表达式,EL 表达式的作用? EL 表达式的全称是:Expression Language.是表达式语言. EL 表达式的什么作用:EL 表达式主要是代替 jsp 页面中的表达式脚本在 ...
- WPF自定义控件一:StackPanel 控件轮播
实现效果 带定时器的轮播图 using引用 using System.Windows; using System.Windows.Controls; using System.Windows.Mark ...
- 慕慕生鲜上线&&腾讯云服务器配置准备
1.购买服务器并配置环境 1.1 购买 618购买了腾讯云服务器三年最低配置(1核2G 1Mbps 50G云盘),一时激动忘记了购买前领优惠券,痛失25元. 1.2 环境配置 系统是 CentOS L ...
- 【笔记】初探KNN算法(3)
KNN算法(3) 测试算法的目的就是为了帮助我们选择一个更好的模型 训练数据集,测试数据集方面 一般来说,我们训练得到的模型直接在真实的环境中使用 这就导致了一些问题 如果模型很差,未经改进就应用在现 ...
- 【笔记】求数据的对应主成分PCA(第一主成分)
求数据的第一主成分 (在notebook中) 将包加载好,再创建出一个虚拟的测试用例,生成的X有两个特征,特征一为0到100之间随机分布,共一百个样本,对于特征二,其和特征一有一个基本的线性关系(为什 ...
- ffmpeg第6篇:滤镜语法
前言 哈哈,回来继续填坑了,前段时间较忙没时间写,现在继续~ 简介 滤镜是ffmpeg的一个很强大的功能,它支持许多有用的视频处理功能,常见的滤镜如:缩放.旋转.水印.裁剪等 一个比较经典的滤镜使用方 ...
- Java:学习什么是多线程
线程是什么 进程是对CPU的抽象,而线程更细化了进程的运行流程 先看一下这个图 线程和进程的关系有 进程中就是线程在执行,所有(主)线程执行完了进程也就结束了 多个线程从1秒钟是同时运行完成,从1纳秒 ...
- hystrix熔断机制修改配置
0.注意 0.1.如果使用command 的 execute( )方法的话,其实在一个for循环,执行多次,其实每个的执行顺序并不是固定的,如果你想固定,需要使用queue circuit break ...
- python下 conda命令手册
0.说明: 对于tensorflow配合keras使用,因为linux服务器没有root权限,所以目前最高可用版本是 1.6.0,否则就会报错某些 so找不到 conda install -n xu ...
- Thread类的常用方法----多线程基础练习
创建多线程程序的第一种方式----继承Thread类 常用API 构造方法 public Thread() :分配一个新的线程对象. public Thread(String name) :分配一个指 ...