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 ;
}

最新文章

  1. JAVA NIO学习笔记1 - 架构简介
  2. 【BZOJ-2325】道馆之战 树链剖分 + 线段树
  3. IIS7 404 模块 IIS Web Core 通知 MapRequestHandler 处理程序 StaticFile 错误代码 0x80070002
  4. MySQL中函数CONCAT及GROUP_CONCAT
  5. 对于旅游业的手机app的分析
  6. Atitit attilax在自然语言处理领域的成果
  7. LIST_ENTRY
  8. iOS异常捕获
  9. 数学物理学报Offprints and Remuneration
  10. linux直接启动到字符界面或从字符界面启动到图形化界面
  11. Python新手学习基础之数据类型——字符串的切片截取
  12. Linux命令 文件备份归档恢复
  13. [转]python 模块 chardet下载及介绍
  14. cassandra 3.x官方文档(2)---架构解析
  15. git配置ssh秘钥(公钥以及私钥)
  16. struct的匿名用法详解
  17. Coursera, Deep Learning 5, Sequence Models, week2, Natural Language Processing &amp; Word Embeddings
  18. PHP基础架构
  19. vue中遇到的坑!!!!!
  20. vue的生命周期(lifecycle)

热门文章

  1. 梅林路由器 开启ssh key远程登录
  2. C#中比较两个对象的地址是否相同(也是引用计数的问题,和Java一样)
  3. vc++的学习目的
  4. CMake编译如何解决[-Werror,-Wformat-security] 问题
  5. Python连载11-Python中os.path模块简介
  6. 优秀的Restful API应该是什么样的
  7. netcore mvc快速开发系统(菜单,角色,权限[精确到按钮])开源
  8. 深入V8引擎-AST(3)
  9. 一篇文章概括 Java Date Time 的使用
  10. Java类库的源码