結果

問題 No.1147 土偶Ⅱ
ユーザー tentententen
提出日時 2020-08-17 18:55:03
言語 Java21
(openjdk 21)
結果
AC  
実行時間 205 ms / 500 ms
コード長 1,188 bytes
コンパイル時間 1,787 ms
コンパイル使用メモリ 77,380 KB
実行使用メモリ 44,200 KB
最終ジャッジ日時 2024-04-19 19:02:42
合計ジャッジ時間 6,127 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 109 ms
41,472 KB
testcase_01 AC 102 ms
41,332 KB
testcase_02 AC 113 ms
41,428 KB
testcase_03 AC 110 ms
41,352 KB
testcase_04 AC 175 ms
42,284 KB
testcase_05 AC 121 ms
41,480 KB
testcase_06 AC 175 ms
44,200 KB
testcase_07 AC 193 ms
42,908 KB
testcase_08 AC 137 ms
41,352 KB
testcase_09 AC 185 ms
43,208 KB
testcase_10 AC 179 ms
42,464 KB
testcase_11 AC 205 ms
42,848 KB
testcase_12 AC 183 ms
42,796 KB
testcase_13 AC 145 ms
41,576 KB
testcase_14 AC 189 ms
42,044 KB
testcase_15 AC 187 ms
43,356 KB
testcase_16 AC 165 ms
42,160 KB
testcase_17 AC 132 ms
41,816 KB
testcase_18 AC 120 ms
41,340 KB
testcase_19 AC 189 ms
43,684 KB
testcase_20 AC 125 ms
41,204 KB
testcase_21 AC 105 ms
41,280 KB
testcase_22 AC 179 ms
43,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] costs = new int[n][n];
        for (int i = 0; i < n; i++) {
            Arrays.fill(costs[i], Integer.MAX_VALUE / 10);
            costs[i][i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            costs[a][b] = 1;
            costs[b][a] = 1;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    costs[j][k] = Math.min(costs[j][k], costs[j][i] + costs[i][k]);
                }
            }
        }
        int count = 0;
        for (int i = 0; i < n - 2; i++) {
            for (int j = i + 1; j < n - 1; j++) {
                for (int k = j + 1; k < n; k++) {
                    if (costs[i][j] != 2 && costs[j][k] != 2 && costs[i][k] != 2) {
                        count++;
                    }
                }
            }
        }
        System.out.println(count);
    }
}
0