C++ || Union-Find || Easy to Understand
Algorithm  ( Union Find with modification ) \n\n Process the request first \n Store Parent Array in some other temporary data structure.\n Merge the vertices/no
#### Algorithm  ( Union Find with modification ) \n\n* Process the request first \n* Store Parent Array in some other temporary data structure.\n* Merge the vertices/nodes \n* Check in the requests array if we have violated the conditions\n* If conditions are violated rollback the requests by updating parent array to the one stored in temporary data structure.\n\n\n```\nclass Solution {\npublic:\n    vector<int>parent;\n    int find(int node)\n    {\n        \n        if(node==parent[node])\n            return node;\n        \n        return parent[node]=find(parent[node]);\n    }\n    void merge(int u,int v)\n    {\n        int pu=find(u);\n        int pv=find(v);\n        parent[pu]=pv;\n    }\n    vector<bool> friendRequests(int n, vector<vector<int>>& restrictions, vector<vector<int>>& requests) {\n        \n        \n        \n        int i;\n        \n        parent.resize(n);\n        for(i=0;i<n;i++)\n            parent[i]=i;\n        \n        vector<bool>ans(requests.size());\n        \n       for(i=0;i<requests.size();i++)\n       {\n           int u=requests[i][0];\n           int v=requests[i][1];\n           \n           \n           auto temp_copy=parent;\n           ans[i]=true;\n           merge(u,v);\n           for(auto &it:restrictions)\n           {\n               \n               int p=it[0];\n               int q=it[1];\n               \n               if(find(p)==find(q))\n               {\n                   ans[i]=false;\n                   break;\n                }\n            }\n           if(!ans[i])\n               parent=temp_copy;\n               \n       }\n       \n        return ans;\n        \n    }\n};\n\n```\n \n### Pls upvote if you found helpful
['Union Find', 'C', 'C++']