結果
問題 | No.2290 UnUnion Find |
ユーザー | tenten |
提出日時 | 2023-07-26 20:39:01 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 671 ms / 2,000 ms |
コード長 | 2,942 bytes |
コンパイル時間 | 2,173 ms |
コンパイル使用メモリ | 85,356 KB |
実行使用メモリ | 66,012 KB |
最終ジャッジ日時 | 2024-10-03 07:03:39 |
合計ジャッジ時間 | 32,364 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 48 ms
37,468 KB |
testcase_01 | AC | 52 ms
37,312 KB |
testcase_02 | AC | 308 ms
48,040 KB |
testcase_03 | AC | 532 ms
53,904 KB |
testcase_04 | AC | 562 ms
59,736 KB |
testcase_05 | AC | 577 ms
59,568 KB |
testcase_06 | AC | 557 ms
60,940 KB |
testcase_07 | AC | 571 ms
58,268 KB |
testcase_08 | AC | 547 ms
60,492 KB |
testcase_09 | AC | 546 ms
57,408 KB |
testcase_10 | AC | 557 ms
58,928 KB |
testcase_11 | AC | 535 ms
60,480 KB |
testcase_12 | AC | 542 ms
59,164 KB |
testcase_13 | AC | 529 ms
59,584 KB |
testcase_14 | AC | 523 ms
59,740 KB |
testcase_15 | AC | 569 ms
60,920 KB |
testcase_16 | AC | 525 ms
58,420 KB |
testcase_17 | AC | 544 ms
62,304 KB |
testcase_18 | AC | 521 ms
60,304 KB |
testcase_19 | AC | 592 ms
56,932 KB |
testcase_20 | AC | 670 ms
66,012 KB |
testcase_21 | AC | 355 ms
48,412 KB |
testcase_22 | AC | 444 ms
51,428 KB |
testcase_23 | AC | 458 ms
51,432 KB |
testcase_24 | AC | 639 ms
60,888 KB |
testcase_25 | AC | 420 ms
50,248 KB |
testcase_26 | AC | 621 ms
63,912 KB |
testcase_27 | AC | 613 ms
62,864 KB |
testcase_28 | AC | 502 ms
52,880 KB |
testcase_29 | AC | 590 ms
58,220 KB |
testcase_30 | AC | 407 ms
50,104 KB |
testcase_31 | AC | 625 ms
57,052 KB |
testcase_32 | AC | 433 ms
50,172 KB |
testcase_33 | AC | 516 ms
52,544 KB |
testcase_34 | AC | 671 ms
65,804 KB |
testcase_35 | AC | 656 ms
63,504 KB |
testcase_36 | AC | 461 ms
50,800 KB |
testcase_37 | AC | 498 ms
52,668 KB |
testcase_38 | AC | 434 ms
50,972 KB |
testcase_39 | AC | 450 ms
51,604 KB |
testcase_40 | AC | 623 ms
62,328 KB |
testcase_41 | AC | 429 ms
50,232 KB |
testcase_42 | AC | 581 ms
54,888 KB |
testcase_43 | AC | 556 ms
54,128 KB |
testcase_44 | AC | 540 ms
60,352 KB |
testcase_45 | AC | 549 ms
54,212 KB |
testcase_46 | AC | 565 ms
54,240 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.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(); } }