Advertisement
AzmayenSabil

Untitled

Jul 26th, 2021
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. using LL = long long;
  6. using ULL = unsigned long long;
  7.  
  8. const long long MOD = 1e9+7;
  9.  
  10.  
  11. #define INPUT(x)                freopen(x,"r",stdin)
  12. #define OUTPUT(x)               freopen (x,"w",stdout)
  13.  
  14. #define YES                     cout << "YES\n"
  15. #define NO                      cout << "NO\n"
  16.  
  17. #define pb                      push_back
  18. #define endl                    '\n'
  19. #define LCM(a,b)                (a/__gcd(a,b))*b
  20. #define GCD(a,b)                __gcd(a,b)
  21. #define SP(x,y)                 setprecision((y))<<fixed<<(x)
  22. #define PI                      acos(-1.0)
  23. #define distance(x1,y1,x2,y2)   sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))
  24.  
  25. #define taratari                ios_base::sync_with_stdio(0);cin.tie(NULL);
  26.  
  27. int parent[100005];
  28.  
  29. void make_set(int numberOfComputers) {
  30.     for(int i = 1; i < numberOfComputers + 1; i++){
  31.         parent[i] = i;
  32.     }
  33. }
  34.  
  35. int find_set(int v) {
  36.     if (v == parent[v])
  37.         return v;
  38.     return find_set(parent[v]);
  39. }
  40.  
  41. void union_sets(int a, int b) {
  42.     a = find_set(a);
  43.     b = find_set(b);
  44.     if (a != b)
  45.         parent[b] = a;
  46. }
  47.  
  48.  
  49. int main()
  50. {
  51.     taratari
  52.  
  53.     int t;
  54.     cin>>t;
  55.  
  56.     string newLine;
  57.     scanf("%d",&t);
  58.     getline(cin,newLine);
  59.     getline(cin,newLine);
  60.  
  61.     while(t--){
  62.  
  63.         int numberOfComputers;
  64.         cin>>numberOfComputers;
  65.  
  66.         make_set(numberOfComputers);
  67.  
  68.         int ac = 0;
  69.         int rej = 0;
  70.  
  71.         string input;
  72.         stringstream ss;
  73.         string x;
  74.  
  75.         while(getline(cin,input)){
  76.  
  77.             string q;
  78.  
  79.             if(input.size() == 0)
  80.                 break;
  81.  
  82.             ss << input;
  83.  
  84.             int a = 0;
  85.             int b = 0;
  86.             int gonona = 0;
  87.  
  88.             while(ss >> x){
  89.                 if(gonona == 0){
  90.                     q = x;
  91.                 }
  92.                 if(gonona == 1){
  93.                     stringstream intValue(x);
  94.                     intValue >> a;
  95.                 }
  96.                 if(gonona == 2){
  97.                     stringstream intValue(x);
  98.                     intValue >> b;
  99.                 }
  100.                 gonona++;
  101.             }
  102.  
  103.             ss.clear();
  104.             input.clear();
  105.             x.clear();
  106.  
  107.  
  108.             if(q=="c")
  109.             {
  110.                 union_sets(a,b);
  111.             }
  112.             else
  113.             {
  114.                 if(find_set(a) != find_set(b)){
  115.                     rej++;
  116.                 }
  117.                 else{
  118.                     ac++;
  119.                 }
  120.             }
  121.  
  122.             printf("%d,%d", ac, rej);
  123.             if(t!=0){
  124.                 cout<<endl<<endl;
  125.             }
  126.  
  127.         }
  128.  
  129.  
  130.  
  131.  
  132.     }
  133.  
  134.     return 0;
  135.  
  136. }
  137.  
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement