Saleh127

UVA 459

Jun 20th, 2021
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5. bool v[200005];
  6. vector<ll>g[20000];
  7.  
  8. void dfs(ll in)
  9. {
  10. if(v[in]) return;
  11.  
  12. v[in]=1;
  13. for(auto dd:g[in])
  14. {
  15. if(v[dd]==0)
  16. {
  17. dfs(dd);
  18. }
  19. }
  20. }
  21.  
  22. int main()
  23. {
  24. ios_base::sync_with_stdio(0);
  25. cin.tie(0);cout.tie(0);
  26.  
  27. string a,c;
  28. ll n,m,i,j,k,l;
  29. test
  30. {
  31.  
  32. cin>>c;
  33. n=c[0]-65;
  34. getline(cin,a);
  35. while(getline(cin,a))
  36. {
  37. if(a.size()==0) break;
  38.  
  39. g[a[0]-65].push_back(a[1]-65);
  40. g[a[1]-65].push_back(a[0]-65);
  41. }
  42. memset(v,0,sizeof v);
  43.  
  44. ll l=0;
  45.  
  46. for(i=0;i<=n;i++)
  47. {
  48. if(v[i]==0)
  49. {
  50. l++;
  51. dfs(i);
  52. }
  53. }
  54. cout<<l<<endl;
  55. if(cs!=t) cout<<endl;
  56.  
  57. for(i=0;i<=n;i++)
  58. {
  59. g[i].clear();
  60. }
  61. }
  62.  
  63. return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment