zoukankan      html  css  js  c++  java
  • POJ2524 Ubiquitous Religions[简单并查集]

    Ubiquitous Religions
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 17762   Accepted: 8653

    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
    

    Hint

    Huge input, scanf is recommended.

    Source

     
     
     
     
     
     
     
    注意一下在初始化的函数里面,不要溢出了!!!
    code:
     1 #include <iostream>   
     2 #include <iomanip>   
     3 #include <fstream>   
     4 #include <sstream>   
     5 #include <algorithm>   
     6 #include <string>   
     7 #include <set>   
     8 #include <utility>   
     9 #include <queue>   
    10 #include <stack>   
    11 #include <list>   
    12 #include <vector>   
    13 #include <cstdio>   
    14 #include <cstdlib>   
    15 #include <cstring>   
    16 #include <cmath>   
    17 #include <ctime>   
    18 #include <ctype.h> 
    19 using namespace std;
    20 
    21 #define MAXN 50006
    22 
    23 int father[MAXN];
    24 int n,m;
    25 
    26 void makeset()
    27 {
    28     for(int i=0;i<MAXN;i++)
    29         father[i]=i;
    30 }
    31 
    32 int findset(int v)
    33 {
    34     int t1,t2=v;
    35     while(v!=father[v])
    36         v=father[v];
    37     while(t2!=v)
    38     {
    39         t1=father[t2];
    40         father[t2]=v;
    41         t2=t1;
    42     }
    43     return v;
    44 }
    45 
    46 void Union(int a,int b)
    47 {
    48     a=findset(a);
    49     b=findset(b);
    50     if(a!=b)
    51     {
    52         father[a]=b;
    53         n--;
    54     }
    55 }
    56 
    57 int main()
    58 {
    59     int a,b;
    60     int cnt=1;
    61     while(~scanf("%d%d",&n,&m))
    62     {
    63         if(n==0&&m==0)
    64             break;
    65         makeset();
    66         while(m--)
    67         {
    68             scanf("%d%d",&a,&b);
    69             Union(a,b);
    70         }
    71         printf("Case %d: %d\n",cnt++,n);
    72     }
    73     return 0;
    74 }
  • 相关阅读:
    分享jstl实现分页,类似百度分页
    分享git的常用命令
    ubuntu certbot 生成免费泛域名证书
    es创建普通索引以及各种查询
    动态代理
    开闭原则
    单一原则
    单例模式
    设计模式之观察者模式
    SpringBoot集成spring-data-jpa注入Bean失败
  • 原文地址:https://www.cnblogs.com/XBWer/p/2629848.html
Copyright © 2011-2022 走看看