POJ 2524 Ubiquitous Religions (Simple and check set, three ways)

Source: Internet
Author: User

Ubiquitous Religions
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 30791 Accepted: 14928

Description

There is, many different religions in the world today that it's difficult to keep track of them all. You is interested in finding-many different religions students in your university believe in.

You know that there is n students in your university (0 < n <= 50000). It is infeasible for your to ask every student their religious beliefs. Furthermore, many students is not comfortable expressing their beliefs. One-to-avoid these problems is-ask m (0 <= m <= N (n-1)/2) pairs of students and ask them whether they believe In the same religion (e.g they may know if they both attend the same church). From this data, you may not be know what is believes in, but can get a idea of the the upper bound of how many diff Erent religions can is possibly represented on campus. You may assume the student subscribes to the most one religion.

Input

The input consists of a number of cases. Each case starts with a line specifying the integers n and M. The next m lines each consists of both integers I and j, specifying that students I and J believe in the same religion. The students is numbered 1 to N. The end of input is specified by a line in which n = m = 0.

Output

For each test case, print to a single line the case number (starting with 1) followed by the maximum number of different R Eligions the students in the university believe in.

Sample Input

10 91 21 31 41 51 61 71 81 91 1010 42 34 54 85 80 0

Sample Output

Case 1:1case 2:7

Hint

Huge input, scanf is recommended.

Source

Alberta Collegiate Programming Contest 2003.10.18
original title link: http://poj.org/problem?id=2524
Simple and check the set, ask how many connected branches.Simple and check the topic of connecting branch, now briefly summarize:The first few questions about and check the set are to find the number of connected branches or at least a few additional edges to form a unicom map.There are four kinds of topic codes, and there are two main categories, each of which is available in two ways:method One: Default initial value fa[x]==-1;method Two: Default initial value fa[x]==x;
There are two ways in which the bind () function can be used in each way, so there are four of codes for this type of topic, and the individual prefers the way they choose.The following code one or two uses method one, the code three uses method two.
AC Code 1:
#include <iostream> #include <cstring>using namespace std;const int maxn=50000+10;int fa[maxn];int Find (int x) {    if (fa[x]==-1)        return x;    Return Fa[x]=find (Fa[x]);} int Bind (int x,int y) {    int fx=find (x);    int Fy=find (y);    if (fx!=fy)    {        fa[fx]=fy;        return 1;    }    return 0;} int main () {    int n,m,x,y;    int kase=0;    while (cin>>n>>m,n,m)    {        int ans=n;        memset (fa,-1,sizeof (FA));        while (m--)        {            cin>>x>>y;            Ans-=bind (x, y);        }        cout<< "Case" <<++kase<< ":" <<ans<<endl;    }

AC Code 2:
#include <iostream> #include <cstring>using namespace std;const int maxn =50000+5;int fa[maxn];int Find (int x {    if (fa[x]==-1)        return x;    Return Fa[x]=find (Fa[x]);} void Bind (int x,int y) {    int fx=find (x);    int Fy=find (y);    if (fx!=fy)        fa[fx]=fy;} int main () {    int n,m,x,y;    int kase=0;    while (cin>>n>>m,n,m)    {        memset (fa,-1,sizeof (FA));        while (m--)        {            cin>>x>>y;            Bind (x, y);        }        int ans=0;        for (int i=1;i<=n;i++)            if (fa[i]==-1)                ans++;        cout<< "Case" <<++kase<< ":" <<ans<<endl;    }    return 0;}

AC Code 3:
#include <iostream> #include <cstring>using namespace std;const int maxn =50000+5;int fa[maxn];int Find (int x {    if (fa[x]==x)        return x;    Return Fa[x]=find (Fa[x]);} void Bind (int x,int y) {    int fx=find (x);    int Fy=find (y);    if (fx!=fy)        fa[fx]=fy;    return;} int main () {    int n,m,x,y;    int kase=0;    while (Cin>>n>>m,n,m)    {for        (int i=1;i<=n;i++)            fa[i]=i;        while (m--)        {            cin>>x>>y;            Bind (x, y);        }        int ans=0;        for (int i=1;i<=n;i++)        {            int t=find (i);            if (fa[i]==i)                ans++;        }        cout<< "Case" <<++kase<< ":" <<ans<<endl;    }



POJ 2524 Ubiquitous Religions (Simple and check set, three ways)

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.