Determine Whether Two Chickens Are Blood Relatives
Problem statement
A chicken family is represented by direct parent lists. parents[i] contains every direct parent of chicken i. Parent edges form a directed acyclic graph.
Two chickens are blood relatives when their ancestor sets intersect. A chicken is included in its own ancestor set, so an ancestor and descendant are relatives. Merely sharing a child does not make two co-parents blood relatives.
Return whether first and second are blood relatives.
Function
areBloodRelatives(parents: int[][], first: int, second: int) → booleanExamples
Example 1
parents = [[],[],[0,1],[0,1]]first = 2second = 3return = trueThe two chickens share parents 0 and 1.
Example 2
parents = [[],[],[0,1]]first = 0second = 1return = falseThe two co-parents share a child but no ancestor.
Example 3
parents = [[],[0],[1]]first = 0second = 2return = trueChicken 0 is an ancestor of chicken 2.
Constraints
1 <= parents.length <= 200000.- Every listed parent is a valid node index.
- The total number of parent edges is at most
300000. - The parent graph is acyclic.
firstandsecondare valid indices.