• POJ 1611 The Suspects【并查集】


    解题思路:一共给出 n个人,m组,接下来是m组数据,每一组开头是该组共有的人 num,则接下来输入的num个数,这些数是一组的

    又因为最开始只有编号为0的人携带有病毒,且只有同一组的人会相互传染,问最后一共有多少人携带有病毒,则问题简化为求含有0那一组一共有多少个人。

    反思:最开始把对应每输入一组的数据用数组来放,发现 int a[30001]编译不过;

    然后就用一个while循环来输入,还有最开始得不到正确结果是因为把最后的判断条件写成了

    if(find(i)==0)

    sum++;

    当时以为含有0的组的根节点就是0,其实0的根节点也不一定就是0,还应该再找一下0的根节点,应该改成

    if(find(i)==find(0))

    sum++;

    Description

    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
    #include<stdio.h>
    int pre[30005];
    int find(int root)
    {
    	if(root!=pre[root])
    	{
    		pre[root]=find(pre[root]);
    	}
    	return pre[root];
    }
    void unionroot(int x,int y)
    {
    	int root1,root2;
    	root1=find(x);
    	root2=find(y);
    	if(root1!=root2)
    	{
    		pre[root1]=root2;
    	}
    }
    
    int main()
    {
    	int n,m,num,k,t,i,p;
    	int x,y;
    	int sum;
    	while(scanf("%d %d",&n,&m)!=EOF&&(n||m))
    	{
    		sum=0;
    		for(i=0;i<n;i++)
    			pre[i]=i;
    		while(m--)
    		{
    			scanf("%d",&num);
    			scanf("%d",&x);
    			k=x;
    			if(num==1)
    			{
    				pre[x]=find(x);
    			}
    			else
    			{
    				p=num-1;
    				while(p--)
    				{
    					scanf("%d",&y);
    					t=y;
    					unionroot(k,y);
    					k=t;
    				}
    			}
    		}
    
    		for(i=0;i<n;i++)
    		{
    			if(find(i)==find(0))
    				sum++;
    
    		}
    		printf("%d
    ",sum);
    	}
    }
    
  • 相关阅读:
    User Agent
    ASP.NET状态管理 APPlication,Session,Cookie和ViewStat用法
    HttpHandler
    转 此版本的 SQL Server 不支持用户实例登录标志。该连接将关闭“的解决
    跨服务器访问SQLSERVER
    sql字符串函数
    使用aspnet_regsql.exe 创建ASPState数据库,用来保存session会话
    SQL中CONVERT转化函数的用法
    Sql时间函数
    JS创建对象的几种方法
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4128342.html
Copyright © 2020-2023  润新知