結果

問題 No.1254 補強への架け橋
ユーザー tentententen
提出日時 2023-03-22 15:27:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,263 ms / 2,000 ms
コード長 3,953 bytes
コンパイル時間 2,748 ms
コンパイル使用メモリ 85,868 KB
実行使用メモリ 113,864 KB
最終ジャッジ日時 2023-10-18 19:00:35
合計ジャッジ時間 46,105 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
52,444 KB
testcase_01 AC 53 ms
53,452 KB
testcase_02 AC 53 ms
53,440 KB
testcase_03 AC 53 ms
53,444 KB
testcase_04 AC 56 ms
53,444 KB
testcase_05 AC 54 ms
53,440 KB
testcase_06 AC 54 ms
53,440 KB
testcase_07 AC 56 ms
53,436 KB
testcase_08 AC 56 ms
53,444 KB
testcase_09 AC 54 ms
53,440 KB
testcase_10 AC 55 ms
53,440 KB
testcase_11 AC 54 ms
53,436 KB
testcase_12 AC 55 ms
53,444 KB
testcase_13 AC 58 ms
52,352 KB
testcase_14 AC 58 ms
53,444 KB
testcase_15 AC 56 ms
53,444 KB
testcase_16 AC 56 ms
53,444 KB
testcase_17 AC 55 ms
53,444 KB
testcase_18 AC 55 ms
53,436 KB
testcase_19 AC 56 ms
52,404 KB
testcase_20 AC 56 ms
53,440 KB
testcase_21 AC 57 ms
53,444 KB
testcase_22 AC 56 ms
53,448 KB
testcase_23 AC 55 ms
53,452 KB
testcase_24 AC 57 ms
53,444 KB
testcase_25 AC 57 ms
52,348 KB
testcase_26 AC 57 ms
53,384 KB
testcase_27 AC 56 ms
53,444 KB
testcase_28 AC 59 ms
53,448 KB
testcase_29 AC 56 ms
53,452 KB
testcase_30 AC 55 ms
53,440 KB
testcase_31 AC 55 ms
53,452 KB
testcase_32 AC 55 ms
53,440 KB
testcase_33 AC 55 ms
53,452 KB
testcase_34 AC 56 ms
53,452 KB
testcase_35 AC 58 ms
53,448 KB
testcase_36 AC 55 ms
53,448 KB
testcase_37 AC 55 ms
53,440 KB
testcase_38 AC 58 ms
53,440 KB
testcase_39 AC 56 ms
53,444 KB
testcase_40 AC 58 ms
53,452 KB
testcase_41 AC 56 ms
53,444 KB
testcase_42 AC 55 ms
53,444 KB
testcase_43 AC 97 ms
54,592 KB
testcase_44 AC 70 ms
54,028 KB
testcase_45 AC 65 ms
53,472 KB
testcase_46 AC 94 ms
54,320 KB
testcase_47 AC 73 ms
54,036 KB
testcase_48 AC 80 ms
54,324 KB
testcase_49 AC 56 ms
53,444 KB
testcase_50 AC 89 ms
54,352 KB
testcase_51 AC 99 ms
54,316 KB
testcase_52 AC 98 ms
54,324 KB
testcase_53 AC 86 ms
54,320 KB
testcase_54 AC 100 ms
54,324 KB
testcase_55 AC 88 ms
54,404 KB
testcase_56 AC 58 ms
53,452 KB
testcase_57 AC 90 ms
54,316 KB
testcase_58 AC 91 ms
54,404 KB
testcase_59 AC 57 ms
53,452 KB
testcase_60 AC 76 ms
54,112 KB
testcase_61 AC 88 ms
54,316 KB
testcase_62 AC 58 ms
53,468 KB
testcase_63 AC 181 ms
60,660 KB
testcase_64 AC 135 ms
55,508 KB
testcase_65 AC 158 ms
57,968 KB
testcase_66 AC 155 ms
57,888 KB
testcase_67 AC 119 ms
55,220 KB
testcase_68 AC 154 ms
57,964 KB
testcase_69 AC 169 ms
58,424 KB
testcase_70 AC 141 ms
55,568 KB
testcase_71 AC 138 ms
55,412 KB
testcase_72 AC 172 ms
58,616 KB
testcase_73 AC 138 ms
55,820 KB
testcase_74 AC 160 ms
58,572 KB
testcase_75 AC 155 ms
57,996 KB
testcase_76 AC 99 ms
54,760 KB
testcase_77 AC 145 ms
55,836 KB
testcase_78 AC 168 ms
58,580 KB
testcase_79 AC 180 ms
58,528 KB
testcase_80 AC 165 ms
58,344 KB
testcase_81 AC 179 ms
58,712 KB
testcase_82 AC 175 ms
58,696 KB
testcase_83 AC 743 ms
89,472 KB
testcase_84 AC 721 ms
88,852 KB
testcase_85 AC 510 ms
79,112 KB
testcase_86 AC 675 ms
84,648 KB
testcase_87 AC 681 ms
85,200 KB
testcase_88 AC 218 ms
61,896 KB
testcase_89 AC 723 ms
89,028 KB
testcase_90 AC 510 ms
79,636 KB
testcase_91 AC 465 ms
75,824 KB
testcase_92 AC 281 ms
68,092 KB
testcase_93 AC 665 ms
84,740 KB
testcase_94 AC 557 ms
81,108 KB
testcase_95 AC 555 ms
80,676 KB
testcase_96 AC 722 ms
83,916 KB
testcase_97 AC 427 ms
71,864 KB
testcase_98 AC 709 ms
85,992 KB
testcase_99 AC 486 ms
78,740 KB
testcase_100 AC 726 ms
89,392 KB
testcase_101 AC 275 ms
67,240 KB
testcase_102 AC 197 ms
60,472 KB
testcase_103 AC 277 ms
68,024 KB
testcase_104 AC 342 ms
69,088 KB
testcase_105 AC 663 ms
84,916 KB
testcase_106 AC 425 ms
71,196 KB
testcase_107 AC 728 ms
88,844 KB
testcase_108 AC 736 ms
89,564 KB
testcase_109 AC 658 ms
84,520 KB
testcase_110 AC 564 ms
80,900 KB
testcase_111 AC 647 ms
84,072 KB
testcase_112 AC 340 ms
69,136 KB
testcase_113 AC 556 ms
80,908 KB
testcase_114 AC 430 ms
71,616 KB
testcase_115 AC 226 ms
62,448 KB
testcase_116 AC 468 ms
75,960 KB
testcase_117 AC 345 ms
69,108 KB
testcase_118 AC 718 ms
85,668 KB
testcase_119 AC 485 ms
78,676 KB
testcase_120 AC 765 ms
85,120 KB
testcase_121 AC 297 ms
68,048 KB
testcase_122 AC 457 ms
75,948 KB
testcase_123 AC 55 ms
53,436 KB
testcase_124 AC 1,263 ms
109,432 KB
testcase_125 AC 1,006 ms
113,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
    }
    
}
0