B - Friends(8.4.1)

B - Friends(8.4.1)
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit Status

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.

 

 

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 <iostream>
#include <cstring>
using namespace std;
const int maxn=30005;
int set[maxn],s[maxn];
int set_find(int p)
{
    if(set[p]<0)
        return p;
    return


        set[p]=set_find(set[p]);
}
int main()
{
    int t,n,m,a,b,x;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        int p,q;
        memset(set,255,sizeof(set));
        memset(s,0,sizeof(s));
        for(int i=0;i<m;i++)
        {
            cin>>a>>b;
            q=set_find(a);
            p=set_find(b);
            if(q!=p)
            {
                set[p]=q;
            }
        }
        for(int k=1;k<=n;k++)
        {
            x=set_find(k);
            s[x]++;
        }
        int max1=-1;
        for(int j=1;j<=n;j++)
            if(max1<s[j])
                max1=s[j];
        cout<<max1<<endl;


    }
    return 0;


}

优化后的代码:

#include <iostream>
#include <cstring>
using namespace std;
const int maxn=30005;
int set[maxn],s[maxn];
int set_find(int p)
{
    if(set[p]<0)
        return p;
    return


        set[p]=set_find(set[p]);
}
int main()
{
    int t,n,m,a,b,x;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        int p,q;
        memset(set,255,sizeof(set));
        //memset(s,0,sizeof(s));
for(int l=0;l<=n;l++)
s[l]=1;
int max1=-1;
        for(int i=0;i<m;i++)
        {
            cin>>a>>b;
            q=set_find(a);
            p=set_find(b);
            if(q!=p)
            {
                set[p]=q;
s[q]+=s[p];
max1=(s[q]>max1)?s[q]:max1;
            }
        }
            
        cout<<max1<<endl;


    }
    return 0;


}

你可能感兴趣的:(B - Friends(8.4.1))