<p>We want to split a group of <code>n</code> people (labeled from <code>1</code> to <code>n</code>) into two groups of <strong>any size</strong>. Each person may dislike some other people, and they should not go into the same group.</p>

<p>Given the integer <code>n</code> and the array <code>dislikes</code> where <code>dislikes[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that the person labeled <code>a<sub>i</sub></code> does not like the person labeled <code>b<sub>i</sub></code>, return <code>true</code> <em>if it is possible to split everyone into two groups in this way</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 4, dislikes = [[1,2],[1,3],[2,4]]
<strong>Output:</strong> true
<strong>Explanation:</strong> The first group has [1,4], and the second group has [2,3].
</pre>

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

<pre>
<strong>Input:</strong> n = 3, dislikes = [[1,2],[1,3],[2,3]]
<strong>Output:</strong> false
<strong>Explanation:</strong> We need at least 3 groups to divide them. We cannot put them in two groups.
</pre>

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

<ul>
	<li><code>1 &lt;= n &lt;= 2000</code></li>
	<li><code>0 &lt;= dislikes.length &lt;= 10<sup>4</sup></code></li>
	<li><code>dislikes[i].length == 2</code></li>
	<li><code>1 &lt;= a<sub>i</sub> &lt; b<sub>i</sub> &lt;= n</code></li>
	<li>All the pairs of <code>dislikes</code> are <strong>unique</strong>.</li>
</ul>


class DisjointSet{
    
public:
    vector<int> size, parent;
    DisjointSet(int n){
        size.resize(n+1, 1);
        parent.resize(n+1);
        for(int i=1; i<=n; i++){
            parent[i]=i;
        }
    }

    //finds ultimate parent in O(1 ) approx
    int findUPar(int node){
        if(parent[node] == node){
            return node;
        }
        return parent[node] = findUPar(parent[node]);
    }

    // this also takes O(1) 
    void unionBySize(int u, int v){
        int ult_u = findUPar(u);
        int ult_v = findUPar(v);

        if(ult_u == ult_v) return; // as its alredy unioned

        //competition comes now, which goes under whom
        if(size[ult_u] < size[ult_v]){
            //smaller goes under bigger guy
            parent[ult_u] = ult_v;
            size[ult_v]+=size[ult_u]; // size of UltV grows due to above
        }
        else{
            parent[ult_v] = ult_u;
            size[ult_u] +=size[ult_v];
        }

    }

};

class Solution {
public:
    bool possibleBipartition(int n, vector<vector<int>>& dislikes) {

        //edge case
        if(n == 1) return true;

        vector<int> adj[n+1];
        DisjointSet ds(n); 

        //first create adj list
        // {node} -> {set of disliked nodes}
        for(auto e : dislikes){
            int u = e[0];
            int v = e[1];

            adj[u].push_back(v);
            adj[v].push_back(u);
        }

        //create trees of disjoint set
        //basically keeping all disliked in one tree 

        for(int i=1; i<=n; i++){
            vector<int> disNodes = adj[i];
            if(disNodes.size() == 0) continue; // skip to avoid seg errs
            
            int first = disNodes[0]; //we union with this guy all other disliked guys

            for(int j=1; j<disNodes.size(); j++){
                ds.unionBySize(first, disNodes[j]);
            }
        }

        //now see is there any 2 disliked node with same BOSS
        for(auto d : dislikes){
            if(ds.findUPar(d[0]) == ds.findUPar(d[1])){
                return false;
            }
        }
        return true;
    }
};
80946