hdu 1213 简单的并查集
来源:互联网 发布:iphone7虚拟定位软件 编辑:程序博客网 时间:2024/05/21 07:46
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16684 Accepted Submission(s): 8177
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.
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
25 31 22 34 55 12 5
Sample Output
24解析:基本的并查集,套模板就行。#include <stdlib.h>#include <iostream>#include <algorithm>using namespace std;const int M = 1005;int fam[M];int t,n,m,a,b;void ufset( ){ for( int i=0 ; i<M ; i++ ) fam[i] = i;}int find( int x ){ if(fam[x]==x)<span style="white-space:pre"></span>//while(fam[x]!=x) x=fam[x]; 非递归法 return fam[x]; return fam[x]= find( fam[x] ); //压缩路径}void merge( int a , int b ){ int x,y; x = find( a ); y = find( b ); if( x!=y ){ fam[x] = y; }}int main(){ cin>>t; while( t-- ){ cin>>n>>m; ufset( ); for( int i=0 ; i<m ; i++ ){ cin>>a>>b; merge( a,b ); } int sum = n; for( int i=1 ; i<=n ; i++ ) if( fam[i] != i ) sum--; cout<<sum<<endl; } return 0;}
0 0
- hdu 1213 简单的并查集
- hdu 1213 How Many Tables(简单的并查集)
- hdu 1213简单并查集
- HDU 1272小希的迷宫(简单并查集)
- hdu 1863(畅通工程)(简单的并查集,模板)
- hdu 1272 小希的迷宫(简单并查集)
- 并查集的简单应用——HDU
- hdu 3047 简单的带权并查集
- HDU 1272 小希的迷宫 (简单并查集)
- HDU 小希的迷宫 (简单并查集)
- 并查集的应用:hdu 1213
- HDU - 1213 How Many Tables (简单并查集)
- hdu 1213 How Many Tables(UFS 简单并查集)
- HDU 1213 How Many Tables(并查集,简单)
- HDU:1213 How Many Tables(简单并查集)
- HDU 1213 How Many Tables? (简单并查集)
- HDU 1213 How Many Tables(简单并查集)
- HDU 1213 How Many Tables【简单并查集】
- zoj 1148 The Game 一个晚上终于AC!
- nyoj349 poj1094 Sorting It All Out(拓扑排序)
- C语言移位运算符精度问题
- 浅谈Android Handler
- KEIL MDK输出map文件分析
- hdu 1213 简单的并查集
- P122,15
- 【C语言】判断一个字符串是否为回文字符串
- Android SQLite数据库之三,使用特定方法操作SQLite数据库
- Linux/Unix下pid文件作用浅析
- nefu-1035 数位统计
- 浅析Java中的final关键字
- Android学习 - ContentObserver监听
- 算法导论笔记:09中位数和顺序统计量