結果

問題 No.1254 補強への架け橋
ユーザー tentententen
提出日時 2023-01-11 15:53:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 996 ms / 2,000 ms
コード長 4,114 bytes
コンパイル時間 4,780 ms
コンパイル使用メモリ 82,088 KB
実行使用メモリ 116,496 KB
最終ジャッジ日時 2023-08-23 22:18:54
合計ジャッジ時間 42,683 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
49,360 KB
testcase_01 AC 44 ms
49,400 KB
testcase_02 AC 44 ms
49,400 KB
testcase_03 AC 43 ms
49,300 KB
testcase_04 AC 43 ms
49,500 KB
testcase_05 AC 43 ms
49,312 KB
testcase_06 AC 43 ms
49,948 KB
testcase_07 AC 43 ms
49,268 KB
testcase_08 AC 46 ms
49,300 KB
testcase_09 AC 44 ms
49,420 KB
testcase_10 AC 46 ms
49,364 KB
testcase_11 AC 45 ms
49,296 KB
testcase_12 AC 44 ms
49,252 KB
testcase_13 AC 47 ms
49,364 KB
testcase_14 AC 46 ms
49,376 KB
testcase_15 AC 46 ms
49,972 KB
testcase_16 AC 44 ms
49,552 KB
testcase_17 AC 43 ms
49,352 KB
testcase_18 AC 44 ms
49,824 KB
testcase_19 AC 44 ms
49,396 KB
testcase_20 AC 45 ms
49,644 KB
testcase_21 AC 43 ms
49,308 KB
testcase_22 AC 45 ms
49,352 KB
testcase_23 AC 45 ms
49,404 KB
testcase_24 AC 46 ms
49,256 KB
testcase_25 AC 45 ms
49,428 KB
testcase_26 AC 46 ms
49,340 KB
testcase_27 AC 45 ms
49,292 KB
testcase_28 AC 46 ms
49,548 KB
testcase_29 AC 46 ms
49,504 KB
testcase_30 AC 44 ms
49,420 KB
testcase_31 AC 46 ms
49,468 KB
testcase_32 AC 45 ms
49,408 KB
testcase_33 AC 46 ms
49,456 KB
testcase_34 AC 48 ms
49,300 KB
testcase_35 AC 46 ms
49,836 KB
testcase_36 AC 46 ms
49,728 KB
testcase_37 AC 45 ms
47,296 KB
testcase_38 AC 46 ms
47,440 KB
testcase_39 AC 45 ms
49,808 KB
testcase_40 AC 45 ms
49,408 KB
testcase_41 AC 45 ms
49,464 KB
testcase_42 AC 44 ms
49,300 KB
testcase_43 AC 72 ms
51,556 KB
testcase_44 AC 56 ms
50,320 KB
testcase_45 AC 55 ms
51,348 KB
testcase_46 AC 72 ms
52,120 KB
testcase_47 AC 72 ms
50,892 KB
testcase_48 AC 82 ms
51,800 KB
testcase_49 AC 48 ms
49,404 KB
testcase_50 AC 78 ms
51,864 KB
testcase_51 AC 75 ms
51,932 KB
testcase_52 AC 86 ms
50,432 KB
testcase_53 AC 73 ms
51,520 KB
testcase_54 AC 76 ms
51,524 KB
testcase_55 AC 83 ms
51,584 KB
testcase_56 AC 50 ms
49,316 KB
testcase_57 AC 66 ms
50,756 KB
testcase_58 AC 67 ms
50,524 KB
testcase_59 AC 47 ms
49,480 KB
testcase_60 AC 57 ms
48,104 KB
testcase_61 AC 87 ms
51,608 KB
testcase_62 AC 49 ms
49,436 KB
testcase_63 AC 160 ms
55,524 KB
testcase_64 AC 123 ms
52,588 KB
testcase_65 AC 131 ms
55,256 KB
testcase_66 AC 136 ms
55,144 KB
testcase_67 AC 109 ms
52,544 KB
testcase_68 AC 136 ms
54,960 KB
testcase_69 AC 138 ms
55,148 KB
testcase_70 AC 128 ms
52,312 KB
testcase_71 AC 120 ms
52,248 KB
testcase_72 AC 150 ms
55,268 KB
testcase_73 AC 124 ms
52,336 KB
testcase_74 AC 146 ms
55,516 KB
testcase_75 AC 138 ms
54,968 KB
testcase_76 AC 95 ms
51,604 KB
testcase_77 AC 131 ms
54,908 KB
testcase_78 AC 145 ms
55,720 KB
testcase_79 AC 156 ms
55,388 KB
testcase_80 AC 157 ms
55,412 KB
testcase_81 AC 169 ms
55,252 KB
testcase_82 AC 156 ms
55,276 KB
testcase_83 AC 662 ms
87,596 KB
testcase_84 AC 654 ms
86,452 KB
testcase_85 AC 439 ms
76,748 KB
testcase_86 AC 595 ms
81,796 KB
testcase_87 AC 618 ms
83,240 KB
testcase_88 AC 181 ms
57,380 KB
testcase_89 AC 655 ms
88,524 KB
testcase_90 AC 451 ms
77,084 KB
testcase_91 AC 376 ms
72,252 KB
testcase_92 AC 239 ms
64,548 KB
testcase_93 AC 577 ms
81,256 KB
testcase_94 AC 519 ms
78,664 KB
testcase_95 AC 506 ms
78,820 KB
testcase_96 AC 651 ms
85,364 KB
testcase_97 AC 351 ms
68,696 KB
testcase_98 AC 657 ms
88,752 KB
testcase_99 AC 437 ms
75,944 KB
testcase_100 AC 673 ms
88,480 KB
testcase_101 AC 229 ms
62,088 KB
testcase_102 AC 170 ms
59,740 KB
testcase_103 AC 243 ms
64,144 KB
testcase_104 AC 310 ms
66,672 KB
testcase_105 AC 572 ms
81,524 KB
testcase_106 AC 359 ms
68,328 KB
testcase_107 AC 663 ms
87,928 KB
testcase_108 AC 678 ms
86,980 KB
testcase_109 AC 594 ms
81,824 KB
testcase_110 AC 500 ms
78,884 KB
testcase_111 AC 597 ms
81,524 KB
testcase_112 AC 292 ms
66,352 KB
testcase_113 AC 509 ms
77,084 KB
testcase_114 AC 389 ms
68,744 KB
testcase_115 AC 191 ms
60,072 KB
testcase_116 AC 413 ms
73,500 KB
testcase_117 AC 322 ms
66,308 KB
testcase_118 AC 669 ms
88,156 KB
testcase_119 AC 445 ms
75,812 KB
testcase_120 AC 635 ms
82,472 KB
testcase_121 AC 259 ms
64,552 KB
testcase_122 AC 389 ms
73,696 KB
testcase_123 AC 43 ms
49,372 KB
testcase_124 AC 996 ms
116,496 KB
testcase_125 AC 963 ms
115,048 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[] 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();
    }
    
}
0