并查集-水题
来源:互联网 发布:python 字典转列表 编辑:程序博客网 时间:2024/05/22 08:07
问题 I: How Many Tables
时间限制: 1 Sec 内存限制: 32 MB提交: 14 解决: 8[提交][状态][讨论版]
题目描述
Today is Ignatius' birthday. He invitesa lot of friends. Now it's dinner time. Ignatius wants to know how many tableshe 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 isthat if I tell you A knows B, and B knows C, that means A, B, C know eachother, so they can stay in one table.
For example: If I tell you A knows B, Bknows C, and D knows E, so A, B, C can stay in one table, and D, E have to stayin the other one. So Ignatius needs 2 tables at least.
输入
The input starts with an integerT(1<=T<=25) which indicate the number of test cases. Then T test casesfollow. Each test case starts with two integers N and M(1<=N,M<=1000). Nindicates the number of friends, the friends are marked from 1 to N. Then Mlines follow. Each line consists of two integers A and B(A!=B), that meansfriend A and friend B know each other. There will be a blank line between twocases.
输出
For each test case, just output how manytables Ignatius needs at least. Do NOT print any blanks.
样例输入
2
6 4
1 2
2 3
3 4
1 4
8 10
1 2
2 3
5 6
7 5
4 6
3 6
6 7
2 5
2 4
4 3
样例输出
3
2
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>#define N 1100int f[N]={0}, n, m, k;void init(){int i;for(i=1; i<=n; i++)f[i]=i;}int getf(int v){if(f[v]==v)return v;else{f[v]=getf(f[v]);return f[v];}}void merge(int v, int u){int t1, t2;t1=getf(v);t2=getf(u);if(t1 != t2){f[t2]=t1;}}int main(){int i, x, y, sum, t;scanf("%d", &t);while(t--){scanf("%d %d", &n, &m);init();for(i=1; i<=m;i++){scanf("%d %d", &x, &y);merge(x, y);}for(i=1, sum=0; i<= n; i++){if(f[i]==i)sum++;}printf("%d\n", sum);}return 0;}
- 并查集 水题
- 并查集-水题
- HDU3938 并查集 并查集
- 并查集(集并查)
- HDU1232 并查集<并>
- X-Plosives +uvalive+并查集+水题
- HDOJ-----2120并查集(水题)
- 并查集
- 数据结构-并查集
- 并查集
- 并查集!
- 并查集
- 并查集
- 并查集
- 并查集
- 并查集总结
- 并查集学习
- 并查集
- 几种基本的数字正则表达式[转]
- 买书
- PHP, Python, Node.js 哪个比较适合写爬虫?
- Android ,在争议中逃离 Linux 内核的 GPL 约束
- classpath和classpath*和配置文件路径的关系
- 并查集-水题
- hadoop-MapReduce
- Python基础入门(十)- 布尔类型条件判断与循环
- 对链表各种操作的实现(C语言)
- Web前端面试笔试题——null和undefined有什么区别么?
- 如何在Unity中对程序进行 Android 真机断点调试
- HDU 1757 A Simple Math Problem [矩阵快速幂]
- 最低通行费
- 十分钟学会Fiddler