fork download
  1. #include<iostream>
  2. #include <vector>
  3. #include<stdio.h>
  4. #include<string>
  5. #include<string.h>
  6. #include <list>
  7. #include <algorithm>
  8. using namespace std;
  9.  
  10. class disjoint
  11. {
  12. private :
  13. vector<int> parent ;
  14. public :
  15. disjoint(int n):parent(n+10)
  16. {
  17. for (int i=1 ; i<=n ;i++)
  18. parent[i]=i;
  19. }
  20. int Find (int x)
  21. {
  22. if (parent[x]==x)
  23. return x;
  24. else
  25. {
  26. parent[x]=Find(parent[x]);
  27. return parent[x];
  28. }
  29. }
  30. void Union (int x ,int y)
  31. {
  32. int a=Find(x);
  33. int b=Find(y);
  34. if(x==y)
  35. return ;
  36. else
  37. parent[b]=a;
  38. }
  39.  
  40. ~disjoint()
  41. {
  42. parent.clear();
  43. }
  44.  
  45. };
  46.  
  47. int main()
  48. { int tc;
  49. cin>>tc;
  50. int n,cm1,cm2;
  51. int yescount =0;
  52. int nocount=0;
  53. char c;
  54. bool first=true;
  55.  
  56. while(tc--)
  57. {
  58. yescount =0;
  59. nocount=0;
  60. cin>>n;
  61. disjoint computers(n);
  62. getchar ();
  63. while((c = getchar ()) && isalpha (c) )
  64. {
  65.  
  66. if(c=='c')
  67. {
  68. cin>>cm1>>cm2;
  69. getchar ();
  70. computers.Union(cm1,cm2);
  71.  
  72. }
  73. else if(c=='q')
  74. {
  75. cin>>cm1>>cm2;
  76. getchar ();
  77. if(computers.Find(cm1)!=computers.Find(cm2))
  78. nocount++;
  79. else
  80. yescount++;
  81.  
  82. }
  83.  
  84. }
  85. if (first)
  86. {
  87. cout<<yescount<<','<<nocount;
  88. first=false;
  89. }
  90. else
  91. {
  92.  
  93. cout<<endl<<yescount<<','<<nocount;
  94. }
  95.  
  96. }
  97. return 0;
  98. }
Success #stdin #stdout 0s 3436KB
stdin
2

4
c 1 4
c 2 4
q 1 3
q 1 4

10
c 1 5
c 2 7
q 7 1
c 3 9
q 9 6
c 2 5
q 7 5
stdout
1,1
1,2