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 57 |
#include <iostream> #include <cstdio> #include <cstring> #include <string> using namespace std; int parent[26]; int countNodes[26]; int getparent(int n) { if(parent[n]==n) return n; else return parent[n]=getparent(parent[n]); } void unite(int a,int b) { parent[getparent(a)]=getparent(b); } int main() { int t; string str; scanf("%d",&t); getchar(); while(t--) { str=""; for(int i=0;i<26;i++) { parent[i]=i; countNodes[i]=0; } while(getline(cin,str),str[0]!='*') { unite(str[1]-'A',str[3]-'A'); } getline(cin,str); for(int i=0;i<str.size();i++) { if(str[i]!=',') countNodes[getparent(str[i]-'A')]+=1; } int tree=0,acron=0; for(int i=0;i<26;i++) { if(countNodes[i]>1) tree+=1; else if(countNodes[i]==1) acron+=1; } printf("There are %d tree(s) and %d acorn(s).\n",tree,acron); } return 0; } |