UVA---10608-Friends(并查集)

2 篇文章 0 订阅

B - Friends(8.4.1)
Crawling in process... Crawling failed Time Limit:3000MS    Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

 

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 <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;
int ss[30001];
int set[30001];
int mmax=0;

int set_find(int p)
{
    if(set[p]<0)return p;
    return set[p]=set_find(set[p]);
}
void join(int a,int b)
{
    a=set_find(a);
    b=set_find(b);
    if(a!=b)
    {
        set[a]=b;
        ss[b]+=ss[a];
        mmax=(ss[b]>mmax?ss[b]:mmax);
    }
}
int main()
{
    int x,y;
    int T,n,i,m;
    cin>>T;
    while(T--)
    {

        memset(set,-1,sizeof(set));
        mmax=1;
        cin>>n>>m;
        for(i=0; i<=n; ++i)
            ss[i]=1;
        for(i=0; i<m; ++i)
        {
            cin>>x>>y;
            join(x,y);
        }
        cout<<mmax<<endl;
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值