you give array pair pairsi xi yi andThere duplicatesxi yiLet way number root tree satisfy follow conditionsthe tree consist node value appear pairsA pair xi yi exist pair xi ancestor yi yi ancestor xiNote tree binary treeTwo way consider different node different parent waysreturn0 way 01 way 12 way 1A root tree tree single root node edge orient outgoing rootAn ancestor node node path root node exclude node the root ancestor Example 1input pair 1223output 1explanation there exactly valid rooted tree show figureexample 2input pair 122313output 2explanation there multiple valid root tree three show figuresexample 3input pair 12232415output 0explanation there valid rooted tree constraints1 pairslength 1051 xi yi 500the element pair unique