Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdio>
- #include<cmath>
- #include<vector>
- #include<algorithm>
- #include<map>
- #include<stack>
- #include<queue>
- #include<cstring>
- #include<set>
- using namespace std;
- #define MEM(array,w) memset((array),w,sizeof((array)))
- int visited[26];
- int visit[26][26];
- char ch;
- int dfs(int i)
- {
- visited[i]=1;
- for(int j=0;j<=ch-'A';j++)
- {
- if(visited[j]==0 && visit[i][j]==1)
- {
- dfs(j);
- }
- }
- }
- int main()
- {
- int test;
- cin>>test;
- getchar();
- bool f=0;
- while(test--)
- {
- if(f==1)cout<<endl;f=1;
- cin>>ch;
- getchar();
- MEM(visit,0);
- memset(visited,0,sizeof(visited));
- while(1)
- {
- string s;
- getline(cin,s);
- if(s.size()==0)break;
- visit[(s[0]-'A')][(s[1]-'A')]= 1;
- visit[(s[1]-'A')][(s[0]-'A')]= 1;
- }
- int sum=0;
- for(int i=0;i<=(ch-'A');i++){
- if(visited[i]!=1){dfs(i);sum++;}
- }
- cout<<sum<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement