using namespace std;
vector<long long int>edges[100];
long long int visited[100];
vector<long long int>result;
long long int dfs(long long int s)
{
long long int x,y,z;
if(visited[s]==0)
{
visited[s]=1;
for(x=0;x<edges[s].size();x++)
{
z=edges[s][x];
if(visited[z]==0)
{
result.push_back(z);
dfs(z);
}
}
}
}
int main()
{
long long int i,j,k,t,l,m,n,u,v,ct;
string s,c;
char d;
//freopen("459in.txt","r",stdin);
//freopen("459out.txt","w",stdout);
cin>>t;
for(i=1;i<=t;i++)
{
cin>>c;
ct=0;
k=c[0]-65;
getline(cin,s);
while(getline(cin,s))
{
if(s.length()==0)
{
break;
}
u=s[0]-65;
v=s[1]-65;
edges[u].push_back(v);
edges[v].push_back(u);
}
for(j=0;j<=k;j++)
{
if(visited[j]==0)
{
ct++;
dfs(j);
}
}
cout<<ct<<endl;
if(i!=t)
{
cout<<endl;
}
memset(visited,0,sizeof visited);
memset(edges,0,sizeof edges);
}
return 0;
}
0 comments:
Post a Comment