Exemplo n.º 1
0
        public int MakeConnected(int n, int[][] connections)
        {
            if (connections.Length < n - 1)
            {
                return(-1);
            }

            QuickUnionUF uf = new QuickUnionUF(n);

            int p;
            int q;

            for (int i = 0; i < connections.Length; i++)
            {
                p = connections[i][0];
                q = connections[i][1];
                if (uf.Find(p) != uf.Find(q))
                {
                    uf.Uunion(p, q);
                }
            }

            int requiredEdges = uf.Count - 1;

            return(requiredEdges);
        }