-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path71597648.cpp
63 lines (62 loc) · 1.17 KB
/
71597648.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <bits/stdc++.h>
using namespace std;
set<int> nodes;
set<int> neigh[100];
set<int> unmarked;
int components=0;
void dfs(int index)
{
//cout<<"call "<<index<<" ";
unmarked.erase(index);
for(auto i:neigh[index])
{
//cout<<"checking "<<i<<" ";
if(unmarked.count(i))
{
dfs(i);
}
}
}
void join(char a,char b)
{
int aa=a-'a';
int bb=b-'a';
neigh[aa].insert(bb);
neigh[bb].insert(aa);
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
string str;
cin>>str;
for(int j=0;j<str.size();j++)
{
nodes.insert(str[j]-'a');
unmarked.insert(str[j]-'a');
}
for(int j=0;j<str.size()-1;j++)
join(str[j],str[j+1]);
}
/*for(int i=0;i<10;i++)
{
cout<<i<<" : ";
for(auto j:neigh[i])
cout<<j<<" ";
cout<<endl;
}
for(auto i:unmarked)
cout<<i<<" ";
cout<<endl;*/
while(unmarked.size())
{
components++;
int a=(int)*unmarked.begin();
//cout<<"in "<<a<<endl;
dfs(a);
}
cout<<components<<endl;
return 0;
}