Code:
[Cpp]
# Include <stdio. h>
Int father [30001];
Int count [30001];
Int I, m, n, first, a, B;
Void setfather (int n) // initialization, set the respective fahter to itself
{
For (I = 0; I <n; I ++)
{
Father [I] = I;
Count [I] = 1;
}
}
Int findfather (int I)
{
If (I! = Father [I])
Father [I] = findfather (father [I]);
Return father [I];
}
Void Untion (int a, int B)
{
Int m1 = findfather ();
Int m2 = findfather (B );
If (m1 = m2)
Return;
If (count [m1]> = count [m2]) // if not equal, the value is assigned to the larger
{
Father [m2] = m1;
Count [m1] = count [m2] + count [m1];
}
Else
{
Father [m1] = m2;
Count [m2] = count [m1] + count [m2];
}
}
Int main ()
{
While (scanf ("% d", & n, & m )! = EOF & n> 0)
{
If (n> = 0 & n <= 30000 & m> = 0 & m <= 500)
{
Setfather (n );
While (m> 0)
{
Scanf ("% d", & a, & first );
For (I = 0; I <A-1; I ++)
{
Scanf ("% d", & B );
Untion (first, B );
}
M --;
}
Printf ("% d \ n", count [findfather (0)]);
}
}
Return 0;
}
# Include <stdio. h>
Int father [30001];
Int count [30001];
Int I, m, n, first, a, B;
Void setfather (int n) // initialization, set the respective fahter to itself
{
For (I = 0; I <n; I ++)
{
Father [I] = I;
Count [I] = 1;
}
}
Int findfather (int I)
{
If (I! = Father [I])
Father [I] = findfather (father [I]);
Return father [I];
}
Void Untion (int a, int B)
{
Int m1 = findfather ();
Int m2 = findfather (B );
If (m1 = m2)
Return;
If (count [m1]> = count [m2]) // if not equal, the value is assigned to the larger
{
Father [m2] = m1;
Count [m1] = count [m2] + count [m1];
}
Else
{
Father [m1] = m2;
Count [m2] = count [m1] + count [m2];
}
}
Int main ()
{
While (scanf ("% d", & n, & m )! = EOF & n> 0)
{
If (n> = 0 & n <= 30000 & m> = 0 & m <= 500)
{
Setfather (n );
While (m> 0)
{
Scanf ("% d", & a, & first );
For (I = 0; I <A-1; I ++)
{
Scanf ("% d", & B );
Untion (first, B );
}
M --;
}
Printf ("% d \ n", count [findfather (0)]);
}
}
Return 0;
}