結果

問題 No.2290 UnUnion Find
ユーザー tentententen
提出日時 2023-07-26 20:39:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 820 ms / 2,000 ms
コード長 2,942 bytes
コンパイル時間 2,690 ms
コンパイル使用メモリ 85,832 KB
実行使用メモリ 75,984 KB
最終ジャッジ日時 2024-04-14 10:04:42
合計ジャッジ時間 38,672 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
50,184 KB
testcase_01 AC 56 ms
50,236 KB
testcase_02 AC 399 ms
59,164 KB
testcase_03 AC 631 ms
65,280 KB
testcase_04 AC 659 ms
69,852 KB
testcase_05 AC 669 ms
69,804 KB
testcase_06 AC 653 ms
71,684 KB
testcase_07 AC 648 ms
68,604 KB
testcase_08 AC 661 ms
72,336 KB
testcase_09 AC 649 ms
67,632 KB
testcase_10 AC 661 ms
70,532 KB
testcase_11 AC 653 ms
71,140 KB
testcase_12 AC 663 ms
69,352 KB
testcase_13 AC 650 ms
69,572 KB
testcase_14 AC 660 ms
71,360 KB
testcase_15 AC 653 ms
71,380 KB
testcase_16 AC 645 ms
68,776 KB
testcase_17 AC 663 ms
73,684 KB
testcase_18 AC 644 ms
71,472 KB
testcase_19 AC 745 ms
66,840 KB
testcase_20 AC 820 ms
75,876 KB
testcase_21 AC 424 ms
58,672 KB
testcase_22 AC 542 ms
63,180 KB
testcase_23 AC 537 ms
63,220 KB
testcase_24 AC 766 ms
69,948 KB
testcase_25 AC 517 ms
61,108 KB
testcase_26 AC 791 ms
75,984 KB
testcase_27 AC 777 ms
71,176 KB
testcase_28 AC 632 ms
63,368 KB
testcase_29 AC 756 ms
66,604 KB
testcase_30 AC 469 ms
61,004 KB
testcase_31 AC 737 ms
65,912 KB
testcase_32 AC 504 ms
60,968 KB
testcase_33 AC 623 ms
63,380 KB
testcase_34 AC 802 ms
75,540 KB
testcase_35 AC 784 ms
75,084 KB
testcase_36 AC 523 ms
61,240 KB
testcase_37 AC 594 ms
63,292 KB
testcase_38 AC 528 ms
61,172 KB
testcase_39 AC 539 ms
63,004 KB
testcase_40 AC 775 ms
72,596 KB
testcase_41 AC 506 ms
60,856 KB
testcase_42 AC 717 ms
65,804 KB
testcase_43 AC 687 ms
65,256 KB
testcase_44 AC 648 ms
71,532 KB
testcase_45 AC 655 ms
65,364 KB
testcase_46 AC 675 ms
65,456 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.notUnion(sc.nextInt() - 1) + 1).append("\n");
            }
        }
        System.out.print(sb);
    }
    
    static class UnionFindTree {
        int[] parents;
        TreeSet<Integer> points = new TreeSet<>();
        
        public UnionFindTree(int size) {
            parents = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
                points.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;
                points.remove(xx);
            }
        }
        
        public int notUnion(int x) {
            if (points.size() == 1) {
                return -2;
            }
            int xx = find(x);
            if (points.first() == xx) {
                return points.last();
            } else {
                return points.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