Example #1
0
        public void testUnionAndConnected_MultiConnection()
        {
            QuickFindUF quickFindUF = new QuickFindUF(10);

            quickFindUF.Union(0, 1);
            quickFindUF.Union(2, 1);
            Assert.IsTrue(quickFindUF.Connected(0, 1));
            Assert.IsTrue(quickFindUF.Connected(2, 1));
            Assert.IsTrue(quickFindUF.Connected(2, 0));
            Assert.AreEqual(8, quickFindUF.Count());
            Assert.AreEqual(1, quickFindUF.Find(1));
            Assert.AreEqual(1, quickFindUF.Find(0));
            Assert.AreEqual(1, quickFindUF.Find(2));

            quickFindUF.Union(2, 5);
            Assert.IsTrue(quickFindUF.Connected(0, 1));
            Assert.IsTrue(quickFindUF.Connected(2, 1));
            Assert.IsTrue(quickFindUF.Connected(2, 0));
            Assert.IsTrue(quickFindUF.Connected(5, 0));
            Assert.IsTrue(quickFindUF.Connected(5, 1));
            Assert.IsTrue(quickFindUF.Connected(5, 2));
            Assert.AreEqual(7, quickFindUF.Count());
            Assert.AreEqual(5, quickFindUF.Find(1));
            Assert.AreEqual(5, quickFindUF.Find(0));
            Assert.AreEqual(5, quickFindUF.Find(2));
            Assert.AreEqual(5, quickFindUF.Find(5));
        }
Example #2
0
        public void testUnionAndConnected_NewConnection()
        {
            QuickFindUF quickFindUF = new QuickFindUF(10);

            Assert.IsFalse(quickFindUF.Connected(0, 1));
            Assert.AreEqual(10, quickFindUF.Count());
            quickFindUF.Union(0, 1);
            Assert.IsTrue(quickFindUF.Connected(0, 1));
            Assert.AreEqual(9, quickFindUF.Count());
            Assert.AreEqual(1, quickFindUF.Find(1));
            Assert.AreEqual(1, quickFindUF.Find(0));
        }
Example #3
0
    public static void Main(String[] args)
    {
        int         n  = Convert.ToInt32(Console.ReadLine());
        QuickFindUF uf = new QuickFindUF(n);
        string      line;

        while ((line = Console.ReadLine()) != null && line != "")
        {
            string[] a_temp = line.Split(' ');
            int[]    array  = Array.ConvertAll(a_temp, Int32.Parse);
            int      p      = array[0];
            int      q      = array[1];

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

            Console.WriteLine(p + " " + q);
        }
        Console.WriteLine(uf.Count() + " components");
        Console.ReadLine();
    }
        public void TestInit()
        {
            var qf = new QuickFindUF(10);

            Assert.AreEqual(10, qf.Count());
        }