HDOJ 1213 How Many Tables
来源:互联网 发布:小鸣单车 知乎 编辑:程序博客网 时间:2024/05/16 18:04
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 <stdio.h>int rank[1002], father[1002];int find(int x){if(father[x] != x)father[x] = find(father[x]);return father[x];}void Union(int x, int y){int fx = find(x), fy = find(y);if(fx == fy)return ;if(rank[fx]>rank[fy]){father[fy] = fx;}else{if(rank[fx] == rank[fy])rank[fy]++;father[fx] = fy;}}int main(){int i = 0, t = 0, n = 0, m = 0, a = 0, b = 0, count = 0;scanf("%d",&t);while(t--){scanf("%d %d",&n,&m);for(i = 1; i<=n; i++){rank[i] = 0; father[i] = i;}for(i = 0; i<m; i++){scanf("%d %d",&a,&b);Union(a,b);}count = 0;for(i = 1; i<=n; i++)if(father[i] == i)count++;printf("%d\n",count);}return 0;}
- 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
- HDOJ-1213-How Many Tables
- hdoj 1213 How Many Tables
- HDOJ 1213 How Many Tables
- HDOJ How Many Tables
- ACM HDOJ 1213 (How Many Tables )
- HDOJ-1213 How Many Tables(包含题意)
- 线程的概念
- 读《白班程序员 VS. 夜猫子程序员》有感
- Linux下LCD自动关闭解决方法
- Nutch 1.0配置与运行
- linux下安装gcc交叉编译工具及其“浮点数例外”
- HDOJ 1213 How Many Tables
- 线程状态和调度
- The Tao Of Programming —— 编程之道
- Ubuntu11.10 下安装 jdk
- 程序员之路──关于代码风格
- if……elseif……else……end if 的用法
- Linux的iptables做代理服务器和防火墙详解
- Android广播机制(两种注册方法)与中断广播
- vm下RedHat Linux5汉化包安装