コード例 #1
0
ファイル: B.cs プロジェクト: AconCavy/atcoder-csharp
        public static void Solve()
        {
            var N    = Scanner.Scan <int>();
            var M    = 400000;
            var dsu  = new DisjointSetUnion(M);
            var edge = new int[M];

            for (var i = 0; i < N; i++)
            {
                var(a, b) = Scanner.Scan <int, int>();
                var u = dsu.LeaderOf(a - 1);
                var v = dsu.LeaderOf(b - 1);
                var w = u;
                if (!dsu.IsSame(u, v))
                {
                    w        = dsu.Merge(u, v);
                    edge[w] += edge[w == u ? v : u];
                }
                edge[w]++;
            }

            var answer = 0;

            for (var i = 0; i < M; i++)
            {
                if (dsu.LeaderOf(i) == i)
                {
                    answer += Math.Min(edge[i], dsu.SizeOf(i));
                }
            }

            Console.WriteLine(answer);
        }
コード例 #2
0
        public static void Solve()
        {
            var(N, Q) = Scanner.Scan <int, int>();
            var C     = Scanner.ScanEnumerable <int>().Select(x => x - 1).ToArray();
            var dsu   = new DisjointSetUnion(N);
            var count = new Dictionary <int, int> [N].Select(_ => new Dictionary <int, int>()).ToArray();

            for (var i = 0; i < N; i++)
            {
                if (!count[i].ContainsKey(C[i]))
                {
                    count[i][C[i]] = 0;
                }
                count[i][C[i]]++;
            }
            while (Q-- > 0)
            {
                var(q, a, b) = Scanner.Scan <int, int, int>();
                a--; b--;
                if (q == 1)
                {
                    if (dsu.IsSame(a, b))
                    {
                        continue;
                    }
                    var la = dsu.LeaderOf(a);
                    var lb = dsu.LeaderOf(b);
                    dsu.Merge(a, b);
                    var na = dsu.LeaderOf(a);
                    var nb = na == la ? lb : la;
                    foreach (var(key, value) in count[nb])
                    {
                        if (!count[na].ContainsKey(key))
                        {
                            count[na][key] = 0;
                        }
                        count[na][key] += value;
                    }
                }
                else
                {
                    var l = dsu.LeaderOf(a);
                    Console.WriteLine(count[l].ContainsKey(b) ? count[l][b] : 0);
                }
            }
        }
コード例 #3
0
        public static void Solve()
        {
            var N = Scanner.Scan <int>();
            var X = new int[N];
            var Y = new int[N];

            for (var i = 0; i < N; i++)
            {
                var(x, y) = Scanner.Scan <int, int>();
                X[i]      = x - 1;
                Y[i]      = y - 1;
            }

            var YByX = new int[N];

            for (var i = 0; i < N; i++)
            {
                YByX[X[i]] = Y[i];
            }
            var indexes = new int[N];

            for (var i = 0; i < N; i++)
            {
                indexes[X[i]] = i;
            }
            var comp = Comparer <(int y, int leader)> .Create((l, r) => Comparer <int> .Default.Compare(l.y, r.y));

            var queue = new PriorityQueue <(int y, int leader)>(comp);
            var dsu   = new DisjointSetUnion(N);

            for (var i = 0; i < N; i++)
            {
                var y      = YByX[i];
                var leader = i;
                while (queue.Any() && queue.Peek().y < YByX[i])
                {
                    var tmp = queue.Dequeue();
                    dsu.Merge(i, tmp.leader);
                    leader = dsu.LeaderOf(i);
                    y      = Math.Min(y, tmp.y);
                }
                queue.Enqueue((y, leader));
            }

            var answer = new int[N];

            for (var i = 0; i < N; i++)
            {
                answer[indexes[i]] = dsu.SizeOf(i);
            }

            for (var k = 0; k < N; k++)
            {
                Console.WriteLine(answer[k]);
            }
        }