結果

問題 No.1647 Travel in Mitaru city 2
ユーザー ks2mks2m
提出日時 2021-08-13 22:42:36
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,543 bytes
コンパイル時間 5,175 ms
コンパイル使用メモリ 78,252 KB
実行使用メモリ 103,060 KB
最終ジャッジ日時 2024-04-14 19:13:55
合計ジャッジ時間 44,985 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
50,520 KB
testcase_01 AC 54 ms
50,388 KB
testcase_02 AC 53 ms
50,464 KB
testcase_03 AC 58 ms
50,528 KB
testcase_04 WA -
testcase_05 AC 57 ms
50,448 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 RE -
testcase_09 WA -
testcase_10 AC 1,031 ms
99,052 KB
testcase_11 RE -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 RE -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 978 ms
98,752 KB
testcase_23 AC 919 ms
95,844 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 988 ms
101,264 KB
testcase_27 AC 947 ms
96,884 KB
testcase_28 AC 1,026 ms
97,652 KB
testcase_29 AC 1,022 ms
101,732 KB
testcase_30 WA -
testcase_31 AC 920 ms
99,000 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 871 ms
97,780 KB
testcase_35 AC 968 ms
98,312 KB
testcase_36 AC 1,031 ms
101,124 KB
testcase_37 AC 924 ms
99,104 KB
testcase_38 AC 876 ms
92,460 KB
testcase_39 WA -
testcase_40 AC 777 ms
89,832 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 RE -
testcase_44 RE -
testcase_45 WA -
testcase_46 TLE -
testcase_47 WA -
testcase_48 WA -
testcase_49 AC 532 ms
74,800 KB
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 n, n2;
	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(" ");
		n = Integer.parseInt(sa[2]);
		n2 = n * 2;
		list = new ArrayList<>(n2);
		for (int i = 0; i < n2; i++) {
			list.add(new ArrayList<>());
		}
		for (int i = 0; i < n; i++) {
			sa = br.readLine().split(" ");
			Hen h = new Hen();
			h.i = i;
			h.x = Integer.parseInt(sa[0]) - 1;
			h.y = Integer.parseInt(sa[1]) - 1 + n;
			list.get(h.x).add(h);
			list.get(h.y).add(h);
		}
		br.close();

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

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

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