<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) {
        // Find the set of all prime numbers.
        std::vector<unsigned> primes;
        std::unordered_map<unsigned, size_t> primeToIndex;
        {
            std::bitset<100001> nonPrime;
            for (size_t i = 2; i < nonPrime.size(); ++i) {
                if (!nonPrime[i]) {
                    for (size_t j = i << 1; j < nonPrime.size(); j += i) {
                        nonPrime[j] = true;
                    }
                }
            }
            for (size_t i = 2; i < nonPrime.size(); ++i) {
                if (!nonPrime[i]) {
                    primeToIndex[i] = primes.size();
                    primes.push_back(i);
                }
            }
        }

        // Create a union-find structure to relate primes.
        size_t n = primes.size();
        std::vector<size_t> parent(n);
        std::vector<unsigned> depth(n);
        std::vector<unsigned> count(n);
        for (size_t i = 0; i < n; ++i) {
            parent[i] = i;
        }

        std::function<size_t(size_t)> find = [&](size_t node) {
            size_t root = node;
            while (parent[root] != root) {
                root = parent[root];
            }  
            while (parent[node] != root) {
                size_t next = parent[node];
                parent[node] = root;
                node = next;
            }
            return root;
        };
        std::function<size_t(size_t, size_t)> unify = [&](size_t a, size_t b) {
            if (a == b) {
                return a;
            }
            if (depth[a] > depth[b]) {
                parent[b] = a;
                count[a] += count[b];
                return a;
            } else {
                parent[a] = b;
                count[b] += count[a];
                if (depth[a] == depth[b]) {
                    depth[b]++;
                }
                return b;
            }
        };

        // Find the divisors of a number. Unify all.
        for (int num : nums) {
            if (num == 1) {
                continue;
            }
            std::optional<size_t> cls;

            for (size_t i = 0; i < n; ++i) {
                unsigned prime = primes[i];
                if (prime * prime > num) {
                    break;
                }
                if (num % prime != 0) {
                    continue;
                }

                if (cls) { 
                    cls = unify(*cls, find(i));
                } else {
                    cls = find(i);
                }

                while (num % prime == 0) {
                    num /= prime;
                }
            }
            if (num != 1) {
                if (cls) {
                    cls = unify(*cls, find(primeToIndex[num]));
                } else {
                    cls = find(primeToIndex[num]);
                }
            }
            count[*cls] += 1;
        }
        
        unsigned bestGroup = 0;
        for (size_t i = 0; i < n; ++i) {
            if (parent[i] == i) {
                bestGroup = std::max(bestGroup, count[i]);
            }
        }
        return bestGroup;
    }
};
153521