zoukankan      html  css  js  c++  java
  • The Suspects

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others. 
    In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP). 
    Once a member in a group is a suspect, all members in the group are suspects. 
    However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.

    Input

    The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space. 
    A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.

    Output

    For each case, output the number of suspects in one line.

    Sample Input

    100 4
    2 1 2
    5 10 13 11 12 14
    2 0 1
    2 99 2
    200 2
    1 5
    5 1 2 3 4 5
    1 0
    0 0

    Sample Output

    4
    1
    1

    题目大意 : 在每个团队中,有一个嫌疑人,那么整个团队的人都将成为嫌疑人,问总共有多少个嫌疑人。

    题目分析 : 在之前的并查集的介绍中,其实已经讲到了这种题目的做法,如果知道了的话,这就是一道很水的题了。那么我就在这里简单的阐述一下。我们要得到相关联的队伍

    人数的多少,我们可以简单的用个数组记录下来,只有有关联就让根节点的值加1。然后输出这个根节点的值就好了。有没有觉得这个跟我们选择哪个子树添加到根树的方法有点像,

    其实是一样的,标记每一个子树的大小,小的树添加到大的树上,再记录合成树的大小。没错就是这样。

    题目收获 : 树的大小的利用。

    这里这个主要代码 :
    void unionSet(int x,int y)
    {
        x=cha(x),y=cha(y);
        if(x==y) return ;
        if(pa1[x]>pa1[y])
        {
            pa[y]=x;
            pa1[x]+=pa1[y];
            //cout <<"***:"<<" X:"<< x << " " <<"Y:"<< y << " " << pa1[x] <<endl;
        }
        else
        {
            pa[x]=y;
            pa1[y]+=pa1[x];
            //cout <<"---:"<<" X:"<< x << " " <<"Y:"<< y << " " << pa1[y] <<endl;
        }
    
    }
  • 相关阅读:
    以用户名注册来分析三种Action获取数据的方式
    Struts2中的OGNL详解 《转》
    Module 'null' not found异常解决办法
    struts标签<logic:iterate>的用法
    struts2的核心和工作原理 <转>
    jstl标签怎么实现分页中下一页
    forward 和redirect
    forward 和redirect的区别
    今天早上起来就想着要问问龙虎有圆通没
    昨天晚上回来弄了两个皮蛋吃
  • 原文地址:https://www.cnblogs.com/7750-13/p/7297639.html
Copyright © 2011-2022 走看看