結果
問題 | No.1605 Matrix Shape |
ユーザー | ks2m |
提出日時 | 2021-07-16 22:31:41 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,132 bytes |
コンパイル時間 | 2,307 ms |
コンパイル使用メモリ | 74,760 KB |
実行使用メモリ | 131,392 KB |
最終ジャッジ日時 | 2023-09-20 14:50:08 |
合計ジャッジ時間 | 21,451 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 110 ms
65,056 KB |
testcase_01 | AC | 119 ms
64,732 KB |
testcase_02 | AC | 102 ms
65,024 KB |
testcase_03 | WA | - |
testcase_04 | AC | 108 ms
65,032 KB |
testcase_05 | AC | 126 ms
64,492 KB |
testcase_06 | AC | 120 ms
64,552 KB |
testcase_07 | AC | 102 ms
64,684 KB |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | AC | 109 ms
64,908 KB |
testcase_11 | AC | 101 ms
64,676 KB |
testcase_12 | AC | 118 ms
64,528 KB |
testcase_13 | AC | 108 ms
64,912 KB |
testcase_14 | AC | 108 ms
64,952 KB |
testcase_15 | AC | 102 ms
64,652 KB |
testcase_16 | AC | 101 ms
64,656 KB |
testcase_17 | AC | 102 ms
64,780 KB |
testcase_18 | AC | 103 ms
64,628 KB |
testcase_19 | AC | 1,214 ms
129,576 KB |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | AC | 960 ms
122,576 KB |
testcase_23 | AC | 1,132 ms
131,392 KB |
testcase_24 | AC | 1,162 ms
131,344 KB |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | AC | 915 ms
115,388 KB |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | AC | 700 ms
98,972 KB |
testcase_34 | WA | - |
testcase_35 | AC | 627 ms
101,636 KB |
testcase_36 | AC | 505 ms
95,248 KB |
ソースコード
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class Main { static int n, n2; static List<Set<Hen>> list; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); n = Integer.parseInt(br.readLine()); n2 = 0; int m = 200000; list = new ArrayList<>(m); for (int i = 0; i < m; i++) { list.add(new HashSet<>()); } int[] in = new int[m]; int[] out = new int[m]; for (int i = 0; i < n; i++) { String[] sa = br.readLine().split(" "); Hen h = new Hen(); h.a = Integer.parseInt(sa[0]) - 1; h.b = Integer.parseInt(sa[1]) - 1; if (h.a != h.b) { list.get(h.a).add(h); out[h.a]++; in[h.b]++; n2++; } } br.close(); int s = -1; int t = -1; boolean even = false; for (int i = 0; i < m; i++) { if (in[i] != out[i]) { if (Math.abs(in[i] - out[i]) > 1) { System.out.println(0); return; } if (in[i] + 1 == out[i]) { if (s == -1) { s = i; } else { System.out.println(0); return; } } else { if (t == -1) { t = i; } else { System.out.println(0); return; } } } } if (s == -1) { for (int i = 0; i < m; i++) { if (out[i] > 0) { s = i; break; } } even = true; } boolean res = dfs(s, new ArrayList<>(n)); if (res) { if (even) { int ans = 0; for (int i = 0; i < m; i++) { if (!list.get(i).isEmpty()) { ans++; } } System.out.println(ans); } else { System.out.println(1); } } else { System.out.println(0); } } static boolean dfs(int x, List<Hen> his) { if (his.size() == n2) { return true; } Set<Hen> set = list.get(x); if (!set.isEmpty()) { Hen h = set.iterator().next(); his.add(h); set.remove(h); boolean res = dfs(h.b, his); set.add(h); his.remove(his.size() - 1); if (res) { return true; } } return false; } static class Hen { int a, b; } }