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. Html5 小游戏 俄罗斯方块

    导言 在一个风和日丽的一天,看完了疯狂HTML 5+CSS 3+JavaScript讲义,跟着做了书里最后一章的俄罗斯方块小游戏,并做了一些改进,作为自己前端学习的第一站. 游戏效果: 制作思路 因为 ...

  2. Kali Linux configuration "Ettercap"

    Xx_Instroduction Ettercap is a man-in-the-middle attack(MITM) tool,kali take this tool,so,use front ...

  3. VUE组件 之 高德地图地址选择

    注:本文基于上一篇文章[ Vue-Cli 3.0 中配置高德地图] ,采用直接引入高德 SDK 的方式来使用高德地图api 一.效果图 二.组件要实现的功能 1. 如果有传入坐标点,则定位到坐标点 2 ...

  4. 教你如何在5分钟轻松部署squid正向代理

    正向代理是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返 ...

  5. 解决idea中mysql连接失败Could not create connection to database server. Attempted reconnect 3 times. Giving up.

    原因是少一个参数,设置时区的.  解决方法: 加一个参数: serverTimezone=UTC jdbc:mysql://localhost:3306/SshProject?useUnicode=t ...

  6. 单个div元素实现双边框

    昨天被问到一个很有意思的问题,单个div元素怎么实现双边框,当时脑子懵了一下,然后就回答出来用伪元素,别的实在是想不起来了,所以在此总结一下子防止以后再被问到 总结了一下大约有以下几种方案: 伪元素实 ...

  7. Ruby中星号打包解包操作

    Ruby中可以使用一个星号*和两个星号**完成一些打包.解包操作,它们称为splat操作符: 一个星号:以数组为依据进行打包解包(参考文章) 两个星号:以hash为依据进行打包解包(参考文章) 两个星 ...

  8. PHP简单判断当前使用的是什么浏览器

    PHP简单判断当前使用的是什么浏览器,判断浏览器类型的方法,方便统计网站访问用户使用浏览器的比例. 判断浏览器类型方法 function userBrowser() { $user_OSagent = ...

  9. 软件设计之基于Java的连连看小游戏(一)——开题及游戏首页的制作

    原本计划紧张忙碌的考试月在图书馆和实验室度过,结果突如其来为期两周的软件设计把课余时间几乎捆绑在了机房.软设没有太多知识上的要求,只要成品简洁美观.实用准确即可.考虑了很久决定要用Java swing ...

  10. zabbix 分布式监控及优化

    1..zabbix分布式监控,模拟多机房实现监控? 1.有多机房时,需要用到proxy 1.网络不通 2.网络延迟 2.当监控的主机较多时,也可以用proxy来缓解压力 1.安装proxy [root ...