UVA-10608 Friends 【并查集】
There is a town with N citizens. It is known that some pairs of people are friends. According to the
famous saying that “The friends of my friends are my friends, too” it follows that if A and B are friends
and B and C are friends then A and C are friends, too.
Your task is to count how many people there are in the largest group of friends.
Input
Input consists of several datasets. The first line of the input consists of a line with the number of test
cases to follow.
The first line of each dataset contains tho numbers N and M, where N is the number of town’s
citizens (1 ≤ N ≤ 30000) and M is the number of pairs of people (0 ≤ M ≤ 500000), which are known
to be friends. Each of the following M lines consists of two integers A and B (1 ≤ A ≤ N, 1 ≤ B ≤ N,
A != B) which describe that A and B are friends. There could be repetitions among the given pairs.
Output
The output for each test case should contain (on a line by itself) one number denoting how many people
there are in the largest group of friends on a line by itself.
Sample Input
2
3 2
1 2
2 3
10 12
1 2
3 1
3 4
5 4
3 5
4 6
5 2
2 1
7 1
1 2
9 10
8 9
Sample Output
3
7
注意: 数据范围M为零的情况。
#include <bits/stdc++.h>
using namespace std; const int maxn = ;
int N, M;
int ans;
int p[maxn];
int cnt[maxn]; int ffind(int x)
{
return p[x] == x? x : p[x] = ffind(p[x]);
} void init(int n)
{
ans = ;
for(int i = ; i <= n ; i++)
p[i] = i, cnt[i] = ; } void uunion(int u, int v)
{
int a = ffind(u);
int b = ffind(v);
if(a != b)
p[b] = a, cnt[a] += cnt[b];
ans = max(cnt[a],ans);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.ini", "r",stdin);
#endif // ONLINE_JUDGE
int T;
cin >> T;
while(T--)
{
cin >> N >> M;
init(N);
int a,b;
for(int i = ; i < M; i++)
{
cin >> a >> b;
uunion(a,b);
} cout << ans << endl;
}
return ;
}
UVA-10608 Friends 【并查集】的更多相关文章
- UVA - 12232 Exclusive-OR (并查集扩展偏离向量)
Description You are not given n non-negative integersX0,X1,..., Xn-1 less than220, but they do exist ...
- 紫书 习题 11-12 UVa 1665 (并查集维护联通分量)
这道题要逆向思维 反过来从大到小枚举, 就是在矩阵中一点一点加进去数字,这样比较 好操作, 如果正着做就要一点一点删除数字, 不好做. 我们需要在这个过程中维护联通块的个数, 这里用到了并查集. 首先 ...
- 紫书 习题11-11 UVa 1644 (并查集)
这道题感觉思路非常巧妙, 我是看了别人的博客才想明白的. 这里用到了并查集, 以根节点为中心城市, 然后把边从大到小排序, 每次的当前的边即为容量, 因为是目前的最小值, 然后去算总的容量, 每次选容 ...
- Bond UVA - 11354(并查集按秩合并)
题意: 给你一张无向图,然后有若干组询问,让你输出a->b的最小瓶颈路. 解析: 应该都想过用prime的次小生成树做..但二维数组开不了那么大..所以只能用kruskal了.... #incl ...
- UVA 572 油田连通块-并查集解决
题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...
- UVA 12232 - Exclusive-OR(带权并查集)
UVA 12232 - Exclusive-OR 题目链接 题意:有n个数字.一開始值都不知道,每次给定一个操作,I a v表示确认a值为v,I a b v,表示确认a^b = v,Q k a1 a2 ...
- UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环
X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...
- UVA 11987 - Almost Union-Find(并查集)
UVA 11987 - Almost Union-Find 题目链接 题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和 思路:并查集,关键在 ...
- uva 1493 - Draw a Mess(并查集)
题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...
- UVa 11987 Almost Union-Find(支持删除操作的并查集)
传送门 Description I hope you know the beautiful Union-Find structure. In this problem, you’re to imple ...
随机推荐
- mysql 的用户权限
查看MySQL的用户权限 show grants for "username"@'host'; 添加新用户 允许本地IP访问localhost:127.0.0.1 use mysq ...
- 解决xp越来越慢的办法(其中有些自动备份的功能)
1.减少磁盘空间占用2.终止不常用的系统服务3.安全问题4.另外一些技巧 首先问一下,你是不是很想激活XP,不...准确的说你是不是想在ms的站上能够升级.如果答案是肯定的话,那我们就先来探讨一下安装 ...
- select Demo
#include <iostream> #include <WinSock2.h> using namespace std; #pragma comment(lib, &quo ...
- vmware centos7虚拟机克隆系统如何修改网卡设置?
1.克隆虚拟机,克隆前需关闭虚拟机2.克隆之后的网卡问题解决,其中需要修改HWADDR和UUID /etc/sysconfig/network-scripts/ifcfg-ens32 uuid获取 ...
- Spring Boot:实现MyBatis动态创建表
综合概述 在有些应用场景中,我们会有需要动态创建和操作表的需求.比如因为单表数据存储量太大而采取分表存储的情况,又或者是按日期生成日志表存储系统日志等等.这个时候就需要我们动态的生成和操作数据库表了. ...
- Python一基本数据类型(dict)
一. 字典的简单介绍 字典(dict)是python中唯一的一个映射类型.他是以{ }括起来的键值对组成. 在dict中key是 唯一的. 在保存的时候, 根据key来计算出一个内存地址. 然后 ...
- 【oracle】Oracle整理笔记
原博主总结了很多技能和小技巧,本人觉的非常实用,转载记录下: Oracle学习笔记整理手册 作者:@smileNicky 链接:https://blog.csdn.net/u014427391/art ...
- 【前端工具】页面加载获取url param
例如跳转进入一个页面: https://mp.csdn.net/postedit/74766644?name=catalina&flag=1 函数: function getParam(par ...
- TCP/IP 第四、五章
1, 2, 整个arp请求的过程. 3,arp -a 获取arp高速缓存.一般arp高速缓存存活时间20分钟,不完整的表项设置为3分钟.因为机器的ip地址可能发生改变. 4, 5,arp一般是操作系统 ...
- C# 死锁 TaskCompletionSource
在异步转同步时,使用不当容易造成死锁(程序卡死) 看如下案例: 有一个异步方法 private static async Task TestAsync() { Debug.WriteLine(&quo ...