# 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. InputThe 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. OutputIn a single line print the minimal number of passwords, the use of
which will allow guaranteed to access the system. Examplesinput 4 a b ab d output 2 input 3 ab bc abc output 1 input 1
codeforces output 1 NoteIn the second example hacker need to use any of the passwords to
access the system.
题意是说有n个密码,密码与密码之间有等价性,如果两个密码都有同一个字母,那么可以说这两个密码是等价的,其次等价性是可以传递的,即如果a等价b,b等价c,即使a与c不存在相同的字母,但他们也可以说是等价的;
你解开这个系统需要所有的密码,具有等价性的密码是可以通用的,那么你至少需要多少个密码?
思路是这样的,先构建这样的n+26个集合,初始时刻,第i个字符串属于第i个集合,然后第i+1个集合对应字母a…第i+26个集合对应字母z;
然后遍历每一个字符串,让每一个字符串与该字符串中所包含的字母合并集合,然后再一次遍历所有的字符串,数出有几个不同的集合
#include <cstdio>
#include <set>
#include <string>
#include <iostream>
using namespace std;
const int N = 2e5 + 10;
string s[N];
int fa[N+26];
int n;
int find(int x){
return fa[x] == x?x:fa[x] = find(fa[x]);
}
void merge(int x,int y){
int fx = find(x);
int fy = find(y);
if(fx != fy){
fa[fx] = fy;
}
}
int main(){
scanf("%d",&n);
for(int i = 1;i <= n;i++){
cin>>s[i];
}
for(int i = 1;i <= n+26;i++) fa[i] = i;
for(int i = 1;i <= n;i++){
for(int j = 0;j < s[i].length();j++){
merge(i,n+s[i][j] - 'a'+1);
}
}
set<int> myset;
int ans = 0;
for(int i = 1;i <= n;i++){
if(!myset.count(find(i))){
ans++;
myset.insert(find(i));
}
}
printf("%d\n",ans);
return 0;
}