<p>You are given an integer array of unique positive integers <code>nums</code>. Consider the following graph:</p>

<ul>
	<li>There are <code>nums.length</code> nodes, labeled <code>nums[0]</code> to <code>nums[nums.length - 1]</code>,</li>
	<li>There is an undirected edge between <code>nums[i]</code> and <code>nums[j]</code> if <code>nums[i]</code> and <code>nums[j]</code> share a common factor greater than <code>1</code>.</li>
</ul>

<p>Return <em>the size of the largest connected component in the graph</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/01/ex1.png" style="width: 500px; height: 97px;" />
<pre>
<strong>Input:</strong> nums = [4,6,15,35]
<strong>Output:</strong> 4
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/01/ex2.png" style="width: 500px; height: 85px;" />
<pre>
<strong>Input:</strong> nums = [20,50,9,63]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/01/ex3.png" style="width: 500px; height: 260px;" />
<pre>
<strong>Input:</strong> nums = [2,3,6,7,4,12,21,39]
<strong>Output:</strong> 8
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
	<li>All the values of <code>nums</code> are <strong>unique</strong>.</li>
</ul>

class Solution {
public:
    int largestComponentSize(vector<int>& nums) {
        
        int maxNum = 0;
        unordered_map<int, vector<int>> factorToNum;
        for(int num : nums)
        {
            maxNum = max(maxNum, num);

            int tmp = num;
            //find all factors
            int maxFactor = sqrt(num);
            bool isPrime = true;
            for(int i = 2; i <= maxFactor; ++i)
            {
                if(tmp % i == 0)
                {
                    isPrime = false;
                    factorToNum[i].push_back(num);
                    while(tmp % i == 0)
                    {
                        tmp = tmp / i;
                    }

                    if(tmp == 1)
                    {
                        break;
                    }
                }
            }

            if(tmp > 1)
            {
                factorToNum[tmp].push_back(num);
            }

            if(isPrime)
            {
                factorToNum[num].push_back(num);
            }
        }

        UnionFind uf(maxNum + 1);

        for(const auto& commonFNums : factorToNum)
        {
            //cout << "Common factor: " << commonFNums.first << endl;
            int a = commonFNums.second.back();

            for(int b : commonFNums.second)
            {
                if(a == b)
                {
                    continue;
                }

                uf.unionSet(a, b);
            }    
        }

        vector<int> rootCount(maxNum+1, 0);
        for(int val : nums)
        {
            ++rootCount[uf.find(val)];
        }

        return *max_element(rootCount.begin(), rootCount.end());
    }

    class UnionFind
    {
    public:
        UnionFind(int num) : root(num), rank(num, 1)
        {
            for(int i = 0; i < num; ++i)
            {
                root[i] = i;
            }
        }

        int find(int val)
        {
            if(val == root[val])
            {
                return val;
            }
            return root[val] = find(root[val]);
        }

        bool connected(int a, int b)
        {
            return find(a) == find(b);
        }

        void unionSet(int a, int b)
        {
            int rootA = find(a);
            int rootB = find(b);

            if(rootA == rootB)
            {
                return;
            }

            if(rank[rootA] > rank[rootB])
            {
                root[rootB] = rootA;
            }
            else if(rank[rootA] < rank[rootB])
            {
                root[rootA] = rootB;
            }
            else
            {
                root[rootB] = rootA;
                ++rank[rootA];
            }

        }

    private:
        vector<int> root;
        vector<int> rank;
    };
};
105466