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. 洛谷P1993 小K的农场_差分约束_dfs跑SPFA

    Code: #include<cstdio> #include<queue> using namespace std; const int N=10000+233; const ...

  2. node——通过express模拟Apache实现静态资源托管

    1.express中处理静态资源的函数 创建一个app.js作为入口文件,创建一个public文件夹作为静态资源文件夹 var app=express();var fn=express.static( ...

  3. Vue学习之路第十二篇:为页面元素设置内联样式

    1.有了上一篇的基础,接下来理解内联样式的设置会更简单一点,先看正常的css内联样式: <dvi id="app"> <p style="font-si ...

  4. Jenkins学习总结(6)——了解DevOps的前世今生

    DevOps是什么?从哪里来? DevOps的概念 DevOps一词的来自于Development和Operations的组合,突出重视软件开发人员和运维人员的沟通合作,通过自动化流程来使得软件构建. ...

  5. Elasticsearch---基于scroll技术滚动搜索大量数据

    如果一次性要查出来比如10万条数据,那么性能会很差,此时一般会采取用scoll滚动查询,一批一批的查,直到所有数据都查询完处理完 使用scoll滚动搜索,可以先搜索一批数据,然后下次再搜索一批数据,以 ...

  6. jdk环境变量设置理解

    1.系统变量→新建 JAVA_HOME 变量 . 变量值填写jdk的安装目录(本人是 E:\Java\jdk1.7.0) 2.系统变量→寻找 Path 变量→编辑 在变量值最后输入 %JAVA_HOM ...

  7. 【配置属性】—Entity Framework实例详解

    Entity Framework Code First的默认行为是使用一系列约定将POCO类映射到表.然而,有时候,不能也不想遵循这些约定,那就需要重写它们.重写默认约定有两种方式:Data Anno ...

  8. 第18题 Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  9. SDUT OJ 1124 飞越原野 (三维BFS练习)

    飞跃原野 nid=24#time" title="C.C++.go.haskell.lua.pascal Time Limit5000ms Memory Limit 65536K ...

  10. 【解决】run-as: Package &#39;&#39; is unknown

    问题: [2014-07-30 20:20:25 - nativeSensorStl] gdbserver output: [2014-07-30 20:20:25 - nativeSensorStl ...