Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
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]);
            }
        }
Ejemplo n.º 3
0
        public static void Solve()
        {
            var(N, M) = Scanner.Scan <int, int>();
            var dsu  = new DisjointSetUnion(N + M);
            var used = new bool[M];

            for (var i = 0; i < N; i++)
            {
                var L = Scanner.ScanEnumerable <int>().Skip(1).Select(x => x - 1).ToArray();
                foreach (var l in L)
                {
                    dsu.Merge(i, N + l);
                    used[l] = true;
                }
            }

            var no     = used.Count(x => !x);
            var answer = dsu.SizeOf(0) == N + M - no;

            Console.WriteLine(answer ? "YES" : "NO");
        }