結果

問題 No.2290 UnUnion Find
ユーザー tentententen
提出日時 2024-04-03 11:11:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 876 ms / 2,000 ms
コード長 2,661 bytes
コンパイル時間 2,601 ms
コンパイル使用メモリ 79,532 KB
実行使用メモリ 81,924 KB
最終ジャッジ日時 2024-04-03 11:12:36
合計ジャッジ時間 42,099 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
53,976 KB
testcase_01 AC 57 ms
53,968 KB
testcase_02 AC 362 ms
62,212 KB
testcase_03 AC 680 ms
69,040 KB
testcase_04 AC 668 ms
74,800 KB
testcase_05 AC 676 ms
74,760 KB
testcase_06 AC 691 ms
76,752 KB
testcase_07 AC 656 ms
72,736 KB
testcase_08 AC 700 ms
74,776 KB
testcase_09 AC 659 ms
72,636 KB
testcase_10 AC 696 ms
72,756 KB
testcase_11 AC 690 ms
74,720 KB
testcase_12 AC 689 ms
74,868 KB
testcase_13 AC 667 ms
74,704 KB
testcase_14 AC 668 ms
74,692 KB
testcase_15 AC 674 ms
72,864 KB
testcase_16 AC 680 ms
72,660 KB
testcase_17 AC 702 ms
76,708 KB
testcase_18 AC 661 ms
72,824 KB
testcase_19 AC 791 ms
70,256 KB
testcase_20 AC 852 ms
81,924 KB
testcase_21 AC 438 ms
64,604 KB
testcase_22 AC 575 ms
66,824 KB
testcase_23 AC 584 ms
66,704 KB
testcase_24 AC 818 ms
77,964 KB
testcase_25 AC 541 ms
66,688 KB
testcase_26 AC 850 ms
81,428 KB
testcase_27 AC 838 ms
78,716 KB
testcase_28 AC 681 ms
69,016 KB
testcase_29 AC 801 ms
72,536 KB
testcase_30 AC 480 ms
64,656 KB
testcase_31 AC 811 ms
70,580 KB
testcase_32 AC 554 ms
66,724 KB
testcase_33 AC 674 ms
69,040 KB
testcase_34 AC 876 ms
81,912 KB
testcase_35 AC 865 ms
79,204 KB
testcase_36 AC 553 ms
66,696 KB
testcase_37 AC 674 ms
69,064 KB
testcase_38 AC 561 ms
66,732 KB
testcase_39 AC 602 ms
66,676 KB
testcase_40 AC 842 ms
79,012 KB
testcase_41 AC 536 ms
66,732 KB
testcase_42 AC 755 ms
69,628 KB
testcase_43 AC 727 ms
69,316 KB
testcase_44 AC 680 ms
74,716 KB
testcase_45 AC 716 ms
69,564 KB
testcase_46 AC 752 ms
69,224 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        UnionFindTree uft = new UnionFindTree(n);
        int q = sc.nextInt();
        StringBuilder sb = new StringBuilder();
        while (q-- > 0) {
            if (sc.nextInt() == 1) {
                uft.unite(sc.nextInt() - 1, sc.nextInt() - 1);
            } else {
                sb.append(uft.getAnother(sc.nextInt() - 1) + 1).append("\n");
            }
        }
        System.out.print(sb);
    }
    
    static class UnionFindTree {
        int[] parents;
        TreeSet<Integer> exist = new TreeSet<>();
        
        public UnionFindTree(int size) {
            parents = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
                exist.add(i);
            }
        }
        
        public int find(int x) {
            if (x == parents[x]) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public void unite(int x, int y) {
            int xx = find(x);
            int yy = find(y);
            if (xx != yy) {
                parents[xx] = yy;
                exist.remove(xx);
            }
        }
        
        public int getAnother(int x) {
            if (exist.size() == 1) {
                return -2;
            } else if (same(x, exist.last())) {
                return exist.first();
            } else {
                return exist.last();
            }
        }
    }
}
class Scanner {
    BufferedReader br;
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() {
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
        } catch (Exception e) {
            
        }
    }
    
    public int nextInt() {
        return Integer.parseInt(next());
    }
    
    public long nextLong() {
        return Long.parseLong(next());
    }
    
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    
    public String next() {
        try {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return st.nextToken();
        }
    }
    
}
0