C++ | Union Find | Simple reset approach
Approach\n Describe your approach to solving the problem. \nIf any request is causing a restriction to happen, reset disjointSet to previous state\n\n\n# Code\n
# Approach\n<!-- Describe your approach to solving the problem. -->\nIf any request is causing a restriction to happen, reset disjointSet to previous state\n\n\n# Code\n```\nclass DisjointSet {\npublic:\n    vector<int>  parent, size;\n\n    DisjointSet(int n) {\n        parent.resize(n );\n        size.resize(n );\n        for (int i = 0; i < n; i++) {\n            parent[i] = i;\n            size[i] = 1;\n        }\n    }\n\n    int findUPar(int node) {\n        if (node == parent[node])\n            return node;\n        return parent[node] = findUPar(parent[node]);\n    }\n\n   \n    void unionBySize(int u, int v) {\n        int ulp_u = findUPar(u);\n        int ulp_v = findUPar(v);\n        if (ulp_u == ulp_v) return;\n        if (size[ulp_u] < size[ulp_v]) {\n            parent[ulp_u] = ulp_v;\n            size[ulp_v] += size[ulp_u];\n        }\n        else {\n            parent[ulp_v] = ulp_u;\n            size[ulp_u] += size[ulp_v];\n        }\n    }\n};\n\nclass Solution {\npublic:\n    vector<bool> friendRequests(int n, vector<vector<int>>& rest, vector<vector<int>>& req) {\n        vector<bool> ans;\n        DisjointSet ds(n);\n        for(int i=0;i<req.size();i++){\n            int u = req[i][0];\n            int v = req[i][1];\n\n            DisjointSet prev=ds;\n            ds.unionBySize(u,v);\n            bool restricted=false;\n\n            for(int j =0 ; j<rest.size();j++){\n                int x=rest[j][0];\n                int y=rest[j][1];\n                if(ds.findUPar(x)==ds.findUPar(y)){\n                    restricted=true;\n                    break;\n                }\n\n            }\n\n            if(restricted){\n                //reset disjoint set\n                ds=prev;\n                ans.push_back(false);\n            }else ans.push_back(true);\n\n        }\n\n\n\n\n        return ans;\n    }\n};\n```
['C++']