-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path459 Graph Connectivity.cpp
More file actions
57 lines (52 loc) · 1.71 KB
/
Copy path459 Graph Connectivity.cpp
File metadata and controls
57 lines (52 loc) · 1.71 KB
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
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define Set(v,d) memset(v, d, sizeof(v))
#define oo 10000000000000007 //infinity
#define F first
#define S second
#define pb push_back
#define sz(x) (int)(x.size())
#define all(x) (x.begin()), (x.end())
#define rall(x) (x.rbegin()), (x.rend())
typedef vector<int> vi;
vi adj[340]; bool visited[340];
void dfs(int n){
visited[n] = 1;
for(int i=0;i<sz(adj[n]);i++)
if(!visited[adj[n][i]])
dfs(adj[n][i]);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int TC; char maxChar;
scanf("%d", &TC);
cin.ignore();
while(TC--){
// cin.ignore();
int cnt = 0;
for(int i=0;i<340;i++)
adj[i].clear();
Set(visited, 0);
cin>>maxChar;
cin.ignore();
while(1){
string s;
getline(cin, s);
if(!sz(s)) break;
adj[s[0]-'A'].pb(s[1]-'A');
adj[s[1]-'A'].pb(s[0]-'A');
}
// for(int i=0;i<=maxChar-'A';i++) for(int j=0;j<sz(adj[i]);j++) cout<<i<<" "<<adj[i][j]<<endl;
for(int i=0;i<=maxChar-'A';i++)
if(!visited[i]){ dfs(i); cnt++; }
cout<<cnt<<endl;
if(TC) puts("");
}
return 0;
}