The Suspects————Peking University OJ

Link to the question
http://poj.org/problem?id=1611. Traditional
thinking
and collection + maintenance of a size array storing each number. How many infections should be found in this question, that is, find those related to 0, When reading each group of data, it is necessary to find the minimum value of each group of data, and use it as a representative.
c++ code

#include<algorithm>
#include<iostream>
#include<stdio.h>
using namespace std;
const int N=30005;
int n,m;
int size[N];
int p[N];
int q[N];
int find(int x)
{
    
    
    if(p[x]!=x) p[x]=find(p[x]);
    return p[x]; 
}
void init()
{
    
    
    for (int i = 0; i < n; i++)
    {
    
    
        p[i] = i;
        size[i] = 1;
    }
}
int main()
{
    
    
    cin >> n >> m;
    while(n||m)
    {
    
    
        if(n==0&&m==0)
        {
    
    
            break;
        }
        if(m==0)
        {
    
    
            printf("1\n");
        }
        else
        {
    
    
        init();
        while(m--)
        {
    
       
            int min1=N-1;
            int x;
            scanf("%d",&x);
            for (int k = 1; k <= x;k++)
            {
    
    
                cin >> q[k];
                min1 = min(min1, q[k]);
            }
            int b=find(min1);
            for (int k = 1; k <= x;k++)
            {
    
    
                if(q[k]==min1)
                    continue;
                int y = find(q[k]);
                if(y!=b)
                    p[y] = b, size[b] += size[y];
            }
        }
        cout << size[find(0)] << endl;
        }
        cin >> n >> m;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/Star_Platinum14/article/details/111937637