• Ubiquitous Religions(并查集)


    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
    
    这题我忘了并查集怎么写了,在网上找了一个经典的写法,几乎和模板一样,大家看看吧
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 using namespace std;
     6 const int maxn=50005;
     7 int pre[maxn];
     8 int find(int v)
     9 {
    10     return pre[v]==v?v:find(pre[v]);
    11     cout<<pre[v]<<endl;
    12 }
    13 void join(int a,int b)
    14 {
    15     int f1=find(a),f2=find(b);
    16     if(f1!=f2)
    17         pre[f1]=f2;
    18 }
    19 int main()
    20 {
    21     int n,m;
    22     int count;
    23     for(count=1;; count++)
    24     {
    25         cin>>n>>m;
    26         if(n==0&&m==0)
    27             break;
    28         for(int i=1; i<=n; i++)
    29             pre[i]=i;
    30         int max=n;
    31         for(int i=1; i<=m; i++)
    32         {
    33             int a,b;
    34             cin>>a>>b;
    35             if(find(a)!=find(b))
    36                 max=max-1;
    37             join(a,b);
    38         }
    39         printf("Case %d: ",count);
    40         printf("%d
    ",max);
    41     }
    42     return 0;
    43 }
    View Code
     
  • 相关阅读:
    如何防止多个人同时编辑文件
    通过Word实现表单套打
    偏前端
    偏前端
    偏前端
    偏前端 -webpack打包之(安装webpack)
    偏前端
    偏前端
    偏前端
    偏前端--之小白学习本地存储与cookie
  • 原文地址:https://www.cnblogs.com/kongkaikai/p/3241817.html
Copyright © 2020-2023  润新知