如果两个人会的语言中有共同语言那么他们之间就可以交流,并且如果a和b可以交流,b和c可以交流,那么a和c也可以交流,具有传递性,就容易联想到并查集,我们将人和语言看成元素,一个人会几种语言的话,就将这些语言和这个人所在的集合合并,最后求一下人一共在几个连通块中,连通块的个数-1就是答案,有一种比较坑的情况是所有人都不会语言,那么每个人都需要学一种语言,人数就是答案。

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define ull unsigned long long
#define db double
#define endl '\n'
#define debug(a) cout<<#a<<"="<<a<<endl;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 0x3f3f3f3f
#define x first
#define y second
using namespace std; const int N=1010;
int fa[N],n,m,vis[N]; int find(int x)
{
if(x==fa[x]) return x;
return fa[x]=find(fa[x]);
} void merge(int x,int y)
{
int px=find(x),py=find(y);
if(px!=py) fa[py]=px;
} void solve()
{
cin>>n>>m;
rep(i,1,n+m) fa[i]=i;
bool st=false;
rep(i,1,n)
{
int k;cin>>k;
if(k) st=true;
rep(j,1,k)
{
int x;cin>>x;
merge(i,x+n);
}
}
int cnt=0;
rep(i,1,n) if(find(i)==i) cnt++;
if(st) cout<<cnt-1<<endl;
else cout<<cnt<<endl;
} int main()
{
IOS
// freopen("1.in", "r", stdin);
int t;
// cin>>t;
// while(t--)
solve();
return 0;
}

Codeforces Round 170 (Div. 1)A. Learning Languages并查集的更多相关文章

  1. [Codeforces Round #170 Div. 1] 277A Learning Languages

    A. Learning Languages time limit per test:2 seconds memory limit per test:256 megabytes input standa ...

  2. Codeforces Round #245 (Div. 2) B. Balls Game 并查集

    B. Balls Game Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...

  3. Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集

    E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...

  4. Codeforces Round #345 (Div. 2) E. Table Compression 并查集

    E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...

  5. Codeforces Round #603 (Div. 2) D. Secret Passwords 并查集

    D. Secret Passwords One unknown hacker wants to get the admin's password of AtForces testing system, ...

  6. Codeforces Round #600 (Div. 2) D题【并查集+思维】

    题意:给你n个点,m条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...

  7. Codeforces Round #345 (Div. 2) E. Table Compression 并查集+智商题

    E. Table Compression time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  8. Codeforces Round #600 (Div. 2) - D. Harmonious Graph(并查集)

    题意:对于一张图,如果$a$与$b$连通,则对于任意的$c(a<c<b)$都有$a$与$c$连通,则称该图为和谐图,现在给你一张图,问你最少添加多少条边使图变为和谐图. 思路:将一个连通块 ...

  9. Codeforces Round #345 (Div. 1) C. Table Compression (并查集)

    Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorith ...

  10. Codeforces Round #582 (Div. 3) G. Path Queries (并查集计数)

    题意:给你带边权的树,有\(m\)次询问,每次询问有多少点对\((u,v)\)之间简单路径上的最大边权不超过\(q_i\). 题解:真的想不到用最小生成树来写啊.... 我们对边权排序,然后再对询问的 ...

随机推荐

  1. 升级到win11 22h2的体验

    win11 22h2更稳定了 在win11 22h2发布后没多久,我就升级到了这个版本,截止目前已经使用半个月了,谈谈我的使用感受. 总体要比之前的版本更稳定,表现为笔记本风扇不会突然响,突然卡顿,不 ...

  2. ShardingSphere

    目录 1.ShardingSphere分表与分库分表 2.ShardingSphere分库分表查询 3.自定义分片算法实现range查询 4.SPI扩展机制概述 5.stand通过SPI实现range ...

  3. 微服务保护-Sentinel

    1.初识Sentinel 1.1.雪崩问题及解决方案 1.1.1.雪崩问题 微服务中,服务间调用关系错综复杂,一个微服务往往依赖于多个其它微服务. 如图,如果服务提供者I发生了故障,当前的应用的部分业 ...

  4. Windows下,SpringBoot JDBC无法连接的问题

    问题症状 在Win7和Win10下启动时均会出现下面的错误,但是在OSX和Linux下没问题 com.mysql.jdbc.exceptions.jdbc4.CommunicationsExcepti ...

  5. SpringBoot使用git-commit-id-maven-plugin打包

    简介 git-commit-id-maven-plugin 是一个maven 插件,用来在打包的时候将git-commit 信息打进jar中. 这样做的好处是可以将发布的某版本和对应的代码关联起来,方 ...

  6. mysqlGTID主从同步出现1236错误问题

    从主库xtrabackup备份,配置好gtid复制,从主库的从库复制.一直报错误 Last_IO_Error: Got fatal error 1236 from master when readin ...

  7. centos7源码方式安装zabbix-4.0

    1.关闭防火墙 systemctl stop firewalld.service #临时关闭firewall systemctl disable firewalld.service #禁止firewa ...

  8. Linux crontab不执行

    Linux 系统里面计划任务,crontab 没有如期执行这是运维工作中比较常见的一种故障了. 下面结合最近部署自动脚本不执行问题排查步骤: 1.检查 crontab 服务是否正常 [dmdba@te ...

  9. .gitignore 无法工作

    在开发一个新项目时,发现每次编译时都会产生一些 .obj 无用的文件,这些文件并不需要 push 到 github 上 故使用 .gitignore 忽略这些文件 首先,我们可以设置这些文件的输出目录 ...

  10. 【Android逆向】破解看雪test3.apk方案二

    方案二就是要hook那三个条件,不让追加字符串变成false v20 = "REAL"; clazz = _JNIEnv::FindClass(env, "android ...