返回介绍

solution / 1900-1999 / 1993.Operations on Tree / README_EN

发布于 2024-06-17 01:03:12 字数 11980 浏览 0 评论 0 收藏 0

1993. Operations on Tree

中文文档

Description

You are given a tree with n nodes numbered from 0 to n - 1 in the form of a parent array parent where parent[i] is the parent of the ith node. The root of the tree is node 0, so parent[0] = -1 since it has no parent. You want to design a data structure that allows users to lock, unlock, and upgrade nodes in the tree.

The data structure should support the following functions:

  • Lock: Locks the given node for the given user and prevents other users from locking the same node. You may only lock a node using this function if the node is unlocked.
  • Unlock: Unlocks the given node for the given user. You may only unlock a node using this function if it is currently locked by the same user.
  • Upgrade: Locks the given node for the given user and unlocks all of its descendants regardless of who locked it. You may only upgrade a node if all 3 conditions are true:
    • The node is unlocked,
    • It has at least one locked descendant (by any user), and
    • It does not have any locked ancestors.

Implement the LockingTree class:

  • LockingTree(int[] parent) initializes the data structure with the parent array.
  • lock(int num, int user) returns true if it is possible for the user with id user to lock the node num, or false otherwise. If it is possible, the node num will become locked by the user with id user.
  • unlock(int num, int user) returns true if it is possible for the user with id user to unlock the node num, or false otherwise. If it is possible, the node num will become unlocked.
  • upgrade(int num, int user) returns true if it is possible for the user with id user to upgrade the node num, or false otherwise. If it is possible, the node num will be upgraded.

 

Example 1:

Input
["LockingTree", "lock", "unlock", "unlock", "lock", "upgrade", "lock"]
[[[-1, 0, 0, 1, 1, 2, 2]], [2, 2], [2, 3], [2, 2], [4, 5], [0, 1], [0, 1]]
Output
[null, true, false, true, true, true, false]

Explanation
LockingTree lockingTree = new LockingTree([-1, 0, 0, 1, 1, 2, 2]);
lockingTree.lock(2, 2); // return true because node 2 is unlocked.
// Node 2 will now be locked by user 2.
lockingTree.unlock(2, 3); // return false because user 3 cannot unlock a node locked by user 2.
lockingTree.unlock(2, 2); // return true because node 2 was previously locked by user 2.
// Node 2 will now be unlocked.
lockingTree.lock(4, 5); // return true because node 4 is unlocked.
// Node 4 will now be locked by user 5.
lockingTree.upgrade(0, 1); // return true because node 0 is unlocked and has at least one locked descendant (node 4).
// Node 0 will now be locked by user 1 and node 4 will now be unlocked.
lockingTree.lock(0, 1); // return false because node 0 is already locked.

 

Constraints:

  • n == parent.length
  • 2 <= n <= 2000
  • 0 <= parent[i] <= n - 1 for i != 0
  • parent[0] == -1
  • 0 <= num <= n - 1
  • 1 <= user <= 104
  • parent represents a valid tree.
  • At most 2000 calls in total will be made to lock, unlock, and upgrade.

Solutions

Solution 1

class LockingTree:
  def __init__(self, parent: List[int]):
    n = len(parent)
    self.locked = [-1] * n
    self.parent = parent
    self.children = [[] for _ in range(n)]
    for son, fa in enumerate(parent[1:], 1):
      self.children[fa].append(son)

  def lock(self, num: int, user: int) -> bool:
    if self.locked[num] == -1:
      self.locked[num] = user
      return True
    return False

  def unlock(self, num: int, user: int) -> bool:
    if self.locked[num] == user:
      self.locked[num] = -1
      return True
    return False

  def upgrade(self, num: int, user: int) -> bool:
    def dfs(x: int):
      nonlocal find
      for y in self.children[x]:
        if self.locked[y] != -1:
          self.locked[y] = -1
          find = True
        dfs(y)

    x = num
    while x != -1:
      if self.locked[x] != -1:
        return False
      x = self.parent[x]

    find = False
    dfs(num)
    if not find:
      return False
    self.locked[num] = user
    return True


# Your LockingTree object will be instantiated and called as such:
# obj = LockingTree(parent)
# param_1 = obj.lock(num,user)
# param_2 = obj.unlock(num,user)
# param_3 = obj.upgrade(num,user)
class LockingTree {
  private int[] locked;
  private int[] parent;
  private List<Integer>[] children;

  public LockingTree(int[] parent) {
    int n = parent.length;
    locked = new int[n];
    this.parent = parent;
    children = new List[n];
    Arrays.fill(locked, -1);
    Arrays.setAll(children, i -> new ArrayList<>());
    for (int i = 1; i < n; i++) {
      children[parent[i]].add(i);
    }
  }

  public boolean lock(int num, int user) {
    if (locked[num] == -1) {
      locked[num] = user;
      return true;
    }
    return false;
  }

  public boolean unlock(int num, int user) {
    if (locked[num] == user) {
      locked[num] = -1;
      return true;
    }
    return false;
  }

