zoukankan      html  css  js  c++  java
  • POJ 1611

    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

    鸡你太美病毒( Chicken you so beautiful), 是一种原因不明的流行性病毒。从最近一段时间起,学蔡徐坤打篮球成为新一代校园毒品,听到鸡你太美这首歌或者看到蔡徐坤打篮球的人很有可能就会感染这种病毒。由于它传染性很强( 只要你周围有人唱鸡你太美或者学蔡徐坤打篮球,你很有可能也会开始唱或者模仿,并开始劝你的同学一起唱或模仿)它开始被认为是全球威胁。为了减少传播给别人的机会, 最好的策略是隔离可能的患者。
    学校里有许多学生团体。同一组的学生经常彼此相通,一个学生可以同时加入几个小组。为了防止鸡你太美病毒的传播, CCUTSOFT算法与程序设计协会收集了所有学生团体的成员名单。他们的标准操作程序如下:
    一旦一组中有一个可能的患者, 组内的所有成员就都是可能的患者。
    为了遏制这种病毒的传播,我们需要找到所有的患者。现在知道编号为0的小吉成员(感染源,因其有吉这个字,被蔡徐坤说太美,所以率先被感染)已经得了鸡你太美病毒,请你设计程序 发现所有可能的患者数量

    Input
    输入文件包含多组数据。
    对于每组测试数据:
    第一行为两个整数n和m, 其中n是学生的数量, m是团体的数量。0 < n <= 30000,0 <= m <= 500。
    每个学生编号是一个0到n-1之间的整数,一开始只有0号的小吉被视为患者。
    紧随其后的是团体的成员列表,每组一行。
    每一行有一个整数k,代表成员数量。之后,有k个整数代表这个群体的学生。一行中的所有整数由至少一个空格隔开。
    n = m = 0表示输入结束,不需要处理。
    Output
    对于每组测试数据, 一行输出一个正整数,即可能的患者数量。
    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

    题目大意:

    多组输入,每行输入 n 和 m,输入以0 0 结束。表示有 n 个学生和m个团队,接下来是m行,每一行输入一个数s,表示这个团队的人数,后面跟着团队中每个学生的编号。0号默认已经感染了病毒,如果在一个团队中,有一个人感染了病毒,那么他所在的团队都感染了病毒,输出可能感染病毒的人数。

    解题思路:

    基础并查集,用并查集的思路解题,默认0号已经感染了病毒,对于每个团队,默认将所有人和团队的第一个人并起来,如果其中出现了感染者,则把1号并入0号,说明这个团队所有人都是感染者了。最后遍历团队,只要根节点是0的 ans++,输出ans,AC代码:

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int _max = 3e4+500;
    int f[_max],n,m;
    int find(int x)//寻根+路径压缩
    {
    	return f[x]==x?x:f[x]=find(f[x]);
    }
    void merge(int x,int y)//合并的过程,y合并到x。
    {
    	f[find(y)]=find(x);
    }
    void init()//每次记得初始化
    {
    	for(int i=0;i<=n;i++)
    	  f[i]=i;
    }
    int main()
    {
    	ios::sync_with_stdio(false);
    	while(cin>>n>>m&&(n||m))
    	{
    		init();
    		while(m--)
    		{
    			int s,t;
    			cin>>s;//团队人数
    			for(int i=1;i<=s;i++)
    			{
    				int a;
    				cin>>a;
    				if(i==1)//临时变量存储第一个的编号
    				  t=a;
    				else if(!find(a))//如果有感染者则把第一个人并到0号
    				  merge(0,t);
    				else//其他情况则把团队中的人全并到第一个人
    				  merge(t,a);
    			}
    		}
    		int ans=0;
    		for(int i=0;i<=n;i++)
    		if(!find(i))//如果根节点是0号
    		    ans++;
    		cout<<ans<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    CF1270H. Number of Components
    NOI Online Round2划水记
    uoj#247. 【Rujia Liu's Present 7】Mysterious Space Station口胡
    mysql习题
    MySQL基础
    python网络编程(进程与多线程)
    xshell连接虚拟机Ubuntu问题
    python来写打飞机
    timeit模块
    python常用模块
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294248.html
Copyright © 2011-2022 走看看