結果
問題 | No.1254 補強への架け橋 |
ユーザー | tenten |
提出日時 | 2023-01-11 15:53:05 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 967 ms / 2,000 ms |
コード長 | 4,114 bytes |
コンパイル時間 | 3,034 ms |
コンパイル使用メモリ | 85,744 KB |
実行使用メモリ | 98,472 KB |
最終ジャッジ日時 | 2024-06-01 19:46:32 |
合計ジャッジ時間 | 37,079 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 50 ms
36,876 KB |
testcase_01 | AC | 50 ms
36,936 KB |
testcase_02 | AC | 51 ms
36,652 KB |
testcase_03 | AC | 51 ms
37,124 KB |
testcase_04 | AC | 51 ms
37,108 KB |
testcase_05 | AC | 48 ms
37,112 KB |
testcase_06 | AC | 49 ms
37,220 KB |
testcase_07 | AC | 48 ms
37,068 KB |
testcase_08 | AC | 46 ms
37,092 KB |
testcase_09 | AC | 47 ms
37,084 KB |
testcase_10 | AC | 47 ms
37,036 KB |
testcase_11 | AC | 47 ms
37,208 KB |
testcase_12 | AC | 47 ms
37,096 KB |
testcase_13 | AC | 51 ms
37,260 KB |
testcase_14 | AC | 48 ms
36,920 KB |
testcase_15 | AC | 49 ms
37,208 KB |
testcase_16 | AC | 49 ms
37,120 KB |
testcase_17 | AC | 49 ms
37,164 KB |
testcase_18 | AC | 48 ms
37,076 KB |
testcase_19 | AC | 51 ms
37,208 KB |
testcase_20 | AC | 50 ms
37,240 KB |
testcase_21 | AC | 48 ms
37,368 KB |
testcase_22 | AC | 46 ms
36,876 KB |
testcase_23 | AC | 48 ms
37,136 KB |
testcase_24 | AC | 48 ms
37,228 KB |
testcase_25 | AC | 48 ms
37,168 KB |
testcase_26 | AC | 56 ms
37,112 KB |
testcase_27 | AC | 51 ms
36,804 KB |
testcase_28 | AC | 48 ms
37,448 KB |
testcase_29 | AC | 48 ms
37,448 KB |
testcase_30 | AC | 48 ms
37,092 KB |
testcase_31 | AC | 50 ms
37,256 KB |
testcase_32 | AC | 48 ms
37,320 KB |
testcase_33 | AC | 50 ms
37,060 KB |
testcase_34 | AC | 49 ms
37,008 KB |
testcase_35 | AC | 49 ms
37,304 KB |
testcase_36 | AC | 48 ms
37,076 KB |
testcase_37 | AC | 49 ms
37,128 KB |
testcase_38 | AC | 50 ms
37,076 KB |
testcase_39 | AC | 48 ms
37,024 KB |
testcase_40 | AC | 50 ms
37,172 KB |
testcase_41 | AC | 51 ms
37,164 KB |
testcase_42 | AC | 50 ms
37,164 KB |
testcase_43 | AC | 86 ms
38,580 KB |
testcase_44 | AC | 54 ms
37,760 KB |
testcase_45 | AC | 56 ms
37,384 KB |
testcase_46 | AC | 86 ms
38,616 KB |
testcase_47 | AC | 73 ms
37,968 KB |
testcase_48 | AC | 80 ms
38,432 KB |
testcase_49 | AC | 51 ms
37,136 KB |
testcase_50 | AC | 88 ms
38,920 KB |
testcase_51 | AC | 90 ms
38,600 KB |
testcase_52 | AC | 85 ms
38,436 KB |
testcase_53 | AC | 75 ms
38,796 KB |
testcase_54 | AC | 81 ms
38,700 KB |
testcase_55 | AC | 80 ms
38,056 KB |
testcase_56 | AC | 54 ms
37,336 KB |
testcase_57 | AC | 77 ms
38,244 KB |
testcase_58 | AC | 73 ms
38,388 KB |
testcase_59 | AC | 52 ms
36,920 KB |
testcase_60 | AC | 57 ms
37,372 KB |
testcase_61 | AC | 88 ms
38,288 KB |
testcase_62 | AC | 52 ms
36,872 KB |
testcase_63 | AC | 155 ms
45,396 KB |
testcase_64 | AC | 127 ms
40,524 KB |
testcase_65 | AC | 142 ms
42,816 KB |
testcase_66 | AC | 139 ms
41,736 KB |
testcase_67 | AC | 113 ms
39,816 KB |
testcase_68 | AC | 139 ms
42,224 KB |
testcase_69 | AC | 144 ms
45,388 KB |
testcase_70 | AC | 127 ms
40,632 KB |
testcase_71 | AC | 125 ms
40,308 KB |
testcase_72 | AC | 144 ms
45,456 KB |
testcase_73 | AC | 118 ms
40,776 KB |
testcase_74 | AC | 148 ms
45,308 KB |
testcase_75 | AC | 137 ms
42,248 KB |
testcase_76 | AC | 97 ms
38,584 KB |
testcase_77 | AC | 134 ms
41,580 KB |
testcase_78 | AC | 152 ms
45,704 KB |
testcase_79 | AC | 160 ms
45,496 KB |
testcase_80 | AC | 149 ms
45,352 KB |
testcase_81 | AC | 162 ms
45,504 KB |
testcase_82 | AC | 155 ms
45,684 KB |
testcase_83 | AC | 626 ms
75,708 KB |
testcase_84 | AC | 566 ms
74,740 KB |
testcase_85 | AC | 388 ms
66,320 KB |
testcase_86 | AC | 622 ms
71,592 KB |
testcase_87 | AC | 563 ms
72,024 KB |
testcase_88 | AC | 174 ms
49,340 KB |
testcase_89 | AC | 587 ms
75,632 KB |
testcase_90 | AC | 406 ms
66,712 KB |
testcase_91 | AC | 332 ms
61,628 KB |
testcase_92 | AC | 213 ms
53,364 KB |
testcase_93 | AC | 506 ms
71,712 KB |
testcase_94 | AC | 455 ms
67,608 KB |
testcase_95 | AC | 421 ms
67,464 KB |
testcase_96 | AC | 565 ms
73,460 KB |
testcase_97 | AC | 313 ms
58,672 KB |
testcase_98 | AC | 579 ms
73,532 KB |
testcase_99 | AC | 378 ms
66,164 KB |
testcase_100 | AC | 592 ms
76,516 KB |
testcase_101 | AC | 212 ms
52,444 KB |
testcase_102 | AC | 169 ms
45,576 KB |
testcase_103 | AC | 228 ms
53,112 KB |
testcase_104 | AC | 279 ms
57,008 KB |
testcase_105 | AC | 522 ms
71,724 KB |
testcase_106 | AC | 332 ms
57,984 KB |
testcase_107 | AC | 582 ms
76,272 KB |
testcase_108 | AC | 576 ms
74,404 KB |
testcase_109 | AC | 499 ms
71,592 KB |
testcase_110 | AC | 441 ms
67,520 KB |
testcase_111 | AC | 497 ms
71,288 KB |
testcase_112 | AC | 263 ms
55,780 KB |
testcase_113 | AC | 441 ms
67,576 KB |
testcase_114 | AC | 316 ms
58,476 KB |
testcase_115 | AC | 178 ms
49,540 KB |
testcase_116 | AC | 375 ms
61,944 KB |
testcase_117 | AC | 256 ms
56,760 KB |
testcase_118 | AC | 571 ms
72,732 KB |
testcase_119 | AC | 383 ms
65,888 KB |
testcase_120 | AC | 553 ms
72,456 KB |
testcase_121 | AC | 229 ms
53,232 KB |
testcase_122 | AC | 335 ms
61,784 KB |
testcase_123 | AC | 47 ms
36,936 KB |
testcase_124 | AC | 967 ms
93,728 KB |
testcase_125 | AC | 783 ms
98,472 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[] depths; 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<>()); } UnionFindTree uft = new UnionFindTree(n); int left = 0; int right = 0; 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)) { left = a; right = 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]; depths = new int[n]; setDepth(0, 0, 0); while (left != right) { if (depths[left] == depths[right]) { ans.add(graph.get(left).get(parents[left])); left = parents[left]; ans.add(graph.get(right).get(parents[right])); right = parents[right]; } else if (depths[left] < depths[right]){ ans.add(graph.get(right).get(parents[right])); right = parents[right]; } else { ans.add(graph.get(left).get(parents[left])); left = parents[left]; } } 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) { depths[idx] = d; parents[idx] = p; for (int x : graph.get(idx).keySet()) { if (x != p) { 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 (parents[x] == 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) { if (!same(x, 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(); } }