Number Of Ways To Reconstruct A Tree Problem
Description
LeetCode Problem 1719.
You are given an array pairs, where pairs[i] = [x_i, y_i], and:
- There are no duplicates.
x_i < y_i
Let ways be the number of rooted trees that satisfy the following conditions:
- The tree consists of nodes whose values appeared in
pairs. - A pair
[x_i, y_i]exists inpairsif and only ifx_iis an ancestor ofy_iory_iis an ancestor ofx_i. - Note: the tree does not have to be a binary tree.
Two ways are considered to be different if there is at least one node that has different parents in both ways.
Return:
0ifways == 01ifways == 12ifways > 1
A rooted tree is a tree that has a single root node, and all edges are oriented to be outgoing from the root.
An ancestor of a node is any node on the path from the root to that node (excluding the node itself). The root has no ancestors.
Example 1:

Input: pairs = [[1,2],[2,3]]
Output: 1
Explanation: There is exactly one valid rooted tree, which is shown in the above figure.
Example 2:

Input: pairs = [[1,2],[2,3],[1,3]]
Output: 2
Explanation: There are multiple valid rooted trees. Three of them are shown in the above figures.
Example 3:
Input: pairs = [[1,2],[2,3],[2,4],[1,5]]
Output: 0
Explanation: There are no valid rooted trees.
Constraints:
1 <= pairs.length <= 10^51 <= x_i< y_i <= 500- The elements in
pairsare unique.
Solution
Approach: Degrees and Ancestors
A pair [x, y] exists exactly when one of the two nodes is an ancestor of the other. So a node's number of neighbours is the size of its subtree minus one, plus its number of ancestors: the closer to the root, the larger the degree. The root must be adjacent to every node.
Count the neighbours of every node, then give each node (other than the root) a parent: the neighbour with the smallest degree that is still at least as large as the node's own degree. A node with no such neighbour must be the root, and it has to be adjacent to all other nodes; otherwise no tree exists and the answer is 0.
Next, check that the parent really is an ancestor of everything the node touches: every neighbour of the node must also be a neighbour of its parent (or the parent itself). If any neighbour is missing, the answer is 0.
If a node and its parent have the same degree, the two can swap places and still give the same pairs, so there is more than one tree and the answer is 2. Otherwise the tree is unique and the answer is 1.
Complexity: O(n^2) time in the worst case and O(n^2) space for the adjacency sets, where n is the number of distinct nodes.
from collections import defaultdict
class Solution:
def checkWays(self, pairs: List[List[int]]) -> int:
g = [[False] * 510 for _ in range(510)]
v = defaultdict(list)
for x, y in pairs:
g[x][y] = g[y][x] = True
v[x].append(y)
v[y].append(x)
nodes = []
for i in range(510):
if v[i]:
nodes.append(i)
g[i][i] = True
nodes.sort(key=lambda x: len(v[x]))
equal = False
root = 0
for i, x in enumerate(nodes):
j = i + 1
while j < len(nodes) and not g[x][nodes[j]]:
j += 1
if j < len(nodes):
y = nodes[j]
if len(v[x]) == len(v[y]):
equal = True
for z in v[x]:
if not g[y][z]:
return 0
else:
root += 1
if root > 1:
return 0
return 2 if equal else 1