java union find
```\n\nclass UF{\n    public int[] parent;\n    public int[] rank;\n    public UF(int n){\n        parent = new int[n];\n        rank  = new int[n];\n        fo
```\n\nclass UF{\n    public int[] parent;\n    public int[] rank;\n    public UF(int n){\n        parent = new int[n];\n        rank  = new int[n];\n        for(int i  = 0; i <parent.length;i++){\n            parent[i] = i;\n            rank[i] = 0;\n        }\n    }\n    public int find(int x){\n        if(x != parent[x]){\n            parent[x] = find(parent[x]);\n        }\n        return parent[x];\n    }\n    public boolean union(int x, int y){\n        int xp = find(x); int yp = find(y);\n        if(xp==yp){\n            return true;\n        }\n        if(rank[xp] > rank[yp]){\n            parent[yp] = xp;\n        }\n        else if(rank[yp]>rank[xp]){\n            parent[xp] = yp;\n        }\n        else{\n            parent[xp] = yp;\n            rank[yp] += 1;\n        }\n        return false;\n        \n    }\n}\n\nclass Solution {\n    public boolean[] friendRequests(int n, int[][] restrictions, int[][] requests) {\n        UF dsu = new UF(n);\n        boolean[] res = new boolean[requests.length];\n        int r = restrictions.length;\n        int cnt = 0;\n        for(int[] pair : requests){\n            int f1 = pair[0]; int f2 = pair[1];\n            int f1p = dsu.find(f1); int f2p = dsu.find(f2);\n            if(f1p == f2p){\n                res[cnt] = true;\n            }\n            else{\n                boolean flag = true;\n                for(int i = 0; i < r; i++){\n                int x = restrictions[i][0]; int y = restrictions[i][1];\n                int xp = dsu.find(x); int yp = dsu.find(y);\n                if((xp == f1p && yp == f2p)| (xp==f2p && yp==f1p))\n                {\n                    flag = false;\n                    break;\n                }\n                \n            }\n                res[cnt] = flag;\n            }\n            if(res[cnt] == true){\n                dsu.union(f1,f2);\n            }\n            \n            cnt ++;\n        }\n        return res;\n    }\n}
[]