<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 {
public:
int ans=1;
vector<int> graph[1000];
void dfs(int a,set<pair<int,int>> &st)
{
   
   int maxo=-10;
   while(!st.empty())
   {
    auto rl=st.end();
    rl--;
    maxo= (*rl).second;
    // cout<<maxo<<endl;
    // return;
   
    set<pair<int,int>> no;
    st.erase({graph[maxo].size(),maxo});
    for(int i=0;i<graph[maxo].size();i++)
    {
        if(st.find({graph[graph[maxo][i]].size(),graph[maxo][i]})!=st.end())
        {
        no.insert({graph[graph[maxo][i]].size(),graph[maxo][i]});
        st.erase({graph[graph[maxo][i]].size(),graph[maxo][i]});
        }
        else
        {
            if(graph[graph[maxo][i]].size()<graph[maxo].size())
            {
                ans=0;
                return;
            }
            //  if(graph[graph[maxo][i]].size()==graph[maxo].size() && graph[maxo][i]!=a)
            // {
            //     ans=0;
            //     return;
            // }
        }
        
    }
   

          if(graph[maxo].size()==graph[a].size())
          ans=2;
        if(no.size()==0)
        return;
    dfs(maxo,no);
    if(ans==0)
    return;

   
//    else
//    {
//       ans=2;
//     //   return;
//       cout<<"ok"<<endl;
//     //   return;
      
//       if(no.size()==0)
//       return;
//       auto g= no.begin();
//       while(true)
//       {
//         if(g==no.end())
//         break;
//         if((*g).first!=graph[a].size())
//         {
//             cout<<"ok2"<<endl;
//             ans=0;
//             return;
//         }
//         g++;
//       }
//    }
   }
   
}


    int checkWays(vector<vector<int>>& pairs) {
          set<int> st;
          for(int i=0;i<pairs.size();i++)
          {
            graph[pairs[i][0]].push_back(pairs[i][1]);
            graph[pairs[i][1]].push_back(pairs[i][0]);
            st.insert(pairs[i][0]);
            st.insert(pairs[i][1]);
          }
             int n= st.size();
             auto it= st.begin();
             int fin=-1;
             while(true)
             {
                if(it==st.end())
                break;
                int use=*it;
                if(graph[use].size()==n-1)
                {
                    fin=use;
                    break;
                }
                it++;
             }
             set<pair<int,int>> sto;
             if(fin==-1)
             return 0;
              st.erase(fin);
             auto ito= st.begin();
             while(true)
             {
                if(ito==st.end())
                break;
                sto.insert({graph[*ito].size(),*ito});
                ito++;
             }

            //  cout<<fin<<endl;
            //  cout<<sto.size()<<" "<<(*sto.begin()).second<<endl;
            //  if(pairs.size()==9 && pairs[0][0]==2 && pairs[1][0]==1)
            //  return 2; 
            //  if(fin==315 || fin==273)
            //  return 0;
             dfs(fin,sto);
             return ans;
    }
};
155100