Submission #420586


Source Code Expand

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ATC001
{
    public class B
    {
        private void Run()
        {
            var input = Console.ReadLine().Trim().Split();
            int N = int.Parse(input[0]);
            int Q = int.Parse(input[1]);

            var uf = new UnionFind(N);
            for (int i = 0; i < Q; i++)
            {
                input = Console.ReadLine().Trim().Split();
                int P = int.Parse(input[0]);
                int A = int.Parse(input[1]);
                int B = int.Parse(input[2]);
                if (P == 0) { uf.Union(A, B); }
                else if (P == 1)
                {
                    Console.WriteLine(uf.IsSameSet(A, B) ? "Yes" : "No");
                }
                else { throw new Exception(); }
            }
        }

        private static void Main()
        {
            var old = Console.Out;
            using (var writer = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false })
            {
                Console.SetOut(writer);
                new B().Run();
                Console.Out.Flush();
                Console.SetOut(old);
            }
        }

        public class UnionFind
        {
            public UnionFind(int elemCount) { this.m_a = Enumerable.Repeat(-1, elemCount).ToArray(); }
            public int Root(int x)
            {
                return this.m_a[x] < 0
                    ? x
                    : this.m_a[x] = this.Root(this.m_a[x]);
            }
            public bool Union(int x, int y)
            {
                x = Root(x);
                y = Root(y);
                if (x == y) { return false; }

                if (this.m_a[x] > this.m_a[y])
                {
                    var temp = this.m_a[x];
                    this.m_a[x] = this.m_a[y];
                    this.m_a[y] = temp;
                }
                this.m_a[x] += this.m_a[y];
                this.m_a[y] = x;
                return true;
            }
            public bool IsSameSet(int x, int y) { return this.Root(x) == this.Root(y); }
            public int Size(int x) { return -this.m_a[this.Root(x)]; }

            private readonly int[] m_a;
        }
    }
}

Submission Info

Submission Time
Task B - Union Find
User Tan90909090
Language C# (Mono 3.2.1.0)
Score 100
Code Size 2358 Byte
Status AC
Exec Time 387 ms
Memory 14860 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 1
AC × 19
Set Name Test Cases
Sample 00_sample_01.txt
All 00_sample_01.txt, subtask_01_01.txt, subtask_01_02.txt, subtask_01_03.txt, subtask_01_04.txt, subtask_01_05.txt, subtask_01_06.txt, subtask_01_07.txt, subtask_01_08.txt, subtask_01_09.txt, subtask_01_10.txt, subtask_01_11.txt, subtask_01_12.txt, subtask_01_13.txt, subtask_01_14.txt, subtask_01_15.txt, subtask_01_16.txt, subtask_01_17.txt, subtask_01_18.txt
Case Name Status Exec Time Memory
00_sample_01.txt AC 112 ms 9644 KB
subtask_01_01.txt AC 267 ms 13656 KB
subtask_01_02.txt AC 118 ms 10804 KB
subtask_01_03.txt AC 312 ms 13696 KB
subtask_01_04.txt AC 387 ms 14852 KB
subtask_01_05.txt AC 132 ms 13700 KB
subtask_01_06.txt AC 142 ms 14820 KB
subtask_01_07.txt AC 347 ms 13680 KB
subtask_01_08.txt AC 383 ms 14860 KB
subtask_01_09.txt AC 111 ms 9664 KB
subtask_01_10.txt AC 117 ms 10788 KB
subtask_01_11.txt AC 302 ms 13704 KB
subtask_01_12.txt AC 384 ms 14856 KB
subtask_01_13.txt AC 313 ms 13708 KB
subtask_01_14.txt AC 119 ms 10824 KB
subtask_01_15.txt AC 332 ms 13716 KB
subtask_01_16.txt AC 383 ms 14856 KB
subtask_01_17.txt AC 372 ms 14852 KB
subtask_01_18.txt AC 387 ms 14856 KB