C++ | DSU | Check in restriction before allowing them to be friends
cpp\nclass DSU {\npublic:\n    vector<int> parent, rank;\n    DSU(int N) {\n        for (int i = 0; i < N + 5; ++i) {\n            parent.push_back(i);\n       
```cpp\nclass DSU {\npublic:\n    vector<int> parent, rank;\n    DSU(int N) {\n        for (int i = 0; i < N + 5; ++i) {\n            parent.push_back(i);\n            rank.push_back(1);\n        }\n    }\n    \n    int find(int u) {\n        if (parent[u] == u) return u;\n        return parent[u] = find(parent[u]);\n    }\n    \n    void merge(int x, int y) {\n        int rootX = find(x);\n        int rootY = find(y);\n        if (rootX == rootY) return ;\n        if (rank[rootX] >= rank[rootY]) {\n            parent[rootY] = rootX;\n            rank[rootX] += rank[rootY];\n        } else {\n            parent[rootX] = rootY;\n            rank[rootY] += rank[rootX];\n        }\n    }\n    \n    bool isConnected(int x, int y) {\n        return find(x) == find(y);\n    }\n};\n\nclass Solution {\npublic:\n    vector<bool> friendRequests(int n, vector<vector<int>>& restrictions, vector<vector<int>>& requests) {\n        DSU dsu(n);\n        vector<bool> res;\n        for (const auto &req: requests) {\n            int p1 = dsu.find(req[0]);\n            int p2 = dsu.find(req[1]);\n            //If they are friends, then they will always be friends\n            if(p1 == p2) {\n                res.push_back(true);\n                continue;\n            }\n            bool flag = true;\n            for(const auto &rest : restrictions) {\n                int x1 = dsu.find(rest[0]);\n                int x2 = dsu.find(rest[1]);\n\t\t\t\t//Since in DSU, parents can also be reversed\n\t\t\t\t//hence we check for the reverse as well\n                if(p1 == x1 && p2 == x2 || p1 == x2 && p2 == x1) {\n                    flag = false;\n                    break;\n                } \n            }\n            if(flag) {\n                dsu.merge(req[0], req[1]);\n            }\n            res.push_back(flag);\n        }\n        return res;\n    }\n};\n```
[]