zoukankan      html  css  js  c++  java
  • POJ1611 && POJ2524 并查集入门

    The Suspects
    Time Limit: 1000MS   Memory Limit: 20000K
    Total Submissions: 28293   Accepted: 13787

    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

    题意是已知编号为0的同学已经染上了SARS病毒,然后跟他一个组的同学假设也染上了SARS病毒,问最后有多少个同学染上了SARS病毒。

    并查集模板题,一开始以为要用什么方法找0的孩子数量,后来发现枚举时间也不会卡。

    代码:

    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <string>
    #include <cstring>
    #include <set>
    #pragma warning(disable:4996)
    using namespace std;
    
    int n,m;
    int pre[30002];
    
    int findpre(int x)
    {
    	while(x!=pre[x])
    	{
    		x=pre[x];
    	}
    	return x;
    }
    
    void union_set(int x,int y)
    {
    	int pre_x = findpre(x);
    	int pre_y = findpre(y);
    
    	if(pre_x == pre_y)
    		return;
    	else if(pre_x>pre_y)
    	{
    		int temp = pre_x;
    		pre_x = pre_y;
    		pre_y = temp;
    	}
    
    	pre[pre_y]=pre_x;
    }
    
    int main()
    {
    	//freopen("i.txt","r",stdin);
    	//freopen("o.txt","w",stdout);
    
    	int i,j,num,temp1,temp;
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		if(n==0&&m==0)
    		{
    			break;
    		}
    		for(i=0;i<n;i++)
    		{
    			pre[i]=i;
    		}
    		for(i=1;i<=m;i++)
    		{
    			scanf("%d",&num);
    			for(j=1;j<=num;j++)
    			{
    				if(j==1)
    				{
    					scanf("%d",&temp1);
    					continue;
    				}
    				else
    				{
    					scanf("%d",&temp);
    					union_set(temp,temp1);
    				}
    			}
    		}
    		int ans=0;
    		for(i=0;i<n;i++)
    		{
    			if(findpre(i)==0)
    			{
    				ans++;
    			}
    		}
    		printf("%d
    ",ans);
    	}
    
    	//system("pause");
    	return 0;
    }
    


    POJ2524题意是查询最终的集合数量,找pre,查有多少不同即可。

    代码:

    #include <iostream>  
    #include <algorithm>  
    #include <cmath>  
    #include <vector>  
    #include <string>  
    #include <cstring>  
    #include <set>
    #pragma warning(disable:4996)  
    using namespace std;
    
    int n,m;
    int pre[50005];
    set <int> res;
    
    int findpre(int x)  
    {  
        while(x!=pre[x])  
        {  
            x=pre[x];  
        }  
        return x;  
    }  
      
    void union_set(int x,int y)  
    {  
        int pre_x = findpre(x);  
        int pre_y = findpre(y);  
      
        if(pre_x == pre_y)  
            return;  
        else if(pre_x>pre_y)  
        {  
            int temp = pre_x;  
            pre_x = pre_y;  
            pre_y = temp;  
        }  
      
        pre[pre_y]=pre_x;  
    } 
    
    int main()
    {
    	//freopen("i.txt","r",stdin);
    	//freopen("o.txt","w",stdout);
    
    	int i,j,temp1,temp2,test_num;
    
    	test_num=1;
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		if(n==0 && m==0)
    		{
    			break;
    		}
    
    		printf("Case %d: ",test_num++);
    		
    		for(i=1;i<=n;i++)
    		{
    			pre[i]=i;
    		}
    		for(i=1;i<=m;i++)
    		{
    			scanf("%d%d",&temp1,&temp2);
    			union_set(temp1,temp2);
    		}
    		res.clear();
    		for(i=1;i<=n;i++)
    		{
    			res.insert(findpre(i));
    		}
    		printf("%d
    ",res.size());
    	}
    
    	//system("pause");
    	return 0;
    }



    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    欧拉筛,线性筛,洛谷P2158仪仗队
    树形DP和状压DP和背包DP
    洛谷P1144最短路计数题解
    洛谷P1373小a和uim大逃离题解
    LCA
    108. Convert Sorted Array to Binary Search Tree
    230. Kth Smallest Element in a BST
    94. Binary Tree Inorder Traversal
    144. Binary Tree Preorder Traversal
    236. Lowest Common Ancestor of a Binary Tree
  • 原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4899542.html
Copyright © 2011-2022 走看看