UVA 10608 Friends

Source: Internet
Author: User

Original question:
There is a town with N citizens. It is known this some pairs of people is friends. According to the famous saying that "The Friends of my friends is my friends, too" it follows that if A and B is friends and B and C is friends then A and C is friends, too.
Your task is to count how many people there be 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 was the number of town ' s citizens (1≤n≤30000) and M is the number of pairs of people (0≤m≤500000), which was known to be friends. Each of the following M lines consists of both integers A and B (1≤a≤n, 1≤b≤n, a̸= b) which describe that A and b Is 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 is in the LA Rgest 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

English:
A city has n inhabitants, these residents have friends relationship, a friend of a friend can also count as friends, finally asked you this n people, the most friends of the community has how many people.

#include <bits/stdc++.h> using namespace std;
int father[30001];
int rank[30001];
    int Find (int x) {if (x==father[x]) return x;
Return Father[x]=find (Father[x]);
    } void Merge (int x,int y) {x=find (x);
    Y=find (y);
    if (x==y) return;
        if (Rank[x]<=rank[y]) {father[x]=y;
    RANK[Y]+=RANK[X];
        } else {father[y]=x;
    Rank[x]+=rank[y];
        }} void init (int n) {for (int i=1;i<=n;i++) {father[i]=i;
    Rank[i]=1;
    }} int main () {Ios::sync_with_stdio (false);
    int n,m,t;
    cin>>t;
        while (t--) {cin>>n>>m;
        Init (n);
            for (int i=1;i<=m;i++) {int A, B;
            cin>>a>>b;
        Merge (A, b);
        } int ans=-1;
        for (int i=1;i<=n;i++) {Ans=max (ans,rank[i]);
    } cout<<ans<<endl;
} return 0;



 }

Answer:

Naked and check set, in rank among the number of friends to retain the first person, the merger when the number of friends to add to another person can.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.