Example #1
0
    public static void Main()
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput())
        {
            AutoFlush = false
        });
        int n   = NextInt;
        int q   = NextInt;
        DSU dsu = new DSU(n);

        for (int i = 0; i < q; i++)
        {
            var t = NextInt;
            var u = NextInt;
            var v = NextInt;
            if (t == 0)
            {
                dsu.Merge(u, v);
            }
            if (t == 1)
            {
                Console.WriteLine(dsu.Same(u, v) ? "1" : "0");
            }
        }
        Console.Out.Flush();
    }
Example #2
0
    public static void Main()
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput())
        {
            AutoFlush = false
        });
        int n = NextInt;
        int q = NextInt;

        UnionFind uf  = new UnionFind(n);
        DSU       dsu = new DSU(n);

        void VerifyGroup()
        {
            var groups = dsu.Groups().ToArray().Select(x => x.OrderBy(x => x).ToArray()).OrderBy(x => x.First()).ToArray();
            var ans    = Enumerable.Range(0, n).GroupBy(uf.Find).Select(x => x.OrderBy(x => x).ToArray()).OrderBy(x => x.First()).ToArray();

            if (groups.Length != ans.Length)
            {
                throw new Exception();
            }
            if (groups.Zip(ans, (x, y) => x.SequenceEqual(y)).Any(x => !x))
            {
                throw new Exception();
            }
        }

        for (int i = 0; i < q; i++)
        {
            var(t, u, v) = (NextInt, NextInt, NextInt);
            if (t == 0)
            {
                dsu.Merge(u, v); uf.TryUnite(u, v);
            }
            else
            {
                Console.WriteLine(dsu.Same(u, v) ? "1" : "0");
            }
            if ((i & -i) == 0)
            {
                VerifyGroup();
            }
        }
        VerifyGroup();

        Console.Out.Flush();
    }
Example #3
0
        public override IEnumerable <object> Solve(TextReader inputStream)
        {
            var(n, queries) = inputStream.ReadValue <int, int>();
            var dsu = new DSU(n);

            for (int q = 0; q < queries; q++)
            {
                var(t, u, v) = inputStream.ReadValue <int, int, int>();
                if (t == 0)
                {
                    dsu.Merge(u, v);
                }
                else
                {
                    yield return(dsu.Same(u, v) ? 1 : 0);
                }
            }
        }
Example #4
0
        public static void Solve()
        {
            var(N, Q) = Scanner.Scan <int, int>();
            var dsu = new DSU(N);

            for (var i = 0; i < Q; i++)
            {
                var(t, u, v) = Scanner.Scan <int, int, int>();
                if (t == 0)
                {
                    dsu.Merge(u, v);
                }
                else
                {
                    Console.WriteLine(dsu.IsSame(u, v) ? 1 : 0);
                }
            }
        }
Example #5
0
        public void Solve(ConsoleReader cr, ConsoleWriter cw)
        {
            int n = cr;
            int q = cr;

            var dsu = new DSU(n);

            for (int i = 0; i < q; i++)
            {
                int t = cr;
                int u = cr;
                int v = cr;
                if (t == 0)
                {
                    dsu.Merge(u, v);
                }
                else
                {
                    cw.WriteLine(dsu.Same(u, v) ? 1 : 0);
                }
            }
        }
Example #6
0
        public static void Solve()
        {
            var(N, M, K) = Scanner.Scan <int, int, int>();
            var friends = new List <int> [N].Select(x => new List <int>()).ToArray();
            var blocks  = new List <int> [N].Select(x => new List <int>()).ToArray();
            var dsu     = new DSU(N);

            for (var i = 0; i < M; i++)
            {
                var(A, B) = Scanner.Scan <int, int>();
                dsu.Merge(--A, --B);
                friends[A].Add(B);
                friends[B].Add(A);
            }

            for (var i = 0; i < K; i++)
            {
                var(C, D) = Scanner.Scan <int, int>();
                blocks[--C].Add(--D);
                blocks[D].Add(C);
            }

            var answer = new int[N];

            for (var i = 0; i < N; i++)
            {
                answer[i] = dsu.SizeOf(i) - friends[i].Count - 1;
                foreach (var block in blocks[i])
                {
                    if (dsu.IsSame(i, block))
                    {
                        answer[i]--;
                    }
                }
            }
            Console.WriteLine(string.Join(" ", answer));
        }