結果

問題 No.1647 Travel in Mitaru city 2
ユーザー ks2mks2m
提出日時 2021-08-13 22:52:17
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,654 bytes
コンパイル時間 2,357 ms
コンパイル使用メモリ 78,772 KB
実行使用メモリ 102,412 KB
最終ジャッジ日時 2024-04-14 19:37:47
合計ジャッジ時間 46,818 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
57,112 KB
testcase_01 AC 53 ms
50,000 KB
testcase_02 AC 55 ms
50,044 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 1,042 ms
97,592 KB
testcase_09 AC 1,061 ms
97,368 KB
testcase_10 AC 958 ms
96,116 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 909 ms
97,576 KB
testcase_15 AC 1,078 ms
100,332 KB
testcase_16 AC 1,029 ms
100,264 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 1,118 ms
100,784 KB
testcase_23 AC 1,017 ms
101,740 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 1,082 ms
101,248 KB
testcase_27 AC 1,045 ms
95,204 KB
testcase_28 AC 1,096 ms
102,412 KB
testcase_29 AC 1,021 ms
102,320 KB
testcase_30 AC 864 ms
95,132 KB
testcase_31 AC 935 ms
98,636 KB
testcase_32 AC 1,072 ms
97,328 KB
testcase_33 WA -
testcase_34 AC 1,091 ms
101,328 KB
testcase_35 AC 980 ms
98,276 KB
testcase_36 AC 1,073 ms
97,832 KB
testcase_37 AC 1,122 ms
102,388 KB
testcase_38 AC 853 ms
92,764 KB
testcase_39 WA -
testcase_40 AC 831 ms
90,836 KB
testcase_41 WA -
testcase_42 AC 1,028 ms
95,992 KB
testcase_43 AC 54 ms
50,256 KB
testcase_44 AC 83 ms
56,312 KB
testcase_45 WA -
testcase_46 TLE -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	static int h, w, n, hw;
	static List<List<Hen>> list;

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		h = Integer.parseInt(sa[0]);
		w = Integer.parseInt(sa[1]);
		n = Integer.parseInt(sa[2]);
		hw = h + w;
		list = new ArrayList<>(hw);
		for (int i = 0; i < hw; i++) {
			list.add(new ArrayList<>());
		}
		Hen[] arr = new Hen[n];
		for (int i = 0; i < n; i++) {
			sa = br.readLine().split(" ");
			Hen o = new Hen();
			o.i = i;
			o.x = Integer.parseInt(sa[0]) - 1;
			o.y = Integer.parseInt(sa[1]) - 1 + h;
			list.get(o.x).add(o);
			list.get(o.y).add(o);
			arr[i] = o;
		}
		br.close();

		for (int i = 0; i < n; i++) {
			if (dfs(arr[i].x, null, new LinkedHashSet<>())) {
				return;
			}
		}
		System.out.println(-1);
	}

	static boolean dfs(int x, Hen p, LinkedHashSet<Hen> his) {
		for (Hen o : list.get(x)) {
			if (o != p) {
				if (his.contains(o)) {
					System.out.println(his.size());
					StringBuilder sb = new StringBuilder();
					for (Hen o2 : his) {
						sb.append(o2.i + 1).append(' ');
					}
					sb.deleteCharAt(sb.length() - 1);
					System.out.println(sb.toString());
					return true;
				}
				his.add(o);
				int nx = o.x;
				if (nx == x) {
					nx = o.y;
				}
				if (dfs(nx, o, his)) {
					return true;
				}
				his.remove(o);
			}
		}
		return false;
	}

	static class Hen {
		int x, y, i;
		boolean used;
	}
}
0