九度 题目1445:How Many Tables
来源:互联网 发布:触摸交互式软件 编辑:程序博客网 时间:2024/06/05 10:08
九度 题目1445:How Many Tables
原题OJ链接:http://ac.jobdu.com/problem.php?pid=1445
题目描述:
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.
样例输入:
25 31 22 34 55 12 5
样例输出:
24
解题思路:
并查集,寻找连通分量的个数。
源代码:
#include<iostream>#include<cstring>using namespace std;#define MAX_N 1005int Tree[MAX_N];int findRoot(int x){ if(Tree[x]==-1) return x; else{ int tmp=findRoot(Tree[x]); Tree[x]=tmp; return tmp; }}int main(){ int T,N,M,A,B; while(cin>>T){ for(int i=0;i<T;i++){ cin>>N>>M; memset(Tree,-1,sizeof(Tree)); for(int j=0;j<M;j++){ cin>>A>>B; A=findRoot(A); B=findRoot(B); if(A!=B){ Tree[A]=B; } } int count=0; for(int j=1;j<=N;j++){ if(Tree[j]==-1) count++; } cout<<count<<endl; } } return 0;}
- 【九度】题目1445:How Many Tables
- 九度 题目1445:How Many Tables
- 九度 题目1445:How Many Tables
- 九度oj 题目1445:How Many Tables
- 【九度OJ】题目1445:How Many Tables 解题报告
- 题目1445:How Many Tables 九度OJ
- 九度OJ-1445:How Many Tables
- 题目1445:How Many Tables
- 题目1445:How Many Tables
- 题目1445:How Many Tables
- 题目69:How Many Tables
- hdu 题目 题目1213 How Many Tables
- How Many Tables
- HDOJ How Many Tables
- hduoj1213:How Many Tables
- hdu How Many Tables
- HDU1213:How Many Tables
- HDU1213--How Many Tables
- C#WinForm的 Window 消息大全
- Android Dagger2入门详解(三)
- 爬格子呀4-8
- 进制转换
- java war 打包、解压命令
- 九度 题目1445:How Many Tables
- StatckOverflowError和OutofMemorryError的区别
- 这个配置决定了每个Input Split 的最小值
- 简介分布式---任意软件变为“分布式”
- Android animation补间动画第二种写法
- java文件读取
- HDU 6198 number number number 题解
- 人脸识别 传统Haar与CNN比较
- 函数式编程初探