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 并查集的更多相关文章

  1. 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 ...

  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 #600 (Div. 2) D题【并查集+思维】

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

  6. 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 ...

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

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

  8. 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 ...

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

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

随机推荐

  1. QT执行shell脚本或者执行linux指令

    由于我在做linux下的QT开发,有时候会用到shell脚本的辅助,但是需要QT运行shell脚本并获取执行结果,今天给大家分享下我的技巧,废话少说直接上代码: //执行shell指令或者shell脚 ...

  2. ORA-27090: Unable to reserve kernel resources for asynchronous disk I/O

    2019-08-19T09:27:33.225584+08:00Slave encountered ORA-27090 exception during crash recoveryRecovery ...

  3. 快速查看本地IP地址

    1. 新建Windows批处理文件(*.bat) @echo off echo 本机IP ipconfig|find "IPv4" echo. echo 电脑名 hostname ...

  4. 如何写一个Python万能装饰器,既可以装饰有参数的方法,也可以装饰无参数方法,或者有无返回值都可以装饰

    Python中的装饰器,可以有参数,可以有返回值,那么如何能让这个装饰器既可以装饰没有参数没有返回值的方法,又可以装饰有返回值或者有参数的方法呢?有一种万能装饰器,代码如下: def decorate ...

  5. jxl.jar下载

    jxl.jar是通过java操作excel表格的工具类库,是由java语言开发而成的. 在网上找了很多,不是链接失效,就是csdn上要钱的,所以干脆上传个到自己的博客文件里,方便你们下载. 下载地址: ...

  6. 通过 Filebeat 收集 ubuntu 系统日志

    需求:收集 ubuntu 系统日志,发送给 logstash,再由 logstash 把数据传递给 elasticsearch,最后通过 kibana 展示日志数据. Filebeat System ...

  7. java之操作集合的工具类--Collections

    Collections是一个操作Set.List和Map等集合的工具类. Collections中提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了对集合对象设置不可变.对集合对象实现同步控 ...

  8. Python 词云可视化

    最近看到不少公众号都有一些词云图,于是想学习一下使用Python生成可视化的词云,上B站搜索教程的时候,发现了一位UP讲的很不错,UP也给出了GitHub上的源码,是一个很不错的教程,这篇博客主要就是 ...

  9. js格式化JSON数据

    前言: 最近做的项目中遇到个需要在前端页面中将某个设备需要的数据格式展示出来,方便用户配置.一开始单纯的将数据格式写入到pre标签中, 但是通过pre标签写入的数据格式在代码可视化上不是很优雅.由于上 ...

  10. windows下cocos2d-x环境搭建

    该教程使用的cocos2dx的版本为3.14,3之后的大概都差不多 Python环境搭建: cocos2dx在windows上新建工程需要用到python脚本,安装python-2.7.x,可以上py ...