HDOJ 1213 How Many Tables——并查集
来源:互联网 发布:css js 执行环境 编辑:程序博客网 时间:2024/05/18 20:32
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12867 Accepted Submission(s): 6297
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
Author
Ignatius.L
简单并查集,毫无疑问....
#include <iostream>#include <cstring>using namespace std;int s[1010];int nfind(int x){ int r=x,q; while(x!=s[x]) { x=s[x]; } while(s[r]!=x) { q=s[r]; s[r]=x; r=q; } return x;}void cz(int x,int y){ int fx=nfind(x); int fy=nfind(y); if(fx!=fy) { s[fx]=fy; }}int main(){ int n,m,i,x,y,t,js=0; while(cin>>t) { while(t--) { js=0; cin>>n>>m; for(i=1; i<=n; i++) { s[i]=i; } for(i=0; i<m; i++) { cin>>x>>y; cz(x,y); } for(i=1; i<=n; i++) { if(s[i]==i) { js++; } } cout<<js<<endl; } } return 0;}
0 0
- HDOJ 1213 How Many Tables——并查集
- hdoj How Many Tables【并查集】
- 并查集——HDOJ 1213How Many Tables解题报告
- HDOJ--1213--How Many Tables【并查集】
- Hdoj 1213 How Many Tables 【并查集】
- hdoj-1213-How Many Tables【并查集】
- HDOJ 1213 How Many Tables(并查集)
- HDOJ 1213 How Many Tables(基础并查集)
- hdoj 1213 How Many Tables 【并查集】
- HDOJ 1213 How Many Tables(并查集)
- HDOJ 1213 how many tables (并查集)
- hdoj 1213 How Many Tables【并查集】
- HDOJ 1213 How Many Tables【并查集】
- HDOJ 1213 How Many Tables(并查集)
- HDOJ 1213 How Many Tables(并查集)
- HDU1213——How Many Tables(并查集)
- 并查集——How Many Tables
- HDOJ 题目1213 How Many Tables(并查集基础)
- Stanford机器学习---第6讲. 怎样选择机器学习方法、系统
- Stanford机器学习---第7讲. 机器学习系统设计
- ID3与C4.5的区别
- 有符号数和无符号数
- vim常用命令分类汇总
- HDOJ 1213 How Many Tables——并查集
- hdu 1874 畅通工程续(图论:两点之间最短路-弗洛伊德算法 || 贝尔曼福特算法)
- Stanford机器学习---第8讲. 支持向量机SVM
- uefi引导装win8.1
- 判断dataGridView是否重复值
- 码农创业记(一)
- 设计模式---工厂方法模式
- Stanford机器学习---第9讲. 聚类
- Stanford机器学习---第10讲. 数据降维