結果

問題 No.679 不思議マーケット
ユーザー tenten
提出日時 2020-12-22 10:42:29
言語 Java
(openjdk 23)
結果
AC  
実行時間 324 ms / 2,000 ms
コード長 1,330 bytes
コンパイル時間 2,560 ms
コンパイル使用メモリ 79,616 KB
実行使用メモリ 48,656 KB
最終ジャッジ日時 2024-09-21 14:01:12
合計ジャッジ時間 7,220 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

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