結果
問題 | No.1194 Replace |
ユーザー | tenten |
提出日時 | 2023-02-01 16:22:20 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,599 bytes |
コンパイル時間 | 5,228 ms |
コンパイル使用メモリ | 88,220 KB |
実行使用メモリ | 148,668 KB |
最終ジャッジ日時 | 2023-09-14 03:27:31 |
合計ジャッジ時間 | 40,307 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge15 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | AC | 1,489 ms
139,620 KB |
testcase_08 | AC | 1,726 ms
143,268 KB |
testcase_09 | AC | 1,930 ms
144,392 KB |
testcase_10 | AC | 1,688 ms
145,692 KB |
testcase_11 | TLE | - |
testcase_12 | AC | 1,696 ms
142,688 KB |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | AC | 47 ms
49,560 KB |
testcase_21 | AC | 46 ms
49,460 KB |
testcase_22 | AC | 46 ms
49,428 KB |
testcase_23 | WA | - |
testcase_24 | AC | 244 ms
59,916 KB |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | AC | 151 ms
55,544 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { static HashMap<Integer, ArrayList<Integer>> graph = new HashMap<>(); static HashMap<Integer, Integer> visited = new HashMap<>(); public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int m = sc.nextInt(); for (int i = 0; i < m; i++) { int left = sc.nextInt(); int right = sc.nextInt(); if (!graph.containsKey(left)) { graph.put(left, new ArrayList<>()); } graph.get(left).add(right); } long ans = (n + 1L) * n / 2; for (int x : graph.keySet()) { int value = getValue(x, new HashSet<>()); ans += value - x; } System.out.println(ans); } static int getValue(int x, HashSet<Integer> used) { if (used.contains(x)) { return 0; } if (!visited.containsKey(x)) { used.add(x); int value = x; if (graph.containsKey(x)) { for (int y : graph.get(x)) { value = Math.max(value, getValue(y, used)); } } visited.put(x, value); used.remove(x); } return visited.get(x); } } 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(); } }