結果

問題 No.2290 UnUnion Find
ユーザー prin_kemkemprin_kemkem
提出日時 2023-05-06 11:46:08
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,706 bytes
コンパイル時間 2,232 ms
コンパイル使用メモリ 77,436 KB
実行使用メモリ 61,236 KB
最終ジャッジ日時 2024-05-03 01:01:39
合計ジャッジ時間 89,114 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
40,368 KB
testcase_01 AC 133 ms
41,204 KB
testcase_02 AC 1,726 ms
58,032 KB
testcase_03 AC 1,709 ms
59,856 KB
testcase_04 AC 1,664 ms
60,208 KB
testcase_05 AC 1,746 ms
60,184 KB
testcase_06 AC 1,688 ms
59,852 KB
testcase_07 AC 1,719 ms
59,820 KB
testcase_08 AC 1,692 ms
60,264 KB
testcase_09 AC 1,794 ms
60,804 KB
testcase_10 AC 1,699 ms
59,880 KB
testcase_11 AC 1,759 ms
60,104 KB
testcase_12 AC 1,716 ms
59,864 KB
testcase_13 AC 1,682 ms
60,068 KB
testcase_14 AC 1,708 ms
60,112 KB
testcase_15 AC 1,759 ms
60,964 KB
testcase_16 AC 1,760 ms
60,044 KB
testcase_17 AC 1,730 ms
60,288 KB
testcase_18 AC 1,723 ms
59,856 KB
testcase_19 AC 1,741 ms
59,960 KB
testcase_20 AC 1,772 ms
61,196 KB
testcase_21 AC 1,881 ms
58,656 KB
testcase_22 AC 1,837 ms
59,680 KB
testcase_23 AC 1,933 ms
59,344 KB
testcase_24 AC 1,760 ms
60,412 KB
testcase_25 AC 1,944 ms
59,368 KB
testcase_26 AC 1,766 ms
61,024 KB
testcase_27 AC 1,788 ms
60,564 KB
testcase_28 AC 1,798 ms
59,876 KB
testcase_29 AC 1,779 ms
60,144 KB
testcase_30 TLE -
testcase_31 AC 1,729 ms
60,336 KB
testcase_32 AC 1,960 ms
59,652 KB
testcase_33 AC 1,803 ms
60,068 KB
testcase_34 AC 1,843 ms
61,236 KB
testcase_35 AC 1,809 ms
60,844 KB
testcase_36 AC 1,900 ms
59,256 KB
testcase_37 AC 1,855 ms
59,880 KB
testcase_38 AC 1,933 ms
59,356 KB
testcase_39 AC 1,903 ms
59,636 KB
testcase_40 AC 1,709 ms
60,532 KB
testcase_41 AC 1,923 ms
59,248 KB
testcase_42 AC 1,823 ms
60,108 KB
testcase_43 AC 1,757 ms
60,164 KB
testcase_44 AC 1,783 ms
60,432 KB
testcase_45 AC 1,790 ms
60,088 KB
testcase_46 AC 1,742 ms
60,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.Arrays;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int Q = sc.nextInt();
        UnionFind uf = new UnionFind(N + 1);
        while (Q-- > 0) {
            int q = sc.nextInt();
            if (q == 1) {
                int u = sc.nextInt();
                int v = sc.nextInt();
                uf.unite(u, v);
            } else {
                int v = sc.nextInt();
                int ans = uf.unUnitePoint(v);
                if (ans > N)
                    ans = -1;
                System.out.println(ans);
            }
        }
    }
}

class UnionFind {
    int[] par, unUnite, size;

    UnionFind(int N) {
        par = new int[N];
        unUnite = new int[N];
        size = new int[N];
        Arrays.fill(par, -1);
        Arrays.fill(unUnite, 1);
        Arrays.fill(size, 1);
        unUnite[1] = 2;
    }

    int root(int x) {
        if (par[x] == -1)
            return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y) {
        int rootX = root(x);
        int rootY = root(y);
        if (rootX == rootY)
            return;
        if (size[rootX] < size[rootY]) {
            int tmp = rootX;
            rootX = rootY;
            rootY = tmp;
        }
        par[rootY] = rootX;
        size[rootX] += size[rootY];
        check(rootY);
        check(rootX);
    }

    void check(int x) {
        int root = root(x);
        while (unUnite[root] < unUnite.length && root == root(unUnite[root]))
            unUnite[root]++;
    }

    int unUnitePoint(int x) {
        return unUnite[root(x)];
    }
}
0