<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:
    def largestComponentSize(self, nums: List[int]) -> int:
 
        # This problem's TLE threshold seems too low, you have to do the problem
        # EXACTLY RIGHT (tm) or you fail.
        #  In particular: you need to use the recursive form of finding unique
        #  factors so you only need to check up to sqrt(n). That's knowledge that's
        #  past what I consider to be good for LC.

        # Overall idea:
        #    > we want to link numbers together if they share a prime factor
        #    > cluster questions are almost always union-find questions
        #    > we can "transitively link" numbers as follows:
        #       > link each number n to its first prime factor
        #       > then link its prime factors together
        #       > this looks like a 1D chain: n -- firstFactor -- secondFactor -- .. -- lastFactor
        #    > by doing this for each number, n will be linked via its prime factor(s) to all
        #      other n' with the same factor
        #    > using union-find lets us get away with adding fewer factors

        M = max(nums)
        U = M//2
        P = [True]*(M+1)
        primes = [2]
        for c in range(3, U+1, 2):
            if P[c]:
                primes.append(c)
                for m in range(c*c, U+1, 2*c):
                    P[m] = False

        print(primes)

        def uniqueFactors(i: int, n: int) -> List[int]:
            """
            Returns unique prime factors in primes[i:] that divide n. 
            
            PRE: n is not divisible by primes[:i]
            
            Factors are returned in descending order.
            """
            if n == 1:
                return []

            for j in range(i, len(primes)):
                p = primes[j]

                if p*p > n:
                    break

                if n % p == 0:
                    r = n//p
                    while r % p == 0:
                        r //= p
                    factors = uniqueFactors(j+1, r)
                    factors.append(p)
                    return factors

            return [n]

        parent = [-1]*(M+1)

        def find(n: int) -> int:
            if parent[n] < 0:
                return n
            else:
                parent[n] = find(parent[n])
                return parent[n]

        def union(m: int, n: int) -> None:
            rm = find(m)
            rn = find(n)

            if rm == rn:
                return

            if parent[rm] > parent[rn]:
                rm, rn = rn, rm

            parent[rm] += parent[rn]
            parent[rn] = rm

        for n in nums:
            if n == 1:
                continue

            # link n to each of its prime factors, transitively linking it to
            # all numbers that share a factor
            fs = uniqueFactors(0, n)
            # print(f"uniqueFactors(0, {n}) = {fs}")
            prev = n
            for f in fs:
                union(f, prev)
                prev = f

        # find cluster with the largest membership
        sizes = Counter(find(n) for n in nums)
        return max(sizes.values())
49410