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, to get problems from the next contest. To achieve that, he sneaked into the administrator's office and stole a piece of paper with a list of n passwords — strings, consists of small Latin letters.
Hacker went home and started preparing to hack AtForces. He found that the system contains only passwords from the stolen list and that the system determines the equivalence of the passwords a and b as follows:
two passwords a and b are equivalent if there is a letter, that exists in both a and b;
two passwords a and b are equivalent if there is a password c from the list, which is equivalent to both a and b.
If a password is set in the system and an equivalent one is applied to access the system, then the user is accessed into the system.
For example, if the list contain passwords "a", "b", "ab", "d", then passwords "a", "b", "ab" are equivalent to each other, but the password "d" is not equivalent to any other password from list. In other words, if:
admin's password is "b", then you can access to system by using any of this passwords: "a", "b", "ab";
admin's password is "d", then you can access to system by using only "d".
Only one password from the list is the admin's password from the testing system. Help hacker to calculate the minimal number of passwords, required to guaranteed access to the system. Keep in mind that the hacker does not know which password is set in the system.
Input
The first line contain integer n (1≤n≤2⋅105) — number of passwords in the list. Next n lines contains passwords from the list – non-empty strings si, with length at most 50 letters. Some of the passwords may be equal.
It is guaranteed that the total length of all passwords does not exceed 106 letters. All of them consist only of lowercase Latin letters.
Output
In a single line print the minimal number of passwords, the use of which will allow guaranteed to access the system.
Examples
input
4
a
b
ab
d
output
2
input
3
ab
bc
abc
output
1
input
1
codeforces
output
1
Note
In the second example hacker need to use any of the passwords to access the system.
题意
现在你有n个密码,但里面有些密码是等价的,等价的定义是:
假设存在一个字母x,在a和b字符串都出现过,那么a字符串和b字符串就是等价的。
假设a字符串和c字符串等价,b和c字符串等价,那么a和b也等价。
问你最少掌握多少个密码,就能掌握所有密码了
题解
视频题解 https://www.bilibili.com/video/av77514280/
并查集裸题。。。每次和自己所包含的字母合成一坨即可
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+7;
int fa[maxn],n;
string s[maxn];
int fi(int x){
return fa[x]==x?fa[x]:fa[x]=fi(fa[x]);
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>s[i];
}
for(int j=1;j<=n+26;j++){
fa[j]=j;
}
for(int i=1;i<=n;i++){
for(int j=0;j<s[i].size();j++){
fa[fi(i)]=fa[fi(n+s[i][j]-'a'+1)];
}
}
int ans = 0;
set<int>vis;
for(int i=1;i<=n;i++){
if(!vis.count(fi(i))){
ans++;
vis.insert(fi(i));
}
}
cout<<ans<<endl;
}
Codeforces Round #603 (Div. 2) D. Secret Passwords 并查集的更多相关文章
- Codeforces Round #603 (Div. 2) D. Secret Passwords(并查集)
链接: https://codeforces.com/contest/1263/problem/D 题意: One unknown hacker wants to get the admin's pa ...
- 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 ...
- Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集
E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...
- Codeforces Round #345 (Div. 2) E. Table Compression 并查集
E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...
- Codeforces Round #600 (Div. 2) D题【并查集+思维】
题意:给你n个点,m条边,然后让你使得这个这个图成为一个协和图,需要加几条边.协和图就是,如果两个点之间有一条边,那么左端点与这之间任意一个点之间都要有条边. 思路:通过并查集不断维护连通量的最大编号 ...
- 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 ...
- Codeforces Round #600 (Div. 2) - D. Harmonious Graph(并查集)
题意:对于一张图,如果$a$与$b$连通,则对于任意的$c(a<c<b)$都有$a$与$c$连通,则称该图为和谐图,现在给你一张图,问你最少添加多少条边使图变为和谐图. 思路:将一个连通块 ...
- 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 ...
- Codeforces Round #582 (Div. 3) G. Path Queries (并查集计数)
题意:给你带边权的树,有\(m\)次询问,每次询问有多少点对\((u,v)\)之间简单路径上的最大边权不超过\(q_i\). 题解:真的想不到用最小生成树来写啊.... 我们对边权排序,然后再对询问的 ...
随机推荐
- PHP switch的写法
switch switch (expression) { case label1: expression = label1 时执行的代码 ; break; case label2: expressio ...
- Python语法速查: 7. 函数基础
返回目录 (1)函数基本 ● 函数是第一类对象 Python中万物皆对象,所有对象都是第一类的(first class),函数也不例外,也是第一类对象.既然是对象,那就可以当作普通的对象数据处理,比如 ...
- 14. Go 语言编译与工具
Go 语言编译与工具 Go 语言的工具链非常丰富,从获取源码.编译.文档.测试.性能分析,到源码格式化.源码提示.重构工具等应有尽有. 在 Go 语言中可以使用测试框架编写单元测试,使用统一的命令行即 ...
- centos安装jdk10
下载一个jdk10文件到linux : wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=ac ...
- Java之线程安全
什么是线程安全? 如果有多个线程在同时运行,而这些线程可能会同时运行这段代码.程序每次运行结果和单线程运行的结果是一样的,而且其他的变量的值也和预期的是一样的,就是线程安全的. 什么是线程安全问题? ...
- Python连载54-FTP编程
一.FTP工作流程 1.客户端链接远程主机上的FTP服务器 2.客户端输入用户名和密码(或者“anonymous”和电子邮件地址) 3.客户端和服务器进行各种文件传输和信息查询操作 4.客户端从远程F ...
- BootStrap 关于input与btn的点击focus取消特效相关css
取消btn按钮点击出现的外边框: .btn:focus, /*清除btn按钮点击出现的边框*/.btn:active:focus,.btn.active:focus,.btn.focus,.btn:a ...
- 分布式应用监控: SkyWalking 快速接入实践
分布式应用,会存在各种问题.而要解决这些难题,除了要应用自己做一些监控埋点外,还应该有一些外围的系统进行主动探测,主动发现. APM工具就是干这活的,SkyWalking 是国人开源的一款优秀的APM ...
- 项目部署到Linux上遇到的坑
作者:晨钟暮鼓c个人微信公众号:程序猿的月光宝盒 1.本地Navicat for MySQL无法连接至服务器(Centos 7 x86_64 bbr) 1045错误: 解决步骤: 1.查看用户名密 ...
- Java 的 IO 流
接着上一篇的 “Java 的 File 类” 的随笔,在File类的基础上,我们就走进Java的IO流吧. 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在 ...