【hdu 1213 基础并查集】How Many Tables
来源:互联网 发布:windows 安全配置指南 编辑:程序博客网 时间:2024/06/06 03:03
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 29112 Accepted Submission(s): 14436
Total Submission(s): 29112 Accepted Submission(s): 14436
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
ac代码:
#include <iostream>#include <set>#include <map>#include <stack>#include <cmath>#include <queue>#include <cstdio>#include <bitset>#include <string>#include <vector>#include <iomanip>#include <cstring>#include <algorithm>#include <functional>#define PI acos(-1)#define eps 1e-8#define inf 0x3f3f3f3f#define debug(x) cout<<"---"<<x<<"---"<<endltypedef long long ll;using namespace std;int pre[1010];bool gg[1010];int findd(int x) //查找根节点{ int r=x; while ( pre[r ] != r ) //返回根节点 r r=pre[r ]; int i=x, j ; while( i != r ) //路径压缩 { j = pre[ i ]; // 在改变上级之前用临时变量j记录下他的值 pre[ i ]= r ; //把上级改为根节点 i=j; } return r ;}void join(int x,int y) //判断x y是否连通,//如果已经连通,就不用管了 //如果不连通,就把它们所在的连通分支合并起,{ int fx=findd(x),fy=findd(y); if(fx!=fy) pre[fx ]=fy;}int main(){ int t; cin>>t; while(t--) { int n,m; cin>>n>>m; for(int i=1; i<=n; i++) //初始化 { pre[i]=i; } int a,b,ans=0; for(int i=0; i<m; i++) // 吸收数据 { cin>>a>>b; join(a,b); } memset(gg,0,sizeof(gg)); for(int i=1; i<=n; i++) { int x=findd(i); //标记根节点 gg[x]=1; } for(int i=1; i<=n; i++) { if(gg[i]==1) ans++; } cout<<ans<<endl; } return 0;}
1 0
- 【hdu 1213 基础并查集】How Many Tables
- HDU 1213How Many Tables(基础并查集)
- HDU 1213 How Many Tables 基础并查集★
- 并查集 hdu How Many Tables
- hdu 1213 How Many Tables(并查集~~)
- Hdu 1213How Many Tables 并查集
- 杭电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(并查集练习)
- HDU 1213 How Many Tables 并查集入门
- HDU 1213 How Many Tables 并查集 水~
- HDU 1213 How Many Tables(并查集)
- [ACM] hdu 1213 How Many Tables(并查集)
- [ACM] hdu 1213 How Many Tables(并查集)
- 【并查集】 HDU 1213 How Many Tables
- hdu 1213 How Many Tables(并查集学习)
- Struts2中对数据简单的验证和property标签的用法
- JZOJsenior1395.2017.04.08【NOIP提高组】模拟赛B组 T1字符串还原
- sql trick
- 【KB2999226】api-ms-win-crt-runtime-l1-1-0.dll 丢失解决办法
- Java并发编程笔记
- 【hdu 1213 基础并查集】How Many Tables
- hdu5423 Rikka with Tree
- JavaScript : 零基础打造自己的类库
- 编译链接的过程
- mysql基本操作
- zoj3948——Marjar Cola
- CSDN日报20170412 ——《程序员该如何定位?看这四大方向》
- 图论-双连通分量
- MySql日期查询语句详解