n个人,每个人会一些语言,两个人只要有会一门相同的语言就可以交流,问为了让这n个人都交流,至少还得学多少门语言

先根据n个人之间他们会的语言,建边

再dfs找出有多少个联通块ans,再加ans-1条边就可以让他们连通

注意特判一下每个人都会0门语言的情况

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std; const int maxn = ;
int g[maxn][maxn],a[maxn][maxn];
int vis[maxn];
int n,m; void dfs(int u){
vis[u] = ;
for(int v = ;v <= n;v++){
if(!vis[v] && g[u][v]) dfs(v);
}
} int main(){
int ans = ;
scanf("%d %d",&n,&m);
memset(g,,sizeof(g));
memset(a,,sizeof(a));
int tot = ;
for(int u = ;u <= n;u++){
int k;
scanf("%d",&k);
if(k == ) tot++;
for(int i = ;i < k;i++){
int v;
scanf("%d",&v);
a[u][v] = ;
}
} if(tot == n) printf("%d\n",tot);
else {
for(int u = ;u <= n;u++){
for(int v = u+;v <= n;v++){
for(int k = ;k <= m;k++){
if(a[u][k] && a[v][k]) g[u][v] = g[v][u] = ;
}
}
} for(int i = ;i <= n;i++) {
if(!vis[i]){
ans++;
dfs(i);
}
}
printf("%d\n",ans-);
}
return ;
}

codeforces 277 A Learning Languages 【DFS 】的更多相关文章

  1. Codeforces 653B Bear and Compressing【DFS】

    题目链接: http://codeforces.com/problemset/problem/653/B 题意: 要求你构造一个长度为n的字符串使得通过使用m个操作,最终获得字符a.已知第i个操作将字 ...

  2. Codeforces 500A - New Year Transportation【DFS】

    题意:给出n个数,终点t 从第i点能够跳到i+a[i],问能否到达终点 #include<iostream> #include<cstdio> #include<cstr ...

  3. 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】

    目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...

  4. Kattis - glitchbot 【DFS】

    Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...

  5. HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))

    度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】

    [CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...

  7. 【dfs】codeforces Journey

    http://codeforces.com/contest/839/problem/C [AC] #include<iostream> #include<cstdio> #in ...

  8. 【DFS】codeforces B. Sagheer, the Hausmeister

    http://codeforces.com/contest/812/problem/B [题意] 有一个n*m的棋盘,每个小格子有0或1两种状态,现在要把所有的1都变成0,问最少的步数是多少?初始位置 ...

  9. 机器学习(Machine Learning)&深度学习(Deep Learning)资料【转】

    转自:机器学习(Machine Learning)&深度学习(Deep Learning)资料 <Brief History of Machine Learning> 介绍:这是一 ...

随机推荐

  1. RabbitMQ学习之Flow Control

    当RabbitMQ发布消息速度快于消费速度或者系统资源不足时,RabbitMQ将降低或阻断发布消息速度,以免服务器资源饱满而宕机,可以通过rabbitmqctl和web管理页面查看连接的状态为flow ...

  2. win系统如何在桌面显示我的电脑

    如果是在Windows Server 2012本地控制台下,直接按Win(键盘上的微软徽标键)+R,输入: rundll32.exe shell32.dll,Control_RunDLL desk.c ...

  3. jQuery删除元素

    remove() - 删除被选元素(及其子元素) empty() - 从被选元素中删除子元素 $("#div1").remove();删除被选元素及其子元素. $("#d ...

  4. 2104 -- K-th Number

    Description You are working for Macrohard company in data structures department. After failing your ...

  5. 嵌入式 ThriftServer in Spark

    我们知道在Spark中可以通过start-thriftServer.sh 来启动ThriftServer,之后并可以通过beeline或者JDBC来连接并执行Spark SQL.在一般的Spark应用 ...

  6. Problem 13

    Problem 13 # Problem_13 """ Work out the first ten digits of the sum of the following ...

  7. (5)全局异常捕捉【从零开始学Spring Boot】

    在一个项目中的异常我们我们都会统一进行处理的,那么如何进行统一进行处理呢? 新建一个类GlobalDefaultExceptionHandler, 在class注解上@ControllerAdvice ...

  8. Windows下通过FTP自动上传和下载动态文件名

    某个项目中每天会生成一个以文件名+日期.rar文件,如bcpdata2012-08-31.rar文件,动态的部分为日期部分,在windows环境变量中用 %date:~0,10% 表示,这个文件生成后 ...

  9. Cache index coloring for virtual-address dynamic allocators

    A method for managing a memory, including obtaining a number of indices and a cache line size of a c ...

  10. php RSA 简单实现

    这是rsa_private_key.pem-----BEGIN PRIVATE KEY----- MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC ...