<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 {
    static const int MAX_V = 1e5 + 5;

    // get primes
    int big[MAX_V];
    bool is_prime[MAX_V];

    void sieve() {
        for (int i = 2; i < MAX_V; i++) {
            if (big[i]) continue;
            for (int j = i; j < MAX_V; j += i) {
                big[j] = i;
            }
        }
        //for (int i = 2; i < MAX_V; i++) if (big[i] == i) is_prime[i] = true;
    }

    vector<int> fact(int n) {
        vector<int> v;
        int p = big[n];
        while(n != 1) {
            while(n % p == 0) {
                v.emplace_back(p);
                n /= p;
            }
            p = big[n];
        }
        return v;
    }

    // dsu
    const int N = 1e5 + 5;
    vector<int> par, sz;
    void init() {
        par.resize(N);
        iota(par.begin(), par.end(), 0);
        sz.assign(N, 1);
    }
    int get(int a) {
        return (a == par[a]) ? a : par[a] = get(par[a]);
    }
    bool join(int a, int b) {
        a = get(a); b = get(b);
        if (a == b) return 0;
        if (sz[a] < sz[b]) swap(a, b);
        par[b] = a;
        sz[a] += sz[b];
        return 1;
    }   

public:
    int largestComponentSize(vector<int>& a) {
        sieve();
        init();
        for (int num : a) {
            vector<int> f = fact(num);
            for (int i = 1; i < f.size(); i++) {
                join(f[0], f[i]);
            }
        }   
        vector<int> len(N);
        for (int num : a) {
            vector<int> f = fact(num);
            set<int> ids;
            for (int i = 0; i < f.size(); i++) {
                int p = get(f[i]);
                if (!ids.count(p)) {
                    len[p]++;
                    ids.emplace(p);
                }
            }
        }
        int ans = 0;
        for (int i = 0; i < N; i++) ans = max(ans, len[i]);
        return ans;
    }
};
232468