結果

問題 No.1647 Travel in Mitaru city 2
ユーザー ks2mks2m
提出日時 2021-08-13 23:12:04
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,034 bytes
コンパイル時間 2,783 ms
コンパイル使用メモリ 79,092 KB
実行使用メモリ 107,020 KB
最終ジャッジ日時 2024-04-14 20:18:50
合計ジャッジ時間 44,273 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
50,384 KB
testcase_01 AC 52 ms
50,328 KB
testcase_02 AC 52 ms
50,400 KB
testcase_03 AC 57 ms
50,296 KB
testcase_04 AC 108 ms
52,204 KB
testcase_05 AC 56 ms
50,552 KB
testcase_06 AC 62 ms
50,468 KB
testcase_07 AC 115 ms
52,228 KB
testcase_08 AC 893 ms
101,256 KB
testcase_09 AC 995 ms
100,124 KB
testcase_10 AC 853 ms
101,332 KB
testcase_11 AC 599 ms
79,088 KB
testcase_12 WA -
testcase_13 AC 1,015 ms
101,580 KB
testcase_14 AC 1,038 ms
102,916 KB
testcase_15 AC 1,097 ms
103,712 KB
testcase_16 AC 862 ms
99,508 KB
testcase_17 AC 882 ms
102,356 KB
testcase_18 AC 773 ms
92,788 KB
testcase_19 AC 828 ms
97,736 KB
testcase_20 AC 809 ms
98,324 KB
testcase_21 AC 898 ms
98,716 KB
testcase_22 AC 884 ms
97,448 KB
testcase_23 AC 892 ms
98,612 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 863 ms
97,924 KB
testcase_27 AC 1,025 ms
103,184 KB
testcase_28 AC 1,118 ms
105,524 KB
testcase_29 AC 935 ms
101,656 KB
testcase_30 AC 971 ms
101,400 KB
testcase_31 AC 908 ms
103,232 KB
testcase_32 AC 923 ms
98,108 KB
testcase_33 AC 775 ms
92,720 KB
testcase_34 AC 911 ms
101,148 KB
testcase_35 AC 1,021 ms
100,772 KB
testcase_36 AC 915 ms
101,580 KB
testcase_37 AC 1,051 ms
107,020 KB
testcase_38 AC 841 ms
95,132 KB
testcase_39 WA -
testcase_40 AC 817 ms
95,268 KB
testcase_41 AC 886 ms
93,784 KB
testcase_42 AC 893 ms
92,092 KB
testcase_43 AC 54 ms
50,428 KB
testcase_44 AC 82 ms
56,192 KB
testcase_45 AC 640 ms
82,920 KB
testcase_46 AC 710 ms
79,608 KB
testcase_47 AC 506 ms
77,060 KB
testcase_48 AC 641 ms
79,108 KB
testcase_49 AC 633 ms
82,584 KB
testcase_50 AC 652 ms
82,872 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();
					boolean flg = false;
					if (o.x == arr[s + 1].x) {
						s++;
						flg = true;
					}
					for (int i = s; i < arr.length; i++) {
						sb.append(arr[i].i + 1).append(' ');
					}
					if (flg) {
						sb.append(o.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