2019-01-29 [POJ-1611],

[POJ-1611]

include

include

using namespace std;
int gen[30005];int rankk[30005];
int find(int a)
{
if (a != gen[a])
gen[a] = find(gen[a]);
return gen[a];
}

int main()
{
int n,m;
while (cin >> n >> m && (n != 0 || m != 0))
{
int a, b, x, y;
for (int i = 0; i < n; i++)
{

        gen[i] = i;
        rankk[i] = 0;
    }


    while (m--)
    {
        cin >> x; x--;
        cin >> a;
        while (x--)
        {
            cin >> b;
            a = find(a);
            b = find(b);
            if (rankk[a] > rankk[b])
            {
                gen[b] = a;
            }
            else
            {
                gen[a] = b;
                if (rankk[a] == rankk[b])
                    rankk[b]++;
            }
            a = b;

        }
    }
    int he = 0, z = find(0);
    for (int i = 0; i < n; i++)
    {
        if (find(i) == z) he++; 
    }
    cout << he<

}

你可能感兴趣的:(2019-01-29 [POJ-1611],)