CodeForces-Learning Languages ages
# Include
# Include
# Include
/* Check and check the set for this question. Pay special attention to the situation where everyone does not know the language. At this time, the result is n. */Int father [105]; int lang [105] [105]; int flag [105]; int set [105]; int find (int x) {int r = x; while (father [r]! = R) r = father [r]; // compress int I = x, j; while (father [I]! = R) {j = father [I]; father [I] = r; I = j;} return r;} void merge (int x, int y) {int fx = find (x); int fy = find (y); if (fx! = Fy) father [fx] = fy;} int main () {freopen ("0input.txt", "r", stdin); int n, m, k, p, res, flag1 = 0; while (scanf ("% d", & n, & m )! = EOF) {// initialize flag1 = 0; memset (flag, 0, sizeof (flag); memset (lang, 0, sizeof (lang); memset (set, 0, sizeof (set); for (int I = 1; I <= n; ++ I) father [I] = I; // read data for (int I = 1; I <= n; ++ I) {scanf ("% d", & k); if (k! = 0) {flag [I] = 1; for (int j = 1; j <= k; ++ j) {scanf ("% d", & p ); lang [p] [I] = 1 ;}}// construct and query the for (int I = 1; I <= m ;++ I) {int j, fa, fb; for (j = 1; j <= n; ++ j) {if (lang [I] [j]) {fa = find (j ); // printf ("fa: % d \ n", fa); break ;}}for (j = j + 1; j <= n; ++ j) {if (lang [I] [j]) {fb = find (j); // printf ("fa: % dfb: % d \ n", fa, fb ); merge (fa, fb) ;}}for (int I = 1; I <= n; ++ I) {// printf ("flag: % d ", flag [I]); if (Flag [I]) {flag1 = 1; break ;}} if (! Flag1) {printf ("% d \ n", n);} else {// Statistics divide employees into several types of int fc; for (int I = 1; I <= n; ++ I) {fc = find (I); set [fc] ++;} res = 0; for (int I = 1; I <= n; ++ I) {if (set [I]> = 1) res ++;} printf ("% d \ n", res-1 );}} return 0 ;}