Esempio n. 1
0
        public static void Main(string[] args)
        {
            int N = StdIn.ReadInt();

            WeightedQuickUnionUF uf = new WeightedQuickUnionUF(N);

            // read in a sequence of pairs of integers (each in the range 0 to N-1),
            // calling find() for each pair: If the members of the pair are not already
            // call union() and print the pair.
            while (!StdIn.IsEmpty())
            {
                int p = StdIn.ReadInt();
                int q = StdIn.ReadInt();

                if (uf.Connected(p, q))
                {
                    continue;
                }
                uf.Union(p, q);

                Console.WriteLine(p + " " + q);
            }

            Console.WriteLine(uf.Count + " components");
        }
 public void WeightedQuickUnionUnionFindTiny()
 {
     IUnionFind unionFind = new WeightedQuickUnionUF();
      CommonUFUnitTests.CommonUnionFindTiny(unionFind);
 }
        public static void Main(string[] args)
        {
            int N = StdIn.ReadInt();

            WeightedQuickUnionUF uf = new WeightedQuickUnionUF(N);

            // read in a sequence of pairs of integers (each in the range 0 to N-1),
            // calling find() for each pair: If the members of the pair are not already
            // call union() and print the pair.
            while (!StdIn.IsEmpty())
            {
                int p = StdIn.ReadInt();
                int q = StdIn.ReadInt();
                
                if (uf.Connected(p, q)) continue;
                uf.Union(p, q);

                Console.WriteLine(p + " " + q);
            }

            Console.WriteLine(uf.Count + " components");
        }