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 【并查集】的更多相关文章

  1. UVA - 12232 Exclusive-OR (并查集扩展偏离向量)

    Description You are not given n non-negative integersX0,X1,..., Xn-1 less than220, but they do exist ...

  2. 紫书 习题 11-12 UVa 1665 (并查集维护联通分量)

    这道题要逆向思维 反过来从大到小枚举, 就是在矩阵中一点一点加进去数字,这样比较 好操作, 如果正着做就要一点一点删除数字, 不好做. 我们需要在这个过程中维护联通块的个数, 这里用到了并查集. 首先 ...

  3. 紫书 习题11-11 UVa 1644 (并查集)

    这道题感觉思路非常巧妙, 我是看了别人的博客才想明白的. 这里用到了并查集, 以根节点为中心城市, 然后把边从大到小排序, 每次的当前的边即为容量, 因为是目前的最小值, 然后去算总的容量, 每次选容 ...

  4. Bond UVA - 11354(并查集按秩合并)

    题意: 给你一张无向图,然后有若干组询问,让你输出a->b的最小瓶颈路. 解析: 应该都想过用prime的次小生成树做..但二维数组开不了那么大..所以只能用kruskal了.... #incl ...

  5. UVA 572 油田连通块-并查集解决

    题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...

  6. 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 ...

  7. UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环

    X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...

  8. UVA 11987 - Almost Union-Find(并查集)

    UVA 11987 - Almost Union-Find 题目链接 题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和 思路:并查集,关键在 ...

  9. uva 1493 - Draw a Mess(并查集)

    题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...

  10. UVa 11987 Almost Union-Find(支持删除操作的并查集)

    传送门 Description I hope you know the beautiful Union-Find structure. In this problem, you’re to imple ...

随机推荐

  1. RedHat 7.3+ORACLE 12c RAC 使用udev绑定磁盘

    在RedHat 7中,很多命令发生了改变,其中使用udev对磁盘绑定的命令也发生了变更,不再使用start_udev,而是改为了udevadm,下面具体介绍如何使用udev对磁盘进行绑定,这里对6和7 ...

  2. Windows下 Composer 安装 Thinkphp5 的记录.

    首先安装Composer, 下载地址: https://www.phpcomposer.com/ Windows安装过程及其简单,请自行搜索解决. 接下来Win+R, 启动命令行窗口,以下所有操作都是 ...

  3. delphi多版本安装方法

    delphi tokyo和其他版本并存的安装方法 1.安装delphi 10.2 tokyo 破解后写入D:\Program Files (x86)\Embarcadero\Studio\19.0\b ...

  4. Jetbrains 工具集

    http://www.jetbrains.com/ PRODUCTS IntelliJ IDEA ReSharper WebStorm PhpStorm PyCharm RubyMine AppCod ...

  5. 编译Qt5.0连接MySql5.5数据库的驱动(5.0版本的编译,我记得5.2开始自带了)

    第一步 1.准备好Mysql数据库安装文件,Qt5.0完整的离线安装包,以及Qt5.0的完整的源代码.安装好程序,假设Mysql的安装路径为:C:\MySQL5.5,Qt5.0的安装路径:C:\Qt\ ...

  6. 孟岩:技术路线的选择重要但不具有决定性(什么是核心竞争力?是你独特的个性知识经验组合,正确的态度应该是着重于你要干的事情,然后认真把这件事情做好,然后融会贯通)good

    转自 http://blog.csdn.net/myan/article/details/3247071   孟岩 2008 年的文章,现在看来还是挺有启发, 送给大家,也送给自己. 最近微软在技术上 ...

  7. Awesome Go (http://awesome-go.com/)

    A curated list of awesome Go frameworks, libraries and software. Inspired by awesome-python. Contrib ...

  8. nginx(一) nginx详解

    nginx是一个被广泛使用的集群架构组件,我们有必要对它有足够的了解.下面将先认识nginx:包括应用场景.nginx基本架构.功能特性.并发模型以及配置说明,最后我们再总结下,为什么选择nginx的 ...

  9. Spring之ApplicationContext

    (1)ApplicationContext接口容器 ApplicationContext用于加载Spring的配置文件,在程序中充当“容器”的角色.其实现类有两个.通过Ctrl +T查看: A.配置文 ...

  10. devexpress GridView按条件给行号上色

    private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDra ...