-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph Connectivity
More file actions
49 lines (48 loc) · 904 Bytes
/
Graph Connectivity
File metadata and controls
49 lines (48 loc) · 904 Bytes
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
#include<bits/stdc++.h>
using namespace std;
vector<int>adj[50];
int vis[50];
void dfs(int n)
{
vis[n]=1;
for(int i=0; i<adj[n].size(); i++)
{
if(vis[adj[n][i]]==0)
dfs(adj[n][i]);
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
string s;
char ch;
cin>>ch;
memset(vis,0,sizeof(vis));
memset(adj,0,sizeof(adj));
int k=ch-'A', cnt=0;
getline(cin,s);
while(getline(cin,s))
{
if(s.length()==0)
break;
adj[s[0]-'A'].push_back(s[1]-'A');
adj[s[1]-'A'].push_back(s[0]-'A');
}
cnt=0;
for(int i=0; i<=k; i++)
{
if(vis[i]==0)
{
cnt++;
dfs(i);
}
}
cout<<cnt<<endl;
if(t)
cout<<endl;
}
return 0;
}