Poj-2524-Ubiquitous (Religions)

Source: Internet
Author: User
There are n students in a school, m of which share the same religious beliefs,
Ask the maximum number of religions in this school.

Solution: basic and query sets. People with the same faith are classified as a collection,

The number of the last set is the result. Evaluate the number of sets: determine the number of root nodes and use the characteristics of the root node (the parent node is its own)

The Code is as follows:

#include<cstdio>#include<cstring>#include<iostream>using namespace std;int n,m;int f[52014];void init(){    for(int i=1; i<=n; i++)        f[i]=i;}int find(int x){    return f[x]==x?x:find(f[x]);}void unity(int a,int b){    if(find(a)!=find(b))        f[find(a)]=find(b);}int main(){    int ji=1;    while(cin>>n>>m)    {        if(n+m==0)break;        init();        while(m--)        {            int a,b;            cin>>a>>b;            unity(a,b);        }        int ans=0;        for(int i=1; i<=n; i++)            if(f[i]==i)                ans++;        cout<<"Case "<<ji++<<": ";        cout<<ans<<endl;    }    return 0;}


Poj-2524-Ubiquitous (Religions)

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.