call(back)
Algorithms & Data StructuresmediumSign in to star problems

DAG Permissions II

Each node of a DAG is [name, allow, disallow]: a name and two strings of distinct lowercase letters. Edges are [parent, child] pairs of names.

A letter is allowed at a node when the node itself or any ancestor allows it — allows flow all the way down. A disallow does not flow: it removes letters from its own node only, and the node still passes those letters on.

Return [name, letters] for every node, sorted by name, where letters is the node's effective letters in alphabetical order ("" when it has none).

effectiveAllow([["a", "ab", ""], ["b", "c", "b"], ["c", "", ""]], [["a", "b"], ["b", "c"]])
  ->  [["a", "ab"], ["b", "ac"], ["c", "abc"]]

b drops its own b but still hands it down, so c ends up with abc.

Hints

Hint 1

Topological order again: a node's allow set is complete once every parent's is, so OR each parent's set into the child as the parent finishes.

Hint 2

Keep the set that flows separate from the set you report: children inherit the allows, not the result after the disallow.

Topics

Asked at

Similar Problems6