← 返回 snowflake 的题目列表Compute Final Allowed Privileges for Roles
类型:online_judge
In a role-permission system, there are n roles, each with its own initial allowed and disallowed privileges. Roles can inherit privileges from one another through a provided grants list. When a role A inherits another role B, A will acquire B's allowed privileges, but A's disallowed privileges will mask any conflicting privileges (i.e., privileges that are allowed but also in the disallowed list). Write an algorithm to compute the final allowed privileges for each role.
Parameters
n: Integer, total number of roles, indexed from 0 to n-1.
grants: 2D integer array, each element is a pair [from, to] indicating that the role to inherits privileges from the role from.
allowedList[i]: List of integers, denoting the initial allowed privileges for role i.
disallowedList[i]: List of integers, denoting the initial disallowed privileges for role i.
Output
Return the final allowed privileges list for each role, sorted in ascending order.
Example
Input
n = 2
grants = [[0, 1]]
allowedList = [[1, 2], [3]]
disallowedList = [[2], []]
Output
[[1], [1, 3]]
Constraints
1 <= n <= 1000
0 <= allowedList[i][j], disallowedList[i][j] <= 10^6
The given role inheritance system is valid and does not form cycles.
Example
Input
2
1
0 1
2 1
1 2
3
1 2