Union FInd
\nclass UnionFind{\n        int n;\n        vector<int> parent;\n        vector<int> rank;\n        vector<unordered_set<int>> rest;\n       \n        vector<se
```\nclass UnionFind{\n        int n;\n        vector<int> parent;\n        vector<int> rank;\n        vector<unordered_set<int>> rest;\n       \n        vector<set<int>>& adj;\n        public:\n        UnionFind(int _n, vector<set<int>>& _adj) : n(_n), adj(_adj), parent(vector<int>(n, 0)), rank(vector<int>(n, 1)), rest(vector<unordered_set<int>>(_n))\n        {\n            for(int i=0; i<_n; i++)\n            {\n                parent[i]=i;\n                if(!adj[i].empty())\n                {\n                    rest[i].emplace(i);\n                }\n            }\n       }\n        \n        int find(int v)\n        {\n            if(v != parent[v])\n            {\n                parent[v] = find(parent[v]);\n            }\n            return parent[v];\n        }\n        \n        bool add(int v, int u)\n        {\n            \n            int pv = find(v);\n            int pu = find(u);\n            \n            \n            if(pv == pu)\n                return true;\n            \n            for(auto itr : rest[pv])\n            {\n                for(auto itr1 : rest[pu])\n                if(adj[itr1].count(itr))\n                    return false;\n            }\n            \n            auto& rpv = rank[pv];\n            auto& rpu = rank[pu];\n            \n            if(rpv >= rpu)\n            {\n                if(rpv == rpu)\n                    rpv++;\n                parent[pu] = pv;\n                rest[pv].insert(rest[pu].begin(), rest[pu].end());\n            }\n            else{\n                parent[pv] = pu;\n                rest[pu].insert(rest[pv].begin(), rest[pv].end());\n            }\n            find(v);find(u);\n            return true;\n        }\n    };\n\nclass Solution {\n    public:\n    vector<set<int>> adj;\n    \npublic:\n    vector<bool> friendRequests(int n, vector<vector<int>>& restrictions, vector<vector<int>>& requests) {\n        adj = vector<set<int>>(n);\n        for(auto r : restrictions)\n        {\n            adj[r[0]].emplace(r[1]);\n            adj[r[1]].emplace(r[0]);\n        }\n        UnionFind uf(n, adj);\n        vector<bool> ans(requests.size(), false);\n        for(int i=0; i<requests.size(); i++)\n        {\n            auto ret = uf.add(requests[i][0], requests[i][1]);\n            ans[i] = ret;\n        }\n        return ans;\n    }\n};\n```
[]