Why did my C++ union find solution get TLE?
My solution during the contest is similar to several solutions which got accepted. Is my Union find implementation slow?\n\n//------DSU---------------------//\n
My solution during the contest is similar to several solutions which got accepted. Is my Union find implementation slow?\n```\n//------DSU---------------------//\n#define ll int\nclass DSU\n{\n    public:\n    vector <ll> parent,rnk;\n    DSU(ll n)\n    {\n        parent.resize(n+5,0);\n        rnk.resize(n+5,0);\n        for(ll i=0;i<n+5;i++)\n         parent[i]=i;\n    }\n    ll find_set(ll v)\n    {\n        if(parent[v]==v)\n         return v;\n        return parent[v]=find_set(parent[v]); \n    }\n    void union_sets(ll a,ll b)\n    {\n        a=find_set(a);\n        b=find_set(b);\n        if(a!=b)\n        {\n            if(rnk[a]<rnk[b])\n            swap(a,b);\n            parent[b]=a; \n            if(rnk[a]==rnk[b])\n             rnk[a]++; \n        }\n    }\n};\n//------DSU---------------------//\nclass Solution {\npublic:\n    vector<bool> friendRequests(int n, vector<vector<int>>& restrictions, vector<vector<int>>& requests) {\n        DSU ds(n);\n        vector <bool> ans;\n        for(auto u:requests){\n            int x=u[0],y=u[1];\n            x=ds.find_set(x);\n            y=ds.find_set(y);\n            if(x==y){\n                ans.push_back(true);\n                continue;\n            }\n            bool flag=true;\n            for(auto uu:restrictions){\n                int p=ds.find_set(uu[0]);\n                int q=ds.find_set(uu[1]);\n                if((p==x and q==y) or (p==y and q==x)){\n                    flag=false;\n                    break;\n                }\n            }\n            ans.push_back(flag);\n            if(flag){\n                ds.union_sets(x,y);\n            }\n        }\n        return ans;\n    }\n};\n\n```\n\nEdit: I found why I am getting TLE. I was creating copies of the vectors in restrictions everytime, because I was not iterating by reference.
[]