結果

問題 No.679 不思議マーケット
ユーザー tentententen
提出日時 2020-12-22 10:42:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 334 ms / 2,000 ms
コード長 1,330 bytes
コンパイル時間 3,606 ms
コンパイル使用メモリ 79,272 KB
実行使用メモリ 62,512 KB
最終ジャッジ日時 2023-10-21 12:45:13
合計ジャッジ時間 8,566 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
56,172 KB
testcase_01 AC 142 ms
57,484 KB
testcase_02 AC 139 ms
57,540 KB
testcase_03 AC 334 ms
62,512 KB
testcase_04 AC 265 ms
60,992 KB
testcase_05 AC 262 ms
60,828 KB
testcase_06 AC 219 ms
60,044 KB
testcase_07 AC 213 ms
60,104 KB
testcase_08 AC 260 ms
60,876 KB
testcase_09 AC 140 ms
57,544 KB
testcase_10 AC 222 ms
60,148 KB
testcase_11 AC 213 ms
60,104 KB
testcase_12 AC 227 ms
60,140 KB
testcase_13 AC 238 ms
60,712 KB
testcase_14 AC 228 ms
59,976 KB
testcase_15 AC 205 ms
60,068 KB
testcase_16 AC 148 ms
57,552 KB
testcase_17 AC 142 ms
57,708 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();
        ArrayList<HashSet<Integer>> outputs = new ArrayList<>();
        ArrayList<HashSet<Integer>> inputs = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            outputs.add(new HashSet<>());
            inputs.add(new HashSet<>());
        }
        for (int i = 0; i < m; i++) {
            int to = sc.nextInt() - 1;
            int count = sc.nextInt();
            for (int j = 0; j < count; j++) {
                int x = sc.nextInt() - 1;
                inputs.get(to).add(x);
                outputs.get(x).add(to);
            }
        }
        int ans = 0;
        ArrayDeque<Integer> deq = new ArrayDeque<>();
        for (int i = 0; i < n; i++) {
            if (inputs.get(i).size() == 0) {
                deq.add(i);
                ans++;
            }
        }
        while (deq.size() > 0) {
            int x = deq.poll();
            for (int y : outputs.get(x)) {
                inputs.get(y).remove(x);
                if (inputs.get(y).size() == 0) {
                    deq.add(y);
                    ans++;
                }
            }
        }
        System.out.println(ans);
    }
}
0