예제 #1
0
    public int LargestComponentSize(int[] A)
    {
        int n  = A.Max();
        var uf = new UF(n + 1);

        foreach (var a in A)
        {
            int t = (int)Math.Sqrt(a);
            for (int k = 2; k <= t; k++)
            {
                if (a % k == 0)
                {
                    uf.Union(a, k);
                    uf.Union(a, a / k);
                }
            }
        }
        var m   = new Dictionary <int, int>();
        int res = 1; // with itself, min = 1;

        foreach (var a in A)
        {
            int x = uf.FindRoot(a);
            if (!m.ContainsKey(x))
            {
                m.Add(x, 0);
            }
            res = Math.Max(res, ++m[x]);
        }
        return(res);
    }