fork download
  1.  
  2.  
  3. //BISMILLAHIRRAHMANIRRAHIM
  4. /*
  5.  manus tar shopner soman boro
  6.  Author :: Shakil Ahmed
  7. .............AUST_CSE27.........
  8.  prob ::
  9.  Type ::
  10.  verdict::
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <ctype.h>
  17. #include <math.h>
  18. #include <limits.h>
  19. #include <set>
  20. #include <algorithm>
  21. #include <iostream>
  22. #include <vector>
  23. #include <stack>
  24. #include <string>
  25. #include <list>
  26. #include <map>
  27. #include <queue>
  28. #include <sstream>
  29. #include <utility>
  30. #define pb push_back
  31. #define mp make_pair
  32. #define pi acos(-1.0)
  33. #define ff first
  34. #define LL long long
  35. #define ss second
  36. #define rep(i,n) for(i = 0; i < n; i++)
  37. #define REP(i,n) for(i=n;i>=0;i--)
  38. #define FOR(i,a,b) for(int i = a; i <= b; i++)
  39. #define ROF(i,a,b) for(int i = a; i >= b; i--)
  40. #define re return
  41. #define QI queue<int>
  42. #define SI stack<int>
  43. #define pii pair <int,int>
  44. #define MAX
  45. #define MOD
  46. #define INF 1<<30
  47. #define SZ(x) ((int) (x).size())
  48. #define ALL(x) (x).begin(), (x).end()
  49. #define sqr(x) ((x) * (x))
  50. #define memo(a,b) memset((a),(b),sizeof(a))
  51. #define G() getchar()
  52. #define MAX3(a,b,c) max(a,max(b,c))
  53.  
  54. double const EPS=3e-8;
  55. using namespace std;
  56.  
  57.  
  58. template< class T > T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a%b) : a); }
  59. template< class T > T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); }
  60. template <class T> inline T bigmod(T p,T e,T M)
  61. {
  62. if(e==0)return 1;
  63. if(e%2==0){LL t=bigmod(p,e/2,M);return (T)((t*t)%M);}
  64. return (T)((LL)bigmod(p,e-1,M)*(LL)p)%M;
  65. }
  66.  
  67. template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}
  68. //Fast Reader
  69. template<class T>inline bool read(T &x){int c=getchar();int sgn=1;while(~c&&c<'0'||c>'9'){if(c=='-')sgn=-1;c=getchar();}for(x=0;~c&&'0'<=c&&c<='9';c=getchar())x=x*10+c-'0'; x*=sgn; return ~c;}
  70.  
  71. /* ************************************** My code start here ****************************************** */
  72. int par[30];
  73. void Initialization(int n)
  74. {
  75. int i ;
  76. for ( i = 0 ; i <= n ; i++ ) par[i] = i ;
  77. }
  78. int find_parent(int x)
  79. {
  80. if(par[x]==x) return x;
  81. return par[x] = find_parent(par[x]);
  82. }
  83. void Make_Union(int x , int y)
  84. {
  85. par[find_parent(x)] = find_parent(y);
  86. }
  87. bool isUnion(int x , int y)
  88. {
  89. return find_parent(x)==find_parent(y);
  90. }
  91. int main()
  92. {
  93. int t,i , cs;
  94.  
  95. bool space = false;
  96.  
  97. scanf("%d",&t);
  98. getchar();
  99. for ( cs = 1 ; cs <= t ; cs++ , space = 1)
  100. { if(space) printf("\n");
  101. char lim;
  102. string str;
  103. int i ;
  104. cin>>lim;
  105. getchar();
  106. Initialization(lim-'A');
  107. int ans = lim-'A' + 1;
  108. while(getline(cin,str) && str!="")
  109. {
  110. int x = str[0]-'A';
  111. int y = str[1]-'A';
  112. if(!isUnion(x,y)) Make_Union(x,y) , ans-- ;
  113.  
  114. }
  115.  
  116. cout<<ans<<endl;
  117.  
  118.  
  119. }
  120. return 0;
  121. }
  122.  
Success #stdin #stdout 0s 3344KB
stdin
Standard input is empty
stdout
Standard output is empty