fork(2) download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define pb push_back
  4. #define maxx 30
  5. int color[maxx];
  6. vector<int>G[maxx];
  7. void clear()
  8. {
  9. for(int i=0;i<maxx;i++)
  10. G[i].clear();
  11. memset(color,-1,sizeof color);
  12. }
  13. void dfs(int s,int col)
  14. {
  15. color[s]=col;
  16. for(int i=0;i<G[s].size();i++)
  17. {
  18. if(color[G[s][i]]==-1)
  19. {
  20. dfs(G[s][i],col);
  21. }
  22. }
  23. }
  24. int main()
  25. {
  26. int t;
  27. scanf("%d",&t);
  28. scanf("\n");
  29. while(t--)
  30. {
  31. clear();
  32. char x;
  33. scanf("%c",&x);
  34. scanf("\n");
  35. int n=(int)(x-'A');
  36. string s;
  37. while((cin>>s))
  38. {
  39. G[s[0]-'A'].pb(s[1]-'A');
  40. G[s[1]-'A'].pb(s[0]-'A');
  41. }
  42. int connect=0;
  43. for(int i=0;i<=n;i++)
  44. {
  45. if(color[i]==-1)
  46. dfs(i,++connect);
  47. }
  48. cout<<connect<<endl;
  49. }
  50. return 0;
  51. }
  52.  
Runtime error #stdin #stdout 0s 3276KB
stdin
7

E
AB
CE
DB
EC

G
AB
CE
DB
EC
EF
GG

Z
AC
AD
AE
AI
AL
AM
AN
AO
AQ
AS
AV
AW
AX
AY
BF
BH
BJ
BK
BL
BM
BN
BO
BP
BQ
BR
BU
BV
BW
BY
CE
CG
CI
CJ
CM
CP
CT
CU
CV
CY
DF
DG
DJ
DK
DL
DM
DN
DO
DQ
DV
DX
EF
EH
EK
EL
EM
EN
EO
ER
ES
ET
EU
EY
EZ
FG
FH
FI
FJ
FM
FN
FO
FP
FR
FU
FW
FY
FZ
GH
GL
GM
GP
GQ
GR
GS
GZ
HI
HJ
HK
HL
HQ
HR
HS
HV
HW
HY
IK
IM
IO
IP
IR
IU
IV
IX
IY
IZ
JK
JL
JM
JN
JR
JW
KM
KO
KP
KR
KS
KX
KY
KZ
LO
LP
LQ
LR
LY
MN
MO
MP
MR
MT
MU
MV
MX
MZ
NP
NR
NT
NV
NY
OQ
OV
OW
OX
PR
PS
PT
PV
PW
PX
PY
PZ
QR
QT
QV
QY
RT
RU
RW
RY
RZ
ST
SV
SZ
TX
TY
TZ
UW
UY
UZ
VW
WZ
XY
XZ

E
AE
EC
CB
BD

F
AB
BC
CA
DE
EF
FD

Z

C
AB
stdout
Standard output is empty