Tap here to switch tabs
Problem
Submissions

Problem: Number Of Ways To Reconstruct A Tree

Try to solve the Number Of Ways To Reconstruct A Tree problem.

Statement

You are given a 22D integer array pairs, where pairs[i] = [x_i, y_i] indicates that there is an ancestor or descendant relationship between nodes x_i and y_i in a rooted tree. That is, either x_i is an ancestor of y_i, or y_i is an ancestor of x_i.

A rooted tree is valid if:

  • It contains all nodes that appear in pairs.

  • For every pair [x_i, y_i] in pairs, either x_i is an ancestor of y_i or y_i is an ancestor of x_i.

  • For any two nodes not in pairs, neither is an ancestor of the other.

Return:

  • 00 if no valid rooted tree exists.

  • 11 if there is exactly 11 valid rooted tree.

  • 22 if there are 22 or more valid rooted trees.

Note: A node u is an ancestor of v if u lies on the path from the root to v and u \neq v.

Constraints:

  • 1≤1 \leq pairs.length ≤105\leq 10^5

  • 1≤1 \leq x_i << y_i ≤500\leq 500

  • The elements in pairs are unique.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Number Of Ways To Reconstruct A Tree

Try to solve the Number Of Ways To Reconstruct A Tree problem.

Statement

You are given a 22D integer array pairs, where pairs[i] = [x_i, y_i] indicates that there is an ancestor or descendant relationship between nodes x_i and y_i in a rooted tree. That is, either x_i is an ancestor of y_i, or y_i is an ancestor of x_i.

A rooted tree is valid if:

  • It contains all nodes that appear in pairs.

  • For every pair [x_i, y_i] in pairs, either x_i is an ancestor of y_i or y_i is an ancestor of x_i.

  • For any two nodes not in pairs, neither is an ancestor of the other.

Return:

  • 00 if no valid rooted tree exists.

  • 11 if there is exactly 11 valid rooted tree.

  • 22 if there are 22 or more valid rooted trees.

Note: A node u is an ancestor of v if u lies on the path from the root to v and u \neq v.

Constraints:

  • 1≤1 \leq pairs.length ≤105\leq 10^5

  • 1≤1 \leq x_i << y_i ≤500\leq 500

  • The elements in pairs are unique.