Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <sstream>
  6. #include <queue>
  7. #include <deque>
  8. #include <bitset>
  9. #include <iterator>
  10. #include <list>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. #include <functional>
  15. #include <numeric>
  16. #include <utility>
  17. #include <limits>
  18. #include <time.h>
  19. #include <math.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <assert.h>
  24.  
  25. using namespace std;
  26.  
  27. int N;
  28. int n_computers;
  29. int interconnected;
  30. int successful_answers, unsuccessful_answers;
  31. int computer_i, computer_j;
  32. char question_type;
  33. string s;
  34.  
  35. class UnionFind {
  36.   private:
  37.     vector<int> p, rank;
  38.     int num_sets;
  39.  
  40.   public:
  41.     UnionFind() {  }
  42.  
  43.     int find_set(int i) {
  44.       return (p[i] == i) ? i : (p[i] = find_set(p[i]));
  45.     }
  46.  
  47.     int is_same_set(int i, int j) {
  48.       return find_set(i) == find_set(j);
  49.     }
  50.  
  51.     void unite(int i, int j) {
  52.       if (is_same_set(i, j)) {
  53.         return;
  54.       }
  55.  
  56.       num_sets--;
  57.       int x = find_set(i);
  58.       int y = find_set(j);
  59.  
  60.       if  (rank[x] > rank[y]) {
  61.         p[y] = x;
  62.       } else {
  63.         p[x] = y;
  64.  
  65.         if (rank[x] == rank[y]) {
  66.           rank[y]++;
  67.         }
  68.       }
  69.     }
  70.  
  71.     int num_disjoint_sets() {
  72.       return num_sets;
  73.     }
  74.  
  75.     void set(int num) {
  76.       num_sets = num;
  77.       rank.assign(num+1, 0);
  78.       p.assign(num+1, 0);
  79.  
  80.       for (int i = 0; i <= num; i++) {
  81.         p[i] = i;
  82.       }
  83.     }
  84.  
  85.     void clear() {
  86.       rank.clear();
  87.       p.clear();
  88.     }
  89.  
  90. };
  91.  
  92. int main() {
  93.   UnionFind union_set;
  94.   scanf("%d\n", &N);
  95.  
  96.   while (N--) {
  97.     successful_answers = 0;
  98.     unsuccessful_answers = 0;
  99.  
  100.     scanf("%d\n", &n_computers);
  101.     union_set.set(n_computers);
  102.  
  103.     while (1) {
  104.       if(!getline(cin,s) || s.empty()) break;
  105.       sscanf(s.c_str(),"%c %d %d", &question_type, &computer_i, &computer_j);
  106.  
  107.       if (question_type == 'c') {
  108.         //printf("i: %d, j: %d\n", computer_i, computer_j);
  109.         //printf("before x: %d, y: %d\n", union_set.find_set(computer_i), union_set.find_set(computer_j));
  110.         union_set.unite(computer_i, computer_j);
  111.         //printf("before y: %d, y: %d\n", union_set.find_set(computer_i), union_set.find_set(computer_j));
  112.       } else {
  113.         interconnected = union_set.is_same_set(computer_i, computer_j);
  114.  
  115.         if (interconnected) {
  116.           successful_answers++;
  117.         } else {
  118.           unsuccessful_answers++;
  119.         }
  120.       }
  121.     }
  122.  
  123.     printf("%d,%d", successful_answers, unsuccessful_answers);
  124.     if (N) {
  125.       printf("\n\n");
  126.     }
  127.     union_set.clear();
  128.   }
  129.  
  130.   return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement