結果

問題 No.2290 UnUnion Find
ユーザー 👑 kakel-sankakel-san
提出日時 2023-07-24 11:05:08
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 337 ms / 2,000 ms
コード長 2,022 bytes
コンパイル時間 1,453 ms
コンパイル使用メモリ 112,520 KB
実行使用メモリ 43,064 KB
最終ジャッジ日時 2024-04-08 13:11:46
合計ジャッジ時間 23,757 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
25,540 KB
testcase_01 AC 33 ms
25,540 KB
testcase_02 AC 273 ms
42,568 KB
testcase_03 AC 306 ms
41,784 KB
testcase_04 AC 298 ms
43,064 KB
testcase_05 AC 298 ms
42,168 KB
testcase_06 AC 302 ms
42,680 KB
testcase_07 AC 306 ms
43,064 KB
testcase_08 AC 307 ms
43,064 KB
testcase_09 AC 304 ms
43,064 KB
testcase_10 AC 301 ms
43,064 KB
testcase_11 AC 306 ms
43,064 KB
testcase_12 AC 302 ms
43,064 KB
testcase_13 AC 312 ms
43,064 KB
testcase_14 AC 302 ms
43,064 KB
testcase_15 AC 303 ms
43,064 KB
testcase_16 AC 302 ms
43,064 KB
testcase_17 AC 304 ms
43,064 KB
testcase_18 AC 302 ms
43,064 KB
testcase_19 AC 308 ms
41,532 KB
testcase_20 AC 319 ms
41,660 KB
testcase_21 AC 292 ms
42,432 KB
testcase_22 AC 299 ms
41,916 KB
testcase_23 AC 293 ms
42,300 KB
testcase_24 AC 312 ms
41,532 KB
testcase_25 AC 291 ms
42,812 KB
testcase_26 AC 318 ms
41,660 KB
testcase_27 AC 316 ms
41,660 KB
testcase_28 AC 297 ms
41,532 KB
testcase_29 AC 313 ms
41,532 KB
testcase_30 AC 287 ms
42,812 KB
testcase_31 AC 308 ms
41,532 KB
testcase_32 AC 288 ms
42,556 KB
testcase_33 AC 298 ms
41,404 KB
testcase_34 AC 318 ms
41,660 KB
testcase_35 AC 313 ms
41,660 KB
testcase_36 AC 293 ms
42,556 KB
testcase_37 AC 308 ms
41,916 KB
testcase_38 AC 300 ms
42,172 KB
testcase_39 AC 302 ms
42,172 KB
testcase_40 AC 337 ms
41,660 KB
testcase_41 AC 301 ms
42,812 KB
testcase_42 AC 311 ms
41,532 KB
testcase_43 AC 316 ms
41,532 KB
testcase_44 AC 306 ms
42,168 KB
testcase_45 AC 311 ms
41,408 KB
testcase_46 AC 310 ms
41,408 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;
class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, q) = (c[0], c[1]);
        var query = NArr(q);
        var uf = new UnionFindTree(n);
        var other = 1;
        var ans = new List<int>();
        foreach (var que in query)
        {
            if (que[0] == 1)
            {
                uf.Unite(que[1] - 1, que[2] - 1);
                while (other < n && uf.IsSameTree(0, other)) ++other;
            }
            else
            {
                if (!uf.IsSameTree(0, que[1] - 1)) ans.Add(1);
                else if (other < n) ans.Add(other + 1);
                else ans.Add(-1);
            }
        }
        WriteLine(string.Join("\n", ans));
    }
    class UnionFindTree
    {
        int[] roots;
        public UnionFindTree(int size)
        {
            roots = new int[size];
            for (var i = 0; i < size; ++i) roots[i] = -1;
        }
        public int GetRoot(int a)
        {
            if (roots[a] < 0) return a;
            return roots[a] = GetRoot(roots[a]);
        }
        public bool IsSameTree(int a, int b)
        {
            return GetRoot(a) == GetRoot(b);
        }
        public bool Unite(int a, int b)
        {
            var x = GetRoot(a);
            var y = GetRoot(b);
            if (x == y) return false;
            if (-roots[x] < -roots[y]) { var tmp = x; x = y; y = tmp; }
            roots[x] += roots[y];
            roots[y] = x;
            return true;
        }
        public int GetSize(int a)
        {
            return -roots[GetRoot(a)];
        }
    }
}
0