Пример #1
0
        /*
         * Calculate the value of friendships
         */
        public static long RunUnionFind(int[][] edges)
        {
            var unionFind = new UnionFind();

            int count = 0;

            foreach (var edge in edges)
            {
                var left  = edge[0];
                var right = edge[1];
                if (left == right)
                {
                    continue;
                }

                if (unionFind.IsSameGroup(left, right))
                {
                    count++;
                    continue;
                }

                unionFind.Unite(left, right);
            }

            var groups = unionFind.GetGroups().Where(v => v != 0).Select(v => v + 1).ToList();

            groups.Sort();
            groups.Reverse();

            return(groups[0]);
        }
Пример #2
0
        /*
         * Calculate the value of friendships
         */
        public static void RunUnionFindTestCase()
        {
            var unionFind = new UnionFind();
            var edges     = new int[4][];

            edges[0] = new int[] { 0, 0 };
            edges[1] = new int[] { 1, 0 };
            edges[2] = new int[] { 2, 0 };
            edges[3] = new int[] { 2, 0 };


            int count = 0;
            int index = 1;

            foreach (var edge in edges)
            {
                var left    = index;
                var right   = edge[0];
                var burnout = edge[1];


                if (burnout == 1)
                {
                    continue;
                }

                if (unionFind.IsSameGroup(left, right))
                {
                    count++;
                    continue;
                }

                unionFind.Unite(left, right);

                index++;
            }

            var bigThanOne = 0;
            var groups     = unionFind.GetGroupsSpecial(ref bigThanOne).Where(v => v != 0).Select(v => v + 1).ToList();

            groups.Sort();
            var result = groups.Sum() - bigThanOne;
        }
Пример #3
0
        /*
         * Calculate the value of friendships
         */
        public static long RunUnionFind(int[][] edges)
        {
            var unionFind = new UnionFind();

            int  count    = 0;
            int  index    = 1;
            bool ceoFound = false;

            foreach (var edge in edges)
            {
                var left    = index;
                var right   = edge[0];
                var burnout = edge[1];

                if (burnout == 1)
                {
                    continue;
                }

                if (right == 0)
                {
                    ceoFound = true;
                }

                if (unionFind.IsSameGroup(left, right))
                {
                    count++;
                    continue;
                }

                unionFind.Unite(right, left);

                index++;
            }
            var bigThanOne = 0;
            var groups     = unionFind.GetGroupsSpecial(ref bigThanOne).Where(v => v != 0).Select(v => v + 1).ToList();

            var original = groups.Sum();

            return((ceoFound) ? (original - 1) : original);
        }
Пример #4
0
        /*
         * Calculate the value of friendships
         */
        public static void RunUnionFindTestCase()
        {
            var unionFind = new UnionFind();
            var edges     = new int[6][];

            edges[0] = new int[] { 1, 2 };
            edges[1] = new int[] { 7, 4 };
            edges[2] = new int[] { 7, 3 };
            edges[3] = new int[] { 5, 8 };
            edges[4] = new int[] { 1, 3 };
            edges[5] = new int[] { 7, 3 };

            int count = 0;

            foreach (var edge in edges)
            {
                var left  = edge[0];
                var right = edge[1];
                if (left == right)
                {
                    continue;
                }

                if (unionFind.IsSameGroup(left, right))
                {
                    count++;
                    continue;
                }

                unionFind.Unite(left, right);
            }

            var groups = unionFind.GetGroups().Where(v => v != 0).Select(v => v + 1).ToList();

            groups.Sort();
            groups.Reverse();
        }