結果

問題 No.1647 Travel in Mitaru city 2
ユーザー ks2mks2m
提出日時 2021-08-13 22:58:15
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,879 bytes
コンパイル時間 2,848 ms
コンパイル使用メモリ 79,204 KB
実行使用メモリ 104,012 KB
最終ジャッジ日時 2024-04-14 19:53:00
合計ジャッジ時間 43,658 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
50,004 KB
testcase_01 AC 52 ms
50,372 KB
testcase_02 AC 51 ms
50,284 KB
testcase_03 WA -
testcase_04 AC 109 ms
52,096 KB
testcase_05 WA -
testcase_06 AC 58 ms
50,532 KB
testcase_07 WA -
testcase_08 AC 1,000 ms
103,500 KB
testcase_09 AC 823 ms
94,996 KB
testcase_10 AC 941 ms
97,296 KB
testcase_11 AC 656 ms
80,188 KB
testcase_12 WA -
testcase_13 AC 970 ms
94,660 KB
testcase_14 AC 854 ms
97,236 KB
testcase_15 AC 1,053 ms
102,456 KB
testcase_16 AC 1,021 ms
102,280 KB
testcase_17 AC 870 ms
96,312 KB
testcase_18 AC 913 ms
89,856 KB
testcase_19 AC 1,021 ms
100,116 KB
testcase_20 AC 960 ms
98,476 KB
testcase_21 AC 1,030 ms
100,156 KB
testcase_22 AC 1,092 ms
100,760 KB
testcase_23 AC 1,009 ms
101,880 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 1,015 ms
102,840 KB
testcase_27 AC 911 ms
98,220 KB
testcase_28 AC 1,067 ms
99,160 KB
testcase_29 AC 1,061 ms
101,292 KB
testcase_30 AC 983 ms
97,956 KB
testcase_31 AC 1,024 ms
104,012 KB
testcase_32 AC 983 ms
97,728 KB
testcase_33 AC 780 ms
92,160 KB
testcase_34 AC 1,070 ms
103,092 KB
testcase_35 AC 848 ms
96,716 KB
testcase_36 AC 955 ms
100,264 KB
testcase_37 AC 890 ms
97,920 KB
testcase_38 AC 826 ms
93,756 KB
testcase_39 WA -
testcase_40 AC 942 ms
95,784 KB
testcase_41 AC 775 ms
89,884 KB
testcase_42 AC 1,009 ms
94,744 KB
testcase_43 AC 55 ms
50,332 KB
testcase_44 AC 82 ms
56,248 KB
testcase_45 AC 655 ms
76,632 KB
testcase_46 AC 871 ms
80,560 KB
testcase_47 AC 519 ms
77,288 KB
testcase_48 AC 758 ms
82,776 KB
testcase_49 AC 508 ms
77,164 KB
testcase_50 AC 520 ms
77,092 KB
権限があれば一括ダウンロードができます

ソースコード

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)) {
					Hen[] arr = his.toArray(new Hen[0]);
					int s = 0;
					for (int i = 0; i < arr.length; i++) {
						if (arr[i] == o) {
							s = i;
							break;
						}
					}
					System.out.println(arr.length - s);
					StringBuilder sb = new StringBuilder();
					for (int i = s; i < arr.length; i++) {
						sb.append(arr[i].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