結果
問題 | No.30 たこやき工場 |
ユーザー |
![]() |
提出日時 | 2020-05-14 18:45:41 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 256 ms / 5,000 ms |
コード長 | 1,737 bytes |
コンパイル時間 | 2,767 ms |
コンパイル使用メモリ | 80,248 KB |
実行使用メモリ | 57,868 KB |
最終ジャッジ日時 | 2024-12-21 05:30:52 |
合計ジャッジ時間 | 6,509 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 17 |
ソースコード
import java.util.*; public class Main { static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>(); static ArrayList<HashMap<Integer, Integer>> result = new ArrayList<>(); public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); for (int i = 0; i < n; i++) { graph.add(new HashMap<>()); result.add(new HashMap<>()); } for (int i = 0; i < m; i++) { int source = sc.nextInt() - 1; int count = sc.nextInt(); graph.get(sc.nextInt() - 1).put(source, count); } HashMap<Integer, Integer> ans = search(n - 1); StringBuilder sb = new StringBuilder(); for (int i = 0; i < n - 1; i++) { if (ans.containsKey(i)) { sb.append(ans.get(i)); } else { sb.append("0"); } sb.append("\n"); } System.out.print(sb); } static HashMap<Integer, Integer> search(int idx) { HashMap<Integer, Integer> map = result.get(idx); if (map.size() > 0) { return map; } if (graph.get(idx).size() == 0) { map.put(idx, 1); return map; } for (Map.Entry<Integer, Integer> entry1 : graph.get(idx).entrySet()) { HashMap<Integer, Integer> parts = search(entry1.getKey()); for (Map.Entry<Integer, Integer> entry2 : parts.entrySet()) { int key = entry2.getKey(); int value = entry1.getValue() * entry2.getValue(); if (map.containsKey(key)) { map.put(key, map.get(key) + value); } else { map.put(key, value); } } } return map; } }