Codeforces 277 A Learning Languages "DFS"

Source: Internet
Author: User

n individuals, each person will have some language, two people can communicate in the same language, and ask how many languages, at least, must be learned in order for the N-people to communicate.

First, according to the language of the N people they will, build the side

Dfs find out how many of the unicom block ans, plus ans-1 edge can let them connect

Pay attention to the case of everyone, 0 languages.

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <vector>6 using namespacestd;7 8 Const intMAXN = the;9 intG[MAXN][MAXN],A[MAXN][MAXN];Ten intVIS[MAXN]; One intn,m; A  - voidDfsintu) { -Vis[u] =1; the      for(intv =1; v <= n;v++){ -         if(!vis[v] &&G[u][v]) Dfs (v); -     } - } +  - intMain () { +     intAns =0; Ascanf"%d%d",&n,&m); atMemset (G,0,sizeof(g)); -Memset (A,0,sizeof(a)); -     inttot =0; -      for(intU =1; u <= n;u++){ -         intK; -scanf"%d",&k); in         if(k = =0) tot++; -          for(inti =0; I < k;i++){ to             intv; +scanf"%d",&v); -A[U][V] =1; the         } *     } $     Panax Notoginseng     if(tot = n) printf ("%d\n", tot); -     Else { the              for(intU =1; u <= n;u++){ +              for(intv = u+1; v <= n;v++){ A              for(intK =1; k <= m;k++){ the                 if(A[u][k] && a[v][k]) g[u][v] = G[v][u] =1; +             } -             } $       } $      -      for(inti =1; I <= n;i++) { -         if(!Vis[i]) { theans++; - DFS (i);Wuyi         } the     } -printf"%d\n", ans-1); Wu     } -     return 0; About}
View Code

Codeforces 277 A Learning Languages "DFS"

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.