zoukankan      html  css  js  c++  java
  • poj2524 Ubiquitous Religions(并查集)

    Ubiquitous Religions
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 39239   Accepted: 18708

    Description

    There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding out how many different religions students in your university believe in.

    You know that there are n students in your university (0 < n <= 50000). It is infeasible for you to ask every student their religious beliefs. Furthermore, many students are not comfortable expressing their beliefs. One way to avoid these problems is to 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 know what each person believes in, but you can get an idea of the upper bound of how many different religions can be possibly represented on campus. You may assume that each student subscribes to at 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 two integers i and j, specifying that students i and j believe in the same religion. The students are numbered 1 to n. The end of input is specified by a line in which n = m = 0.

    Output

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

    Sample Input

    10 9
    1 2
    1 3
    1 4
    1 5
    1 6
    1 7
    1 8
    1 9
    1 10
    10 4
    2 3
    4 5
    4 8
    5 8
    0 0
    

    Sample Output

    Case 1: 1
    Case 2: 7
    
    分析:并查集模板题。

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int M,N,cas=0;//N个点M条边 
    int pre[50010];//pre[i]=x表示点i的父亲节点为点x 
    void init()//初始化N个元素pre[x]=x,即开始时有N个集合 
    {
        for(int i=0;i<=N;i++) pre[i]=i;
    }
    
    int find(int x)//查询某点所在树的根 
    {
        int root=x;
        while(pre[root]!=root) root=pre[root];
        int i=x,k;
        while(k!=root)//路径压缩,让x到根经过的所有节点直接指向根节点 
        {
            k=pre[i];
            pre[i]=root;
            i=k;
        }
        return root;
    }
    
    int unite(int x,int y)
    {
        int rootx=find(x),rooty=find(y);
        if(rootx!=rooty)//合并 
        {pre[rootx]=pre[rooty];return 1;}
        return 0;
    }
    
    int main()
    {
        while(scanf("%d%d",&N,&M))
        {
            init();
            if(!M&&!N) break;
            int x,y,ans=N;
            while(M--)
            {
                scanf("%d%d",&x,&y);
                ans-=unite(x,y);
            }
            printf("Case %d: %d
    ",++cas,ans);
        }
        return 0;
    }
    View Code


  • 相关阅读:
    Stack frame
    How a stack frame works 栈帧的要素与构建步骤
    Symbol Table
    函数调用栈实例研究
    崩溃日志的实例
    符号表的作用和地位
    A C compiler that parses this code will contain at least the following symbol table entries
    前复权是从今天的价格倒推 后复权是从上市价格前推 不复权就是原始K线。
    Thinkphp5+PHPExcel实现批量上传表格数据功能
    PHP 开发API接口签名验证
  • 原文地址:https://www.cnblogs.com/ACRykl/p/9103982.html
Copyright © 2011-2022 走看看