一、题目

Description

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 one number denoting how many people there are in the largest group of friends.

Sample Input

Sample Output

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 10

1 2

9 10

8 9

3

6

二、题目源程序

#include <cstdio>
#include <iostream>
#include <cmath>using namespace std;int parent[30005];
int num[30005];
int n, m;
int find (int x) {return parent[x] == x ? x : find(parent[x]);
}
void init () {int i;for (i = 0; i < n; i ++)parent[i] = i;for (i = 0; i < n; i ++)num[i] = 0;
}
void Union(int a, int b) {int fa = find(a);int fb = find(b);if(fa != fb) {parent[fa] = fb;}
}
int main () {int cases;int a, b;int i;scanf("%d", &cases);while (cases --) {scanf("%d%d", &n, &m);init ();for (i = 0; i < m; i ++) {scanf("%d%d", &a, &b);Union(a, b);}for (i = 0; i < n; i ++) {int x = find(i);num[x] ++;}int max = 0;for (i = 0; i < n; i ++) {if(num[i] > max)max = num[i];}printf("%d\n", max);}return 0;
}

三、解题思路

一个简单的运用并查集的知识的题目。

练习基本的并查集的运用可以用这道题。

转载于:https://www.cnblogs.com/fightfor/p/3871547.html

acm集训训练赛(二)D题【并查集】相关推荐

  1. 2020-12-22 ACM集训一(二维数组与结构体)

    ACM集训一 NEFU 1064 B题:(矩阵外围数字之和) Problem:B Time Limit:1000ms Memory Limit:65536K Description 在军事训练中,战士 ...

  2. ACM题解——训练赛2_D - The Beatles

    ACM题解--训练赛2_D - The Beatles 题目描述 Examples Input 2 3 1 1 Output 1 6 Input 3 2 0 0 Output 1 3 Input 1 ...

  3. ACM算法训练赛——STL(完结)

    STL训练赛 A - JiaoZhu and SC #include <bits/stdc++.h> #define int long long #define rep(i, a, b) ...

  4. 2021-01-25广州大学ACM寒假训练赛解题心得

    https://vjudge.net/contest/419545 目录 A - Airplane AtCoder - abc129_a B - Balance AtCoder - abc129_b ...

  5. C#LeetCode刷题-并查集

    并查集篇 # 题名 刷题 通过率 难度 128 最长连续序列 39.3% 困难 130 被围绕的区域 30.5% 中等 200 岛屿的个数 38.4% 中等 547 朋友圈 45.1% 中等 684 ...

  6. 中医药暑假训练赛三 c题 题解 (多重背包问题)

    原题: Description 主角kirito是使用世界首款完全潜行游戏"刀剑神域(Sword Art Online)"的玩家.曾经很幸运的参与过封闭测试,并买下正式版的kiri ...

  7. QLU—新生训练赛002补题

    I.十进制中的二进制 解题方法:直接把给定范围内的有0和1组成的数暴力输出,然后开个数组把这些数放进去,进而在数组中找要求的范围内符合条件的数就行了(一开始做的时候还以为有什么规律...一直在找规律, ...

  8. 寒假的牛客训练赛1补题

    寒假我太摸鱼了 牛牛最近做了这么一道题: "对于一给定的 n(1≤n≤109)n(1\leq n\leq 10^{9})n(1≤n≤109),计算ϕ(n)\phi(n)ϕ(n), 之中 ϕ( ...

  9. 2014CSU-ACM暑假集训训练赛--七夕专场

    A题目来源:http://codeforces.com/problemset/problem/20/C A题是跟一般的单源最短路径一样,SPFA算法即可,就是多了一个路径的的输出,记录父亲节点就行,不 ...

  10. J - Sum(南京预选赛周末训练赛)补题

    题意:(square-free integer )无平方整数是除了1以外不被其它整数的平方除的数. 一个数n = ab,问你有多少个a,b的组合,a,b都是无平方整数,用f(n)表示,让你求 思路:首 ...

最新文章

  1. 【Windows 逆向】内存地址分析 ( 动态地址 | 静态地址 | 偏移量 )
  2. 安卓开发真机遇到Failed to install Spaceassault.apk on device 'HT1CKV205198': timeout 测试机没有问题...
  3. 存放80000000学生成绩的集合,怎么统计平均分性能高
  4. 用一个程序生成另一个程序_还有另一个报告生成器?
  5. linux 内存强度测试软件,linux下的CPU、内存、IO、网络的压力测试工具与方法介绍...
  6. 基于asp.net的Web开发架构探索(转)
  7. mybatis工作笔记003---Mybatis批量删除deleteByIds的用法
  8. Leetcode c语言-Divide Two Integers
  9. Java4Android视频笔记
  10. WebSphere报错指南
  11. python计算iv值_Python计算IV值的示例讲解
  12. 番茄的随笔4:Clark变换与Park变换
  13. Eclipse集成Zxing实现扫一扫功能
  14. 运维安全操作建议规范手册
  15. 深澜系统服务器架构,S7510E-X结合深澜服务器做Portal无感知认证终端不定时掉线经验案例...
  16. iphone屏幕自定义排版_如何使用小部件和图标自定义iPhone主屏幕
  17. HEVC Tile 编码器-kvazaar
  18. 量子计算(二):为什么需要量子计算
  19. VLOOKUP函数使用及注意事项
  20. 【建议星星】要就来45道Promise面试题一次爽到底(1.1w字用心整理)

热门文章

  1. 20170908算法工程师在线笔试之jd编程思路
  2. python实现将将输入的可约分数化简为不可约分数
  3. python模块学习---glob 文件路径查找
  4. pcl1.8.1在VS2017中编译遇到pop_t找不到标识符的问题的解决办法
  5. C++ concurrent_queue::try_pop 方法
  6. Linux常用监控服务器性能命令 内存 CPU 磁盘
  7. kubernetes视频教程笔记 (10)-资源清单yaml常用字段解释
  8. thinkphp5 insertAll 插入的数据列不对 对应关系不对
  9. JDBC01 mysql和navicat的安装
  10. 2020-07-02