Javascript - Union Find
Code\n\n/**\n * @param {number} n\n * @param {number[][]} restrictions\n * @param {number[][]} requests\n * @return {boolean[]}\n */\nvar friendRequests = funct
# Code\n```\n/**\n * @param {number} n\n * @param {number[][]} restrictions\n * @param {number[][]} requests\n * @return {boolean[]}\n */\nvar friendRequests = function (n, restrictions, requests) {\n  const ans = new Array(requests.length).fill(false);\n  const dsu = new DSU(n);\n\n  for (let i = 0; i < requests.length; i++) {\n    const [req1, req2] = requests[i];\n    const reqParent1 = dsu.find(req1);\n    const reqParent2 = dsu.find(req2);\n\n    let isOk = true;\n    if (reqParent1 !== reqParent2) {\n      for (const [res1, res2] of restrictions) {\n        const resParent1 = dsu.find(res1);\n        const resParent2 = dsu.find(res2);\n\n        const case1 = reqParent1 === resParent1 && reqParent2 === resParent2;\n        const case2 = reqParent1 === resParent2 && reqParent2 === resParent1;\n        if (case1 || case2) {\n          isOk = false;\n          break;\n        }\n      }\n    }\n\n    if (isOk) {\n      ans[i] = true;\n      dsu.union(req1, req2);\n    }\n  }\n\n  return ans;\n};\n\nclass DSU {\n  constructor(n) {\n    this.parent = new Array(n).fill();\n    for (let i = 0; i < n; i++) {\n      this.parent[i] = i;\n    }\n  }\n\n  find(x) {\n    if (this.parent[x] !== x) {\n      this.parent[x] = this.find(this.parent[x]);\n    }\n\n    return this.parent[x];\n  }\n\n  union(x, y) {\n    this.parent[this.find(x)] = this.parent[this.find(y)];\n  }\n}\n```
['Union Find', 'JavaScript']