CodeForces-Learning Languages ages

Source: Internet
Author: User

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 ;}
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.