C++ Union solution
IntuitionApproachUnion Find data structure for quick identifying of which group friend belongs toComplexity
Time complexity:
O(N * M * α(n)) (worst case)
Space
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->
Union Find data structure for quick identifying of which group friend belongs to

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(N * M * α(n)) (worst case)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(n + N)


N - number of requests
M - number of restrictions
n - total number of friends/users
α(n) - inverse Ackermann function

# Code
```cpp []
class UnionFind {
public:
    // initialise union
    UnionFind(int n) : parents(n, 0), size(n, 0) {
        for(int i = 0; i < n; ++i) {
            parents[i] = i;
            size[i] = 1;
        }
    }
    // find root parent of some node
    int find(int id) {
        if(parents[id] == id) return id;
        // find a root and rewrite it's root for each parent
        parents[id] = find(parents[id]);
        return parents[id];
    }
    // connect 2 groups of connected graph/tree
    void connect(int a, int b) {
        int rootA = find(a);
        int rootB = find(b);
        if(size[rootA] > size[rootB]) {
            swap(rootA, rootB);
        }
        parents[rootA] = rootB;
        size[rootB] += size[rootA];
    }
    bool isConnected(int a, int b) {
        // if their root parent is the same, they are connected
        return find(a) == find(b);
    }
private: 
    vector<int> parents;
    vector<long long> size;
};

class Solution {
public:
    vector<bool> friendRequests(int n, vector<vector<int>>& restrictions, vector<vector<int>>& requests) {
        // N number of requests
        int N = requests.size();
        vector<bool> ans(N);
        UnionFind uf(n);
        // process all requests in turn
        for(int i = 0; i < N; ++i) {
            // find parent of each friend, to see if they are connected
            int a = uf.find(requests[i][0]);
            int b = uf.find(requests[i][1]);
            // allowed to set a friendship
            bool allowed = true;
            // only continue if friends are not connected
            if(a != b) {
                // check their restrictions
                for(int j = 0; j < restrictions.size(); ++j) {
                    // p and q are groups of friends which are not allowed to be connected together
                    int p = uf.find(restrictions[j][0]);
                    int q = uf.find(restrictions[j][1]);
                    // make sure we check a for both groups and b for both groups
                    if((a == p && b == q) || (a == q && b == p)) {
                        allowed = false;
                    }
                }
            }
            // create a friendship between a and b if it's allowed
            if(allowed) {
                uf.connect(a, b);
            }
            // add to answer
            ans[i] = allowed;
        }
        return ans;
    }
};
```
['C++']