結果

問題 No.1605 Matrix Shape
ユーザー ks2mks2m
提出日時 2021-07-16 22:18:30
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,026 bytes
コンパイル時間 2,099 ms
コンパイル使用メモリ 74,720 KB
実行使用メモリ 103,920 KB
最終ジャッジ日時 2023-09-20 14:29:31
合計ジャッジ時間 8,897 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
60,652 KB
testcase_01 AC 86 ms
56,304 KB
testcase_02 AC 70 ms
56,116 KB
testcase_03 AC 85 ms
56,168 KB
testcase_04 AC 77 ms
56,180 KB
testcase_05 AC 84 ms
56,148 KB
testcase_06 AC 84 ms
56,188 KB
testcase_07 AC 72 ms
55,588 KB
testcase_08 AC 88 ms
56,184 KB
testcase_09 AC 80 ms
56,852 KB
testcase_10 AC 80 ms
56,544 KB
testcase_11 AC 72 ms
55,876 KB
testcase_12 AC 86 ms
56,340 KB
testcase_13 AC 87 ms
56,268 KB
testcase_14 AC 88 ms
56,452 KB
testcase_15 AC 72 ms
55,868 KB
testcase_16 AC 70 ms
53,832 KB
testcase_17 AC 71 ms
55,764 KB
testcase_18 AC 71 ms
55,692 KB
testcase_19 AC 746 ms
103,920 KB
testcase_20 TLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {
	static int n;
	static List<List<Hen>> list;

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		n = Integer.parseInt(br.readLine());
		int m = 200000;
		list = new ArrayList<>(m);
		for (int i = 0; i < m; i++) {
			list.add(new ArrayList<>());
		}
		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;
			list.get(h.a).add(h);
			out[h.a]++;
			in[h.b]++;
		}
		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() == n) {
			return true;
		}
		for (Hen h : list.get(x)) {
			if (!h.used) {
				h.used = true;
				his.add(h);
				boolean res = dfs(h.b, his);
				if (res) {
					return true;
				}
				his.remove(his.size() - 1);
				h.used = false;
			}
		}
		return false;
	}

	static class Hen {
		int a, b;
		boolean used;
	}
}
0