Formatted question description: https://leetcode.ca/all/1719.html

1719. Number Of Ways To Reconstruct A Tree

Level

Hard

Description

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 in pairs if and only if x_i is an ancestor of y_i or y_i is an ancestor of x_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:

  • 0 if ways == 0
  • 1 if ways == 1
  • 2 if ways > 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:

Image text

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:

Image text

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^5
  • 1 <= x_i < y_i <= 500
  • The elements in pairs are unique.

Solution

Loop over pairs. Use a 2D matrix to store whether two nodes have a relation of pairs. Use an array of lists to store the related nodes of each node. Then sort the array of lists according to the lists’ sizes in ascending order. Node i is an ancestor of node j if and only if the size of node i is greater than or equal to the size of node j. If the sizes are equal, then the two nodes can be swapped. Use this way to determine the final result.

class Solution {
    public int checkWays(int[][] pairs) {
        int maxValue = 0;
        for (int[] pair : pairs)
            maxValue = Math.max(maxValue, Math.max(pair[0], pair[1]));
        boolean[][] relations = new boolean[maxValue + 1][maxValue + 1];
        List<Integer>[] lists = new List[maxValue + 1];
        for (int i = 0; i <= maxValue; i++)
            lists[i] = new ArrayList<Integer>();
        for (int[] pair : pairs) {
            int value1 = pair[0], value2 = pair[1];
            relations[value1][value2] = true;
            relations[value2][value1] = true;
            lists[value1].add(value2);
            lists[value2].add(value1);
        }
        List<Integer> indices = new ArrayList<Integer>();
        for (int i = 1; i <= maxValue; i++) {
            if (!lists[i].isEmpty()) {
                indices.add(i);
                relations[i][i] = true;
            }
        }
        Collections.sort(indices, new Comparator<Integer>() {
            public int compare(Integer index1, Integer index2) {
                return lists[index1].size() - lists[index2].size();
            }
        });
        boolean equal = false;
        int rootCount = 0;
        int size = indices.size();
        for (int i = 0; i < size; i++) {
            int j = i + 1;
            while (j < size && !relations[indices.get(i)][indices.get(j)])
                j++;
            if (j < size) {
                if (lists[indices.get(i)].size() == lists[indices.get(j)].size())
                    equal = true;
                for (int value : lists[indices.get(i)]) {
                    if (!relations[value][indices.get(j)])
                        return 0;
                }
            } else
                rootCount++;
        }
        if (rootCount > 1)
            return 0;
        else
            return equal ? 2 : 1;
    }
}

All Problems

All Solutions