結果

問題 No.2290 UnUnion Find
ユーザー tentententen
提出日時 2023-12-12 08:31:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 821 ms / 2,000 ms
コード長 2,926 bytes
コンパイル時間 3,219 ms
コンパイル使用メモリ 96,380 KB
実行使用メモリ 78,780 KB
最終ジャッジ日時 2023-12-12 08:32:23
合計ジャッジ時間 39,507 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
53,980 KB
testcase_01 AC 49 ms
53,980 KB
testcase_02 AC 356 ms
62,312 KB
testcase_03 AC 544 ms
69,016 KB
testcase_04 AC 560 ms
74,640 KB
testcase_05 AC 569 ms
74,696 KB
testcase_06 AC 581 ms
76,692 KB
testcase_07 AC 569 ms
72,604 KB
testcase_08 AC 568 ms
74,820 KB
testcase_09 AC 574 ms
72,612 KB
testcase_10 AC 568 ms
72,564 KB
testcase_11 AC 574 ms
74,664 KB
testcase_12 AC 570 ms
74,640 KB
testcase_13 AC 578 ms
74,684 KB
testcase_14 AC 531 ms
74,656 KB
testcase_15 AC 571 ms
74,604 KB
testcase_16 AC 579 ms
72,620 KB
testcase_17 AC 577 ms
76,660 KB
testcase_18 AC 568 ms
74,640 KB
testcase_19 AC 630 ms
69,108 KB
testcase_20 AC 692 ms
78,780 KB
testcase_21 AC 396 ms
62,456 KB
testcase_22 AC 496 ms
66,772 KB
testcase_23 AC 502 ms
66,708 KB
testcase_24 AC 661 ms
72,344 KB
testcase_25 AC 480 ms
64,624 KB
testcase_26 AC 684 ms
77,676 KB
testcase_27 AC 665 ms
73,936 KB
testcase_28 AC 569 ms
66,952 KB
testcase_29 AC 821 ms
69,592 KB
testcase_30 AC 470 ms
64,620 KB
testcase_31 AC 768 ms
69,064 KB
testcase_32 AC 544 ms
64,624 KB
testcase_33 AC 556 ms
66,880 KB
testcase_34 AC 694 ms
78,744 KB
testcase_35 AC 643 ms
77,180 KB
testcase_36 AC 492 ms
64,660 KB
testcase_37 AC 540 ms
66,868 KB
testcase_38 AC 491 ms
62,740 KB
testcase_39 AC 511 ms
66,760 KB
testcase_40 AC 667 ms
74,364 KB
testcase_41 AC 460 ms
64,664 KB
testcase_42 AC 615 ms
69,120 KB
testcase_43 AC 618 ms
69,032 KB
testcase_44 AC 548 ms
74,628 KB
testcase_45 AC 576 ms
69,036 KB
testcase_46 AC 600 ms
69,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int q = sc.nextInt();
        UnionFindTree uft = new UnionFindTree(n);
        StringBuilder sb = new StringBuilder();
        while (q-- > 0) {
            if (sc.nextInt() == 1) {
                uft.unite(sc.nextInt() - 1, sc.nextInt() - 1);
            } else {
                sb.append(uft.expect(sc.nextInt() - 1) + 1).append("\n");
            }
        }
        System.out.print(sb);
    }
    
    static class UnionFindTree {
        int[] parents;
        TreeSet<Integer> peeks = new TreeSet<>();
        
        public UnionFindTree(int size) {
            parents = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
                peeks.add(i);
            }
        }
        
        public int find(int x) {
            if (x == parents[x]) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public void unite(int x, int y) {
            int xx = find(x);
            int yy = find(y);
            if (xx != yy) {
                parents[xx] = yy;
                peeks.remove(xx);
            }
        }
        
        public int expect(int x) {
            if (peeks.size() == 1) {
                return -2;
            }
            int xx = find(x);
            if (peeks.first() == xx) {
                return peeks.last();
            } else {
                return peeks.first();
            }
        }
    }
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0