UVA10608
来源:互联网 发布:淘宝的促销活动有哪些 编辑:程序博客网 时间:2024/04/29 23:32
题意:计算出所有集合中元素最多的个数
思路:只需在Union函数中更新最大值就可以了
#include <iostream>#include <cstdio>#include <cstring>#define N 30000using namespace std;int cas, n, m, ans;int father[N], cnt[N]; void init(int n) { for (int i = 1; i <= n; i++) { father[i] = i; cnt[i] = 1; }}int getFather(int x) { return x == father[x] ? x : father[x] = getFather(father[x]);}void Union(int a, int b) { int pa = getFather(a); int pb = getFather(b); if (pa != pb) { father[pb] = pa; cnt[pa] += cnt[pb]; } if (cnt[pa] > ans) ans = cnt[pa];}int main() { scanf("%d", &cas); while (cas--) { scanf("%d %d", &n, &m); ans = 0; init(n); int a, b; for (int i = 0; i < m; i++) { scanf("%d %d", &a, &b); Union(a, b); } printf("%d\n", ans); } return 0;}
0 0
- uva10608
- UVA10608
- uva10608
- uva10608
- UVa10608 Friends
- UVa10608 Friends
- 并查集+uva10608
- uva10608(并查集)
- uva10608 并查集
- uva10608 (并查集)
- uva10608(基础的并查集)
- 创建对象和使用方法以及属性
- Java的回调函数和观察者模式的区别
- 一些能提高MAC用户效率的方法
- 调整数组顺序使奇数位于偶数前面
- Android:Layout_weight的深刻理解
- UVA10608
- 最有收藏价值的太茅31年典藏手工捆沙酒
- nyoj-123 士兵杀敌(四) (树状数组插线问点—模板题)
- Configuring SNMP Support
- 《金钱不能买什么》读书笔记
- plsql developer显示数据乱码
- 基于@AspectJ注解的纯POJO Spring 2.x Aop
- SharePoint2010 Managed Metadata Service托管元数据服务
- 堆排序