結果
問題 | No.1254 補強への架け橋 |
ユーザー | tenten |
提出日時 | 2023-03-22 15:27:23 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 967 ms / 2,000 ms |
コード長 | 3,953 bytes |
コンパイル時間 | 2,326 ms |
コンパイル使用メモリ | 85,324 KB |
実行使用メモリ | 102,716 KB |
最終ジャッジ日時 | 2024-09-18 14:54:52 |
合計ジャッジ時間 | 36,746 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
37,364 KB |
testcase_01 | AC | 46 ms
37,104 KB |
testcase_02 | AC | 46 ms
37,128 KB |
testcase_03 | AC | 44 ms
37,088 KB |
testcase_04 | AC | 46 ms
37,080 KB |
testcase_05 | AC | 45 ms
36,812 KB |
testcase_06 | AC | 47 ms
37,376 KB |
testcase_07 | AC | 46 ms
36,908 KB |
testcase_08 | AC | 48 ms
37,436 KB |
testcase_09 | AC | 47 ms
37,312 KB |
testcase_10 | AC | 43 ms
37,192 KB |
testcase_11 | AC | 46 ms
37,148 KB |
testcase_12 | AC | 49 ms
37,100 KB |
testcase_13 | AC | 49 ms
37,016 KB |
testcase_14 | AC | 51 ms
42,448 KB |
testcase_15 | AC | 48 ms
36,944 KB |
testcase_16 | AC | 47 ms
37,348 KB |
testcase_17 | AC | 48 ms
36,768 KB |
testcase_18 | AC | 47 ms
37,212 KB |
testcase_19 | AC | 45 ms
37,016 KB |
testcase_20 | AC | 47 ms
36,808 KB |
testcase_21 | AC | 48 ms
36,652 KB |
testcase_22 | AC | 47 ms
37,212 KB |
testcase_23 | AC | 48 ms
37,008 KB |
testcase_24 | AC | 47 ms
37,476 KB |
testcase_25 | AC | 47 ms
37,476 KB |
testcase_26 | AC | 49 ms
37,096 KB |
testcase_27 | AC | 46 ms
37,268 KB |
testcase_28 | AC | 45 ms
37,020 KB |
testcase_29 | AC | 45 ms
37,172 KB |
testcase_30 | AC | 51 ms
37,152 KB |
testcase_31 | AC | 47 ms
37,120 KB |
testcase_32 | AC | 45 ms
37,292 KB |
testcase_33 | AC | 47 ms
36,972 KB |
testcase_34 | AC | 46 ms
37,148 KB |
testcase_35 | AC | 46 ms
37,388 KB |
testcase_36 | AC | 47 ms
37,444 KB |
testcase_37 | AC | 46 ms
36,800 KB |
testcase_38 | AC | 48 ms
37,224 KB |
testcase_39 | AC | 46 ms
37,120 KB |
testcase_40 | AC | 48 ms
37,368 KB |
testcase_41 | AC | 47 ms
37,440 KB |
testcase_42 | AC | 48 ms
37,120 KB |
testcase_43 | AC | 85 ms
38,312 KB |
testcase_44 | AC | 53 ms
37,748 KB |
testcase_45 | AC | 51 ms
36,944 KB |
testcase_46 | AC | 83 ms
38,456 KB |
testcase_47 | AC | 61 ms
38,212 KB |
testcase_48 | AC | 77 ms
38,392 KB |
testcase_49 | AC | 46 ms
37,440 KB |
testcase_50 | AC | 86 ms
38,572 KB |
testcase_51 | AC | 84 ms
38,884 KB |
testcase_52 | AC | 87 ms
38,420 KB |
testcase_53 | AC | 81 ms
38,620 KB |
testcase_54 | AC | 83 ms
38,352 KB |
testcase_55 | AC | 70 ms
38,752 KB |
testcase_56 | AC | 48 ms
36,924 KB |
testcase_57 | AC | 77 ms
38,244 KB |
testcase_58 | AC | 75 ms
38,292 KB |
testcase_59 | AC | 49 ms
37,216 KB |
testcase_60 | AC | 51 ms
37,488 KB |
testcase_61 | AC | 81 ms
38,328 KB |
testcase_62 | AC | 48 ms
37,348 KB |
testcase_63 | AC | 148 ms
45,540 KB |
testcase_64 | AC | 120 ms
40,516 KB |
testcase_65 | AC | 142 ms
42,728 KB |
testcase_66 | AC | 134 ms
41,868 KB |
testcase_67 | AC | 100 ms
39,700 KB |
testcase_68 | AC | 135 ms
42,380 KB |
testcase_69 | AC | 145 ms
45,340 KB |
testcase_70 | AC | 122 ms
40,896 KB |
testcase_71 | AC | 124 ms
40,572 KB |
testcase_72 | AC | 148 ms
45,260 KB |
testcase_73 | AC | 121 ms
40,496 KB |
testcase_74 | AC | 133 ms
45,208 KB |
testcase_75 | AC | 132 ms
42,028 KB |
testcase_76 | AC | 79 ms
38,880 KB |
testcase_77 | AC | 131 ms
41,832 KB |
testcase_78 | AC | 149 ms
45,428 KB |
testcase_79 | AC | 143 ms
45,352 KB |
testcase_80 | AC | 149 ms
45,648 KB |
testcase_81 | AC | 157 ms
45,728 KB |
testcase_82 | AC | 148 ms
45,252 KB |
testcase_83 | AC | 598 ms
76,132 KB |
testcase_84 | AC | 619 ms
75,028 KB |
testcase_85 | AC | 400 ms
66,236 KB |
testcase_86 | AC | 601 ms
71,532 KB |
testcase_87 | AC | 589 ms
72,740 KB |
testcase_88 | AC | 177 ms
48,708 KB |
testcase_89 | AC | 598 ms
75,872 KB |
testcase_90 | AC | 410 ms
66,548 KB |
testcase_91 | AC | 358 ms
61,644 KB |
testcase_92 | AC | 228 ms
52,716 KB |
testcase_93 | AC | 546 ms
71,708 KB |
testcase_94 | AC | 493 ms
67,692 KB |
testcase_95 | AC | 467 ms
67,444 KB |
testcase_96 | AC | 634 ms
73,600 KB |
testcase_97 | AC | 330 ms
58,120 KB |
testcase_98 | AC | 623 ms
74,532 KB |
testcase_99 | AC | 424 ms
65,868 KB |
testcase_100 | AC | 646 ms
77,064 KB |
testcase_101 | AC | 215 ms
52,180 KB |
testcase_102 | AC | 170 ms
45,872 KB |
testcase_103 | AC | 234 ms
52,068 KB |
testcase_104 | AC | 273 ms
57,144 KB |
testcase_105 | AC | 563 ms
71,556 KB |
testcase_106 | AC | 335 ms
58,200 KB |
testcase_107 | AC | 625 ms
75,752 KB |
testcase_108 | AC | 637 ms
74,932 KB |
testcase_109 | AC | 569 ms
71,280 KB |
testcase_110 | AC | 451 ms
67,468 KB |
testcase_111 | AC | 514 ms
71,236 KB |
testcase_112 | AC | 286 ms
57,120 KB |
testcase_113 | AC | 496 ms
67,260 KB |
testcase_114 | AC | 375 ms
58,064 KB |
testcase_115 | AC | 186 ms
49,604 KB |
testcase_116 | AC | 380 ms
61,804 KB |
testcase_117 | AC | 278 ms
56,708 KB |
testcase_118 | AC | 610 ms
73,208 KB |
testcase_119 | AC | 398 ms
65,928 KB |
testcase_120 | AC | 569 ms
72,256 KB |
testcase_121 | AC | 229 ms
53,396 KB |
testcase_122 | AC | 342 ms
61,712 KB |
testcase_123 | AC | 45 ms
37,092 KB |
testcase_124 | AC | 967 ms
94,136 KB |
testcase_125 | AC | 840 ms
102,716 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>(); static int[] parents; static int[] depth; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); for (int i = 0; i < n; i++) { graph.add(new HashMap<>()); } int p = 0; int q = 0; UnionFindTree uft = new UnionFindTree(n); ArrayList<Integer> ans = new ArrayList<>(); for (int i = 0; i < n; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; if (uft.same(a, b)) { p = a; q = b; ans.add(i); } else { uft.unite(a, b); graph.get(a).put(b, i); graph.get(b).put(a, i); } } parents = new int[n]; depth = new int[n]; setDepth(0, 0, 0); while (depth[p] != depth[q]) { if (depth[p] < depth[q]) { ans.add(graph.get(q).get(parents[q])); q = parents[q]; } else { ans.add(graph.get(p).get(parents[p])); p = parents[p]; } } while (p != q) { ans.add(graph.get(p).get(parents[p])); p = parents[p]; ans.add(graph.get(q).get(parents[q])); q = parents[q]; } StringBuilder sb = new StringBuilder(); sb.append(ans.size()).append("\n"); for (int i = 0; i < ans.size(); i++) { if (i > 0) { sb.append(" "); } sb.append(ans.get(i) + 1); } System.out.println(sb); } static void setDepth(int idx, int p, int d) { parents[idx] = p; depth[idx] = d; for (int x : graph.get(idx).keySet()) { if (p != x) { setDepth(x, idx, d + 1); } } } static class UnionFindTree { int[] parents; public UnionFindTree(int size) { parents = new int[size]; for (int i = 0; i < size; i++) { parents[i] = 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) { parents[find(x)] = find(y); } } } 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(); } }