結果

問題 No.1647 Travel in Mitaru city 2
ユーザー ks2mks2m
提出日時 2021-08-13 22:55:03
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,683 bytes
コンパイル時間 2,650 ms
コンパイル使用メモリ 79,052 KB
実行使用メモリ 102,532 KB
最終ジャッジ日時 2024-04-14 19:47:02
合計ジャッジ時間 45,671 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
50,472 KB
testcase_01 AC 56 ms
50,184 KB
testcase_02 AC 54 ms
50,448 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 1,065 ms
94,904 KB
testcase_09 AC 955 ms
97,144 KB
testcase_10 AC 1,017 ms
99,484 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 963 ms
99,428 KB
testcase_15 AC 952 ms
100,876 KB
testcase_16 AC 936 ms
99,892 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 957 ms
95,516 KB
testcase_23 AC 823 ms
99,120 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 1,011 ms
102,532 KB
testcase_27 AC 1,069 ms
101,284 KB
testcase_28 AC 1,011 ms
96,780 KB
testcase_29 AC 864 ms
99,760 KB
testcase_30 AC 905 ms
96,804 KB
testcase_31 AC 867 ms
98,976 KB
testcase_32 AC 971 ms
92,792 KB
testcase_33 WA -
testcase_34 AC 989 ms
97,164 KB
testcase_35 AC 791 ms
95,000 KB
testcase_36 AC 1,000 ms
99,724 KB
testcase_37 AC 1,044 ms
101,548 KB
testcase_38 AC 781 ms
91,744 KB
testcase_39 WA -
testcase_40 AC 954 ms
92,284 KB
testcase_41 WA -
testcase_42 AC 798 ms
92,644 KB
testcase_43 AC 55 ms
50,008 KB
testcase_44 AC 94 ms
56,200 KB
testcase_45 WA -
testcase_46 AC 672 ms
79,480 KB
testcase_47 WA -
testcase_48 AC 619 ms
78,768 KB
testcase_49 WA -
testcase_50 WA -
権限があれば一括ダウンロードができます

ソースコード

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 (Hen o : arr) {
			if (!o.used) {
				if (dfs(o.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)) {
			o.used = true;
			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