結果
問題 | No.2290 UnUnion Find |
ユーザー | tenten |
提出日時 | 2023-12-12 08:31:39 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 835 ms / 2,000 ms |
コード長 | 2,926 bytes |
コンパイル時間 | 3,738 ms |
コンパイル使用メモリ | 85,008 KB |
実行使用メモリ | 66,096 KB |
最終ジャッジ日時 | 2024-09-27 04:50:16 |
合計ジャッジ時間 | 37,371 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 52 ms
37,216 KB |
testcase_01 | AC | 51 ms
36,968 KB |
testcase_02 | AC | 389 ms
48,572 KB |
testcase_03 | AC | 625 ms
53,524 KB |
testcase_04 | AC | 645 ms
59,592 KB |
testcase_05 | AC | 654 ms
59,280 KB |
testcase_06 | AC | 644 ms
60,092 KB |
testcase_07 | AC | 637 ms
57,800 KB |
testcase_08 | AC | 645 ms
60,168 KB |
testcase_09 | AC | 625 ms
57,132 KB |
testcase_10 | AC | 647 ms
58,744 KB |
testcase_11 | AC | 647 ms
60,492 KB |
testcase_12 | AC | 634 ms
58,488 KB |
testcase_13 | AC | 637 ms
59,044 KB |
testcase_14 | AC | 640 ms
59,648 KB |
testcase_15 | AC | 643 ms
60,884 KB |
testcase_16 | AC | 659 ms
58,268 KB |
testcase_17 | AC | 655 ms
62,272 KB |
testcase_18 | AC | 652 ms
60,084 KB |
testcase_19 | AC | 730 ms
54,936 KB |
testcase_20 | AC | 835 ms
66,096 KB |
testcase_21 | AC | 417 ms
48,020 KB |
testcase_22 | AC | 542 ms
51,316 KB |
testcase_23 | AC | 528 ms
51,148 KB |
testcase_24 | AC | 779 ms
59,736 KB |
testcase_25 | AC | 506 ms
49,980 KB |
testcase_26 | AC | 787 ms
63,104 KB |
testcase_27 | AC | 758 ms
60,184 KB |
testcase_28 | AC | 616 ms
52,592 KB |
testcase_29 | AC | 736 ms
55,800 KB |
testcase_30 | AC | 464 ms
49,900 KB |
testcase_31 | AC | 709 ms
56,512 KB |
testcase_32 | AC | 487 ms
50,116 KB |
testcase_33 | AC | 637 ms
52,568 KB |
testcase_34 | AC | 827 ms
65,992 KB |
testcase_35 | AC | 792 ms
63,404 KB |
testcase_36 | AC | 549 ms
50,724 KB |
testcase_37 | AC | 608 ms
52,648 KB |
testcase_38 | AC | 509 ms
50,384 KB |
testcase_39 | AC | 538 ms
51,356 KB |
testcase_40 | AC | 763 ms
60,932 KB |
testcase_41 | AC | 513 ms
49,888 KB |
testcase_42 | AC | 712 ms
54,512 KB |
testcase_43 | AC | 686 ms
54,108 KB |
testcase_44 | AC | 649 ms
60,284 KB |
testcase_45 | AC | 648 ms
53,828 KB |
testcase_46 | AC | 676 ms
53,852 KB |
ソースコード
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(); } }