CSU-ACM暑假集训基础组训练赛(2) D - Problem D
来源:互联网 发布:萧山中学网络课程 编辑:程序博客网 时间:2024/05/21 13:58
Description
Problem I
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 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
Problem source: Bulgarian National Olympiad in Informatics 2003
Problem submitter: Ivaylo Riskov
Problem solution: Ivaylo Riskov
并查集的基础题,代码如下:
#include <iostream>
#include<cstdio>
#include <algorithm>
using namespace std;
const int maxn=30000+10;
int fa[maxn],num[maxn],ans;
int find(int u)
{
return fa[u]==u?u:fa[u]=find(fa[u]);
}
int Union(int u,int v)
{
int a=find(u);
int b=find(v);
if(a!=b)
{
fa[a]=b;
num[a]+=num[b];
num[b]=num[a];
ans=max(num[b],ans);
}
}
int main()
{
int t,n,m,i,j,a,b;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
fa[i]=i;
num[i]=1;
}
ans=0;
for(i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
Union(a,b);
}
printf("%d\n",ans);
}
return 0;
}
总结:
并查集+维护num数组。最基础的并查集了估计。
- CSU-ACM暑假集训基础组训练赛(2) D - Problem D
- CSU-ACM暑假集训基础组训练赛(2) B - Problem B
- CSU-ACM暑假集训基础组训练赛(1) A - Problem A
- CSU-ACM暑假集训基础组训练赛(1) B - Problem B
- CSU-ACM暑假集训基础组训练赛(5-1) B - Problem B
- CSU-ACM暑假集训基础组训练赛(5-1) C - Problem C
- CSU-ACM暑假集训基础组训练赛(5-1) A - Problem A
- CSU-ACM暑假集训基础组七夕专场 D - Problem D
- CSU-ACM2017暑假集训比赛2 D
- 2013暑假集训B组训练赛第一场
- 2013暑假集训B组训练赛第二场
- csu-acm2017暑假集训2-二分搜索D
- CSU-ACM2017暑假集训2-二分搜索 D
- CSU-ACM2017暑假集训比赛3D
- CSU-ACM暑假集训基础组七夕专场 F - Problem F
- csu Problem D: Long Long
- 2016ACM暑假集训 - Prime Ring Problem
- 暑假集训第三周周三赛 STL D - 产生冠军
- CF 451 C Predict Outcome of the Game(数学题)
- UVa 10723 - Cyborg Genes
- 理解RESTful架构
- google IP
- 快逸报表连接access数据库
- CSU-ACM暑假集训基础组训练赛(2) D - Problem D
- #ifdef,#else,#if,#endif
- SQL语句的各种连接查询
- Useful Reference Books
- Eclipse中SVN的安装步骤(两种)和使用方法
- 10.7 Combination Sum
- NSArray排序
- 树结构应用之MySQL索引背后的数据结构及算法原理
- Linux usb gadget configfs用法