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"