Category: disjoint-set

Count of distinct groups of strings formed after performing equivalent operation

#include using namespace std;  int Find(vector& parent, int a){    return parent[a]           = (parent[a] == a ? a : Find(parent, parent[a]));}  void Union(vector& parent,           vector& rank, int a,           int b){          a = Find(parent, a);    b = Find(parent, b);          if (rank[a] == rank[b])        rank[a]++;    if (rank[a] > rank[b])        parent[b] = a;    else        parent[a] = b;}  void numOfDistinctStrings(string arr[],                          int N){            vector parent(27);          vector rank(27, 0);      for (int j = 0; j < 27; j++) {                parent[j] = […]

Continue Reading

Maximum element in connected component of given node for Q queries

  #include using namespace std;  int Find(vector& parent, int a){    return parent[a] = (parent[a] == a)                           ? a                           : (Find(parent, parent[a]));}  void Union(vector& parent,           vector& rank,           vector& maxValue,           int a, int b){      a = Find(parent, a);    b = Find(parent, b);      if (a == b)        return;          if (rank[a] == rank[b]) {        rank[a]++;    }      if (rank[a] < rank[b]) {        int temp = a;        a = b;        b = temp;    }      parent[b] = a;          maxValue[a] = max(maxValue[a],                      maxValue[b]);}  void findMaxValueOfSet(    vector& arr,    vector& queries, int […]

Continue Reading