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 ...
随机推荐
- C# WebRequest POST上传数据
WebRequest request = WebRequest.Create("http://www.cnsos.net"); // Set the Method property ...
- .NET Core整合log4net以及全局异常捕获实现
在使用log4net之前先安装log4net.这里操作很简单,通过nuget下载并安装log4net很方便.如下图. log4net配置 <?xml version="1.0" ...
- Qt4.7.4下单独编译QtWebkit
最近编译出了Qt4.7.4的嵌入式版本,但没有编译QtWebkit库.在编译一个使用Webkit的工程时出错,而根据工程的需要,要单独编译QtWebkit库. 由于不想再次编译整个的Qt库,于是 ...
- 开源代码分析工具 good
checkstyle - static code analysis tool for JavaPMD - A source code analyzer
- VC6下 try catch 在release下的杯具(默认情况下,要加上throw语句catch才不会被优化掉)
IDE:VC6 今天遇到一个小问题,把我郁闷了好久,××医生的VulEngine不时在wcsstr处发生crash,加了一番强大的参数检查后,再加上了强大的try catch,其实不是很喜欢用try和 ...
- Struts2 学习笔记(概述)
Struts2 学习笔记 2015年3月7日11:02:55 MVC思想 Strust2的MVC对应关系如下: 在MVC三个模块当中,struts2对应关系如下: Model: 负责封装应用的状态,并 ...
- 30442数据操纵语言DML
5.5 SQL的数据操纵功能 5.5.1 数据插入 使用CREATE语句创建的数据表还只是一个“空壳”,表中没有任何数据.利用SQL语言提供的INSERT语句可以完成向数据表插入数据的任务. INSE ...
- 如何打造VUCA时代的敏捷型组织?
王明兰 --原华为.微软创新与转型教练.华为云SaaS产品总监,著名精益&敏捷转型专家 VUCA最早来源于冷战时期,在现代世界意指商业世界越来越不确定性,越来越易变,越来越不可预测,我们已经进 ...
- Spring Boot:实现MyBatis动态数据源
综合概述 在很多具体应用场景中,我们需要用到动态数据源的情况,比如多租户的场景,系统登录时需要根据用户信息切换到用户对应的数据库.又比如业务A要访问A数据库,业务B要访问B数据库等,都可以使用动态数据 ...
- spring boot 2.x 系列 —— spring boot 整合 redis
文章目录 一.说明 1.1 项目结构 1.2 项目主要依赖 二.整合 Redis 2.1 在application.yml 中配置redis数据源 2.2 封装redis基本操作 2.3 redisT ...