#include<bits/stdc++.h>
#define White 1
#define Gray 2
#define Black 3
#define N 50
using namespace std;
int node, edge, a[N][N], c[N], cnt = 0;
void dfsvisit(int x)
{
c[x] = Gray;
for(int i=1;i<=node;i++)
{
if(a[x][i] == 1 && c[i] == White)
{
dfsvisit(i);
}
}
c[x] = Black;
}
void dfs()
{
for(int i=1;i<=node;i++)
c[i] = White;
for(int i=1;i<=node;i++)
{
if(c[i] == White)
{
dfsvisit(i);
cnt++;
}
}
}
int main()
{
char ch;
int t;
cin >> t;
for(int q=1;q<=t;q++)
{
if(q > 1)
cout << "\n";
for(int i=0;i<=49;i++)
{
c[i] = White;
for(int j=0;j<=49;j++)
{
a[i][j] = 0;
}
}
cnt = 0;
cin >> ch;
getchar();
string s;
int mx = ch - 64;
while(getline(cin, s))
{
if(s == "")
break;
int t1 = (s[0] - 64);
int t2 = (s[1] - 64);
mx = max(mx, t1);
mx = max(mx, t2);
a[t1][t2] = 1;
a[t2][t1] = 1;
}
node = mx;
dfs();
cout << cnt << "\n";
}
return 0;
}
Comments
Post a Comment