Ejemplo n.º 1
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            var(cityCount, roadCount) = inputStream.ReadValue <int, int>();
            var roads   = GetRoads(inputStream, roadCount);
            var queries = inputStream.ReadInt();
            var people  = new Person[queries];

            for (int i = 0; i < people.Length; i++)
            {
                var(v, w) = inputStream.ReadValue <int, int>();
                v--;
                people[i] = new Person(v, w, i);
            }
            Array.Sort(people);
            var results   = new int[queries];
            var unionFind = new UnionFindTree(cityCount);

            for (int i = 0; i < people.Length; i++)
            {
                while (roads.Count > 0 && roads.Peek().Year > people[i].Year)
                {
                    var road = roads.Dequeue();
                    unionFind.Unite(road.From, road.To);
                }

                results[people[i].Index] = unionFind.GetGroupSizeOf(people[i].From);
            }

            foreach (var result in results)
            {
                yield return(result);
            }
        }
Ejemplo n.º 2
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            var(people, friends, blocks) = inputStream.ReadValue <int, int, int>();
            var uf = new UnionFindTree(people);

            var friendsCount = new int[people];

            for (int i = 0; i < friends; i++)
            {
                var(a, b) = inputStream.ReadValue <int, int>();
                a--;
                b--;
                uf.Unite(a, b);
                friendsCount[a]++;
                friendsCount[b]++;
            }

            var blockCounts = new int[people];

            for (int i = 0; i < blocks; i++)
            {
                var(c, d) = inputStream.ReadValue <int, int>();
                c--;
                d--;
                if (uf.IsInSameGroup(c, d))
                {
                    blockCounts[c]++;
                    blockCounts[d]++;
                }
            }

            yield return(Enumerable.Range(0, people).Select(i => uf.GetGroupSizeOf(i) - friendsCount[i] - blockCounts[i] - 1).Join(' '));
        }
Ejemplo n.º 3
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            var nm           = inputStream.ReadIntArray();
            var islandsCount = nm[0];
            var bridgesCount = nm[1];

            var bridges = new Bridge[bridgesCount];

            for (int i = 0; i < bridges.Length; i++)
            {
                var ab = inputStream.ReadIntArray();
                var a  = ab[0] - 1;
                var b  = ab[1] - 1;
                bridges[i] = new Bridge(a, b);
            }

            var inconvenienceDiff = new Stack <long>(bridgesCount);
            var islandGroups      = new UnionFindTree(islandsCount);

            foreach (var bridge in bridges.Reverse())
            {
                if (!islandGroups.IsInSameGroup(bridge.IslandA, bridge.IslandB))
                {
                    long groupA = islandGroups.GetGroupSizeOf(bridge.IslandA);
                    long groupB = islandGroups.GetGroupSizeOf(bridge.IslandB);
                    inconvenienceDiff.Push(groupA * groupB);
                    islandGroups.Unite(bridge.IslandA, bridge.IslandB);
                }
                else
                {
                    inconvenienceDiff.Push(0);
                }
            }

            long total = 0;

            foreach (var diff in inconvenienceDiff)
            {
                total += diff;
                yield return(total);
            }
        }
Ejemplo n.º 4
0
        public override void Solve(IOManager io)
        {
            var n  = io.ReadInt();
            var uf = new UnionFindTree(n);

            var towns = new Town[n];

            for (int i = 0; i < towns.Length; i++)
            {
                var x = io.ReadInt();
                var y = io.ReadInt();
                towns[i] = new Town(x, y, i);
            }

            Array.Sort(towns, (a, b) => a.X - b.X);

            var rbTree = new RedBlackTree <Town>();

            for (int i = 0; i < towns.Length; i++)
            {
                var toRemove = new Queue <Town>();

                foreach (var town in rbTree.EnumerateRange(new Town(0, 0, 0), towns[i]))
                {
                    uf.Unite(town.ID, towns[i].ID);
                    toRemove.Enqueue(town);
                }

                if (toRemove.Count > 0)
                {
                    toRemove.Dequeue();
                    while (toRemove.Count > 0)
                    {
                        rbTree.Remove(toRemove.Dequeue());
                    }
                }
                else
                {
                    rbTree.Add(towns[i]);
                }
            }

            for (int i = 0; i < uf.Count; i++)
            {
                io.WriteLine(uf.GetGroupSizeOf(i));
            }
        }
Ejemplo n.º 5
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            var(people, relations) = inputStream.ReadValue <int, int>();
            var unionFind = new UnionFindTree(people);

            for (int i = 0; i < relations; i++)
            {
                var(a, b) = inputStream.ReadValue <int, int>();
                a--;
                b--;
                unionFind.Unite(a, b);
            }

            var max = 0;

            for (int i = 0; i < unionFind.Count; i++)
            {
                max = Math.Max(max, unionFind.GetGroupSizeOf(i));
            }

            yield return(max);
        }
Ejemplo n.º 6
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            var nm           = inputStream.ReadIntArray();
            var islandsCount = nm[0];
            var bridgesCount = nm[1];

            var bridges = new Bridge[bridgesCount];

            for (int i = 0; i < bridgesCount; i++)
            {
                var ab = inputStream.ReadIntArray();
                bridges[i] = new Bridge(ab[0] - 1, ab[1] - 1);
            }

            var conveniences = new long[bridgesCount + 1];
            var islands      = new UnionFindTree(islandsCount);

            for (int i = bridges.Length - 1; i >= 0; i--)
            {
                var bridge = bridges[i];
                conveniences[i] = conveniences[i + 1];

                if (!islands.IsInSameGroup(bridge.Island1, bridge.Island2))
                {
                    conveniences[i] += (long)islands.GetGroupSizeOf(bridge.Island1) * islands.GetGroupSizeOf(bridge.Island2);
                    islands.Unite(bridge.Island1, bridge.Island2);
                }
            }

            long unconvenience = 0;

            for (int i = 0; i < bridges.Length; i++)
            {
                unconvenience += conveniences[i] - conveniences[i + 1];
                yield return(unconvenience);
            }
        }