  public boolean upgrade(int num, int user) {
    int x = num;
    while (x != -1) {
      if (locked[x] != -1) {
        return false;
      }
      x = parent[x];
    }
    boolean[] find = new boolean[1];
    dfs(num, find);
    if (!find[0]) {
      return false;
    }
    locked[num] = user;
    return true;
  }

  private void dfs(int x, boolean[] find) {
    for (int y : children[x]) {
      if (locked[y] != -1) {
        locked[y] = -1;
        find[0] = true;
      }
      dfs(y, find);
    }
  }
}

/**
 * Your LockingTree object will be instantiated and called as such:
 * LockingTree obj = new LockingTree(parent);
 * boolean param_1 = obj.lock(num,user);
 * boolean param_2 = obj.unlock(num,user);
 * boolean param_3 = obj.upgrade(num,user);
 */
class LockingTree {
public:
  LockingTree(vector<int>& parent) {
    int n = parent.size();
    locked = vector<int>(n, -1);
    this->parent = parent;
    children.resize(n);
    for (int i = 1; i < n; ++i) {
      children[parent[i]].push_back(i);
    }
  }

  bool lock(int num, int user) {
    if (locked[num] == -1) {
      locked[num] = user;
      return true;
    }
    return false;
  }

  bool unlock(int num, int user) {
    if (locked[num] == user) {
      locked[num] = -1;
      return true;
    }
    return false;
  }

  bool upgrade(int num, int user) {
    int x = num;
    while (x != -1) {
      if (locked[x] != -1) {
        return false;
      }
      x = parent[x];
    }
    bool find = false;
    function<void(int)> dfs = [&](int x) {
      for (int y : children[x]) {
        if (locked[y] != -1) {
          find = true;
          locked[y] = -1;
        }
        dfs(y);
      }
    };
    dfs(num);
    if (!find) {
      return false;
    }
    locked[num] = user;
    return true;
  }

private:
  vector<int> locked;
  vector<int> parent;
  vector<vector<int>> children;
};

/**
 * Your LockingTree object will be instantiated and called as such:
 * LockingTree* obj = new LockingTree(parent);
 * bool param_1 = obj->lock(num,user);
 * bool param_2 = obj->unlock(num,user);
 * bool param_3 = obj->upgrade(num,user);
 */
type LockingTree struct {
  locked   []int
  parent   []int
  children [][]int
}

func Constructor(parent []int) LockingTree {
  n := len(parent)
  locked := make([]int, n)
  for i := range locked {
    locked[i] = -1
  }
  children := make([][]int, n)
  for i := 1; i < n; i++ {
    children[parent[i]] = append(children[parent[i]], i)
  }
  return LockingTree{locked, parent, children}
}

func (this *LockingTree) Lock(num int, user int) bool {
  if this.locked[num] == -1 {
    this.locked[num] = user
    return true
  }
  return false
}

func (this *LockingTree) Unlock(num int, user int) bool {
  if this.locked[num] == user {
    this.locked[num] = -1
    return true
  }
  return false
}

func (this *LockingTree) Upgrade(num int, user int) bool {
  x := num
  for ; x != -1; x = this.parent[x] {
    if this.locked[x] != -1 {
      return false
    }
  }
  find := false
  var dfs func(int)
  dfs = func(x int) {
    for _, y := range this.children[x] {
      if this.locked[y] != -1 {
        find = true
        this.locked[y] = -1
      }
      dfs(y)
    }
  }
  dfs(num)
  if !find {
    return false
  }
  this.locked[num] = user
  return true
}

/**
 * Your LockingTree object will be instantiated and called as such:
 * obj := Constructor(parent);
 * param_1 := obj.Lock(num,user);
 * param_2 := obj.Unlock(num,user);
 * param_3 := obj.Upgrade(num,user);
 */
class LockingTree {
  private locked: number[];
  private parent: number[];
  private children: number[][];

  constructor(parent: number[]) {
    const n = parent.length;
    this.locked = Array(n).fill(-1);
    this.parent = parent;
    this.children = Array(n)
      .fill(0)
      .map(() => []);
    for (let i = 1; i < n; i++) {
      this.children[parent[i]].push(i);
    }
  }

  lock(num: number, user: number): boolean {
    if (this.locked[num] === -1) {
      this.locked[num] = user;
      return true;
    }
    return false;
  }

  unlock(num: number, user: number): boolean {
    if (this.locked[num] === user) {
      this.locked[num] = -1;
      return true;
    }
    return false;
  }

  upgrade(num: number, user: number): boolean {
    let x = num;
    for (; x !== -1; x = this.parent[x]) {
      if (this.locked[x] !== -1) {
        return false;
      }
    }
    let find = false;
    const dfs = (x: number) => {
      for (const y of this.children[x]) {
        if (this.locked[y] !== -1) {
          this.locked[y] = -1;
          find = true;
        }
        dfs(y);
      }
    };
    dfs(num);
    if (!find) {
      return false;
    }
    this.locked[num] = user;
    return true;
  }
}

/**
 * Your LockingTree object will be instantiated and called as such:
 * var obj = new LockingTree(parent)
 * var param_1 = obj.lock(num,user)
 * var param_2 = obj.unlock(num,user)
 * var param_3 = obj.upgrade(num,user)
 */

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文