<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>

#define printf(...) (0)

struct Node {
    vector<int> vals;
    vector<Node *> next;
};

void printArray(const auto& A) {
#ifndef printf(...)
    for (int x : A) {
        printf("%d ", x);
    }
    printf("\n");
#endif
}

class Solution {
public:
    int checkWays(vector<vector<int>>& pairs) {
        int n = pairs.size();

        vector<unordered_set<int>> all_nodes_related(501);
        unordered_set<int> node_vals_set;
        for (auto &p : pairs) {
            all_nodes_related[p[1]].insert(p[0]);
            node_vals_set.insert(p[0]);
            node_vals_set.insert(p[1]);
        }
        vector<int> node_vals(node_vals_set.begin(), node_vals_set.end());
        sort(node_vals.begin(), node_vals.end());

        Node *sential_node = new Node;
        for (int node_val : node_vals) {
            printf("node_val: %d\n", node_val);
            auto &related = all_nodes_related[node_val];
            printf("related: "); printArray(related);
            printf("\n");
            unordered_set<int> seen_related;

            auto partition_node_vals_in_related = [&](Node *node) {
                assert(node != nullptr);
                vector<int> in_related, not_in_related;
                auto &vals = node->vals;
                assert(node == sential_node || vals.size() > 0);
                for (int val : vals) {
                    if (related.count(val)) {
                        seen_related.insert(val);
                        in_related.push_back(val);
                    } else {
                        not_in_related.push_back(val);
                    }
                }
                return make_pair(in_related, not_in_related);
            };

            Node *curr = sential_node;

            bool inserted = false;

            auto insert_into_curr_next = [&]() {
                assert(!inserted);
                inserted = true;
                // if (curr->next.size() == 0 && curr != sential_node) {
                //     curr->vals.push_back(node_val);
                //     return curr;
                //     // return nullptr;
                // } else {
                    Node *to_insert_node = new Node {
                        { node_val },
                        {}
                    };
                    curr->next.push_back(to_insert_node);
                    return to_insert_node;
                // }
            };

            auto insert_into_curr_vals = [&]() {
                if (curr == sential_node) {
                    Node *ret = insert_into_curr_next();
                    printf("curr == sential_node (only first node)\n");
                    return ret;
                } else {
                    assert(!inserted);
                    inserted = true;
                    curr->vals.push_back(node_val);
                    printf("insert at leaf's vals\n");
                }
                return curr;
            };

            auto split_curr = [&](vector<int>&& curr_vals, vector<int>&& new_next_vals) {
                Node *new_next = new Node {
                    move(new_next_vals),
                    {}
                };
                curr->next.swap(new_next->next);
                assert(curr->next.size() == 0);
                curr->next.push_back(new_next);
                curr->vals = move(curr_vals);
            };

            while (true) {
                printf("curr->vals: "); printArray(curr->vals);
                auto [curr_vals_in_related, curr_vals_not_in_related] = partition_node_vals_in_related(curr);
                assert(curr == sential_node || curr_vals_in_related.size() > 0);
                if (curr_vals_not_in_related.size() > 0) {
                    split_curr(move(curr_vals_in_related), move(curr_vals_not_in_related));
                    curr = insert_into_curr_next();
                    printf("insert at internal node's next after spliting vals\n");
                    break;
                }

                if (curr->next.size() == 0) {
                    assert(!inserted);
                    curr = insert_into_curr_vals();
                    printf("insert at leaf's vals\n");
                    break;
                }

                vector<Node *> next_some_in_related, next_none_in_related;
                for (Node *next_node : curr->next) {
                    printf("next_node->vals: "); printArray(next_node->vals);
                    auto [next_node_vals_in_related, next_node_vals_not_in_related] = partition_node_vals_in_related(next_node);
                    if (next_node_vals_in_related.size() > 0) {
                        next_some_in_related.push_back(next_node);
                    } else {
                        next_none_in_related.push_back(next_node);
                    }
                }

                if (next_some_in_related.size() == 0) {
                    curr = insert_into_curr_next();
                    printf("insert at internal node's next as leaf\n");
                    break;
                } else if (next_some_in_related.size() == 1) {
                    curr = next_some_in_related[0];
                } else if (next_none_in_related.size() > 0) {
                    curr->next = next_none_in_related;
                    curr = insert_into_curr_next();
                    curr->next = next_some_in_related;
                    printf("insert at internal node's next as internal node after spliting next\n");
                    // printf("curr->next[0]->vals: "); printArray(curr->next[0]->vals);
                    // printf("curr->next[1]->vals: "); printArray(curr->next[1]->vals);
                    break;
                } else {
                    curr->next.resize(0); // TODO: try clear
                    curr = insert_into_curr_vals();
                    curr->next = next_some_in_related;
                    printf("insert at internal node's vals without spliting\n");
                    break;
                }
                printf("\n");
            }
            printf("\n");


            queue<Node *> todo(curr->next.begin(), curr->next.end());
            // todo.push(curr);
            while (!todo.empty()) {
                Node *todo_exploring = todo.front();
                todo.pop();

                auto [in_related, not_in_related] = partition_node_vals_in_related(todo_exploring);
                if (not_in_related.size() > 0) {
                    printf("return 0: has non related descendant\n");
                    printf("in_related: "); printArray(in_related);
                    printf("not_in_related: "); printArray(not_in_related);
                    return 0;
                }

                for (Node *next : todo_exploring->next) {
                    todo.push(next);
                }
            }
            
            printf("related: "); printArray(related);
            printf("seen_related: "); printArray(seen_related);

            assert(seen_related.size() <= related.size());
            if (seen_related.size() != related.size()) {
                printf("return 0: did not see all related\n");
                return 0;
            }

            printf("==========\n");
        }
        if (sential_node->next.size() != 1) {
            printf("return 0: multiple roots\n");
            return 0;
        }

        queue<Node *> todo;
        todo.push(sential_node->next[0]);
        while (!todo.empty()) {
            Node *todo_exploring = todo.front();
            todo.pop();
            if (todo_exploring->vals.size() > 1) {
                return 2;
            }
            for (Node *next : todo_exploring->next) {
                todo.push(next);
            }
        }

        return 1;
    }
};
234600