hdu 1213 (基础并查集)
来源:互联网 发布:黄药师软件使用方法 编辑:程序博客网 时间:2024/06/18 14:03
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.
25 31 22 34 55 12 5
24
#include<stdio.h>#include<string.h>using namespace std;int num[10003];int pre[10003];int Find(int x){ if (pre[x] != x) return pre[x] = Find(pre[x]); else return x;}void Merage(int x, int y){ int X = Find(x); int Y = Find(y); if (X != Y) pre[X] = Y; return;}void init(){ for(int i = 1; i <= 1000; i++) { pre[i] = i; } return;}int main(){ int T; scanf("%d", &T); while (T--) { int n; int m; scanf("%d%d", &n, &m); init(); for(int i = 1; i <= m; i++) { int x, y; scanf("%d%d", &x, &y); Merage(x, y); } int res = 0; memset(num, 0, sizeof(num)); for(int i = 1; i <= n; i++) { int Tmp = Find(i); if(num[Tmp] == 0) { res++; num[Tmp]++; } } printf("%d\n", res); }}
阅读全文
0 0
- hdu 1213 (基础并查集)
- hdu 1213 基础并查集
- 【hdu 1213 基础并查集】How Many Tables
- HDU 1213How Many Tables(基础并查集)
- HDU 1213 How Many Tables 基础并查集★
- HDU 5606_基础并查集
- hdu 1272 (基础并查集★ )
- hdu 1213 并查集
- hdu 1213 并查集
- hdu 1213并查集
- HDU-1213并查集
- hdu 1213 并查集
- HDU 1213 并查集
- hdu 1213 并查集
- HDU 1213 并查集
- hdu 1213 并查集
- hdu 1213并查集
- hdu 1213 并查集
- 谷歌翻译SDK (Google Translate SDK)的使用
- VNPY初步分析
- 解析json并插入数据库
- 近实时运算的利器---presto在公司实践
- 使用Dom4j解析XML
- hdu 1213 (基础并查集)
- 【NOIP模拟】Competing Souls
- mongodb读写性能分析之(写)
- 三维变换
- Quartz使用总结
- JAVA BASE64Encoder使用,导入sun.misc.BASE64Encoder包
- 并发容器
- Android 资源大全中文版
- Oracle实例名,服务名等概念区别与联系