<p>You are given an array <code>pairs</code>, where <code>pairs[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>, and:</p>

<ul>
	<li>There are no duplicates.</li>
	<li><code>x<sub>i</sub> &lt; y<sub>i</sub></code></li>
</ul>

<p>Let <code>ways</code> be the number of rooted trees that satisfy the following conditions:</p>

<ul>
	<li>The tree consists of nodes whose values appeared in <code>pairs</code>.</li>
	<li>A pair <code>[x<sub>i</sub>, y<sub>i</sub>]</code> exists in <code>pairs</code> <strong>if and only if</strong> <code>x<sub>i</sub></code> is an ancestor of <code>y<sub>i</sub></code> or <code>y<sub>i</sub></code> is an ancestor of <code>x<sub>i</sub></code>.</li>
	<li><strong>Note:</strong> the tree does not have to be a binary tree.</li>
</ul>

<p>Two ways are considered to be different if there is at least one node that has different parents in both ways.</p>

<p>Return:</p>

<ul>
	<li><code>0</code> if <code>ways == 0</code></li>
	<li><code>1</code> if <code>ways == 1</code></li>
	<li><code>2</code> if <code>ways &gt; 1</code></li>
</ul>

<p>A <strong>rooted tree</strong> is a tree that has a single root node, and all edges are oriented to be outgoing from the root.</p>

<p>An <strong>ancestor</strong> of a node is any node on the path from the root to that node (excluding the node itself). The root has no ancestors.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img src="https://assets.leetcode.com/uploads/2020/12/03/trees2.png" style="width: 208px; height: 221px;" />
<pre>
<strong>Input:</strong> pairs = [[1,2],[2,3]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> There is exactly one valid rooted tree, which is shown in the above figure.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/03/tree.png" style="width: 234px; height: 241px;" />
<pre>
<strong>Input:</strong> pairs = [[1,2],[2,3],[1,3]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are multiple valid rooted trees. Three of them are shown in the above figures.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> pairs = [[1,2],[2,3],[2,4],[1,5]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There are no valid rooted trees.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= pairs.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= x<sub>i </sub>&lt; y<sub>i</sub> &lt;= 500</code></li>
	<li>The elements in <code>pairs</code> are unique.</li>
</ul>

class Solution:
    # initialization 
    def __init__(self) : 
        self.graph = collections.defaultdict(set)
        self.ancestors = set()
        self.rank_needed = 0

    # add edge to graph 
    def add_edge(self, source, dest) : 
        self.graph[source].add(dest)
        self.graph[dest].add(source)

    # build graph from a list of edges 
    def build_graph_from_edges(self, edges) :
        # loop over edges  
        for edge in edges :
            # call build with unpacked edge  
            self.add_edge(*edge)
        # when done, set rank needed 
        self.rank_needed = len(self.graph) - 1
    
    # return the node rank of a node in our graph as the length of the neighbors 
    def get_node_rank(self, node_id) : 
        return len(self.graph[node_id])

    # get a sorted version of the graph from greatest to least neighbors 
    def get_sorted_graph(self) : 
        return sorted(self.graph, key=lambda node_id : len(self.graph[node_id]), reverse=True)

    # get the immediate ancestor of our given node by 
    # taking the union of the ancestors so far with the neighbors of the given node 
    # as the minmal of the sorted list of intersections based on the size of the neighbors 
    def get_immediate_ancestor(self, node_id) : 
        return min(self.ancestors & self.graph[node_id], key = lambda node_id : len(self.graph[node_id]), default=None)

    # process a sorted version of the graph for the given problem
    def process_sorted_graph(self) : 
        # what we are asked to find 
        ways = 1 
        # sort the nodes by number of neighbors for faster run time 
        for node in self.get_sorted_graph(): 
            # add this node to our set of ancestors in case we need them later 
            self.ancestors.add(node)
            # get this nodes rank 
            node_rank = self.get_node_rank(node)
            # get the immediate ancestor, aka parent 
            parent = self.get_immediate_ancestor(node)
            # if you did not get the immediate ancestor, we may have a problem 
            if parent is None : 
                # if we did not have an immediate ancestor and we are not a possible root
                if node_rank != self.rank_needed : 
                    # then this precludes there ever being a return that is not 0 
                    return 0
                else : 
                    # otherwise, keep going to other nodes, this might just be a child 
                    continue
            else : 
                # if parent is not None 
                # get parent rank for comparisons between the two 
                parent_rank = self.get_node_rank(parent)
                # if the set difference of the neighbors of node with either the neighbors of parent or the parent itself is not 0
                # then we have a situation where we do not have a tree line. As soon as one case shows this, none are possible, so return 0 
                if self.graph[node] - (self.graph[parent] | {parent}): return 0 
                # otherwise, if the length of the neighbors of node matches the length of the neighbors of parent, 
                # we have two possibilities for the root and can set ways to 2 for now. We may run across a case later that negates this 
                if node_rank == parent_rank: ways = 2
        # if you have not returned yet, return ways. It is either 2 or 1. 
        return ways 

    def checkWays(self, pairs: List[List[int]]) -> int:
        # build graph function for bidirectional 
        self.build_graph_from_edges(pairs)
        # return the result of processing the sorted graph 
        return self.process_sorted_graph()
47700