結果
問題 |
No.1647 Travel in Mitaru city 2
|
ユーザー |
![]() |
提出日時 | 2021-08-13 22:42:36 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,543 bytes |
コンパイル時間 | 5,085 ms |
コンパイル使用メモリ | 78,024 KB |
実行使用メモリ | 102,988 KB |
最終ジャッジ日時 | 2024-10-03 20:52:28 |
合計ジャッジ時間 | 48,319 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 WA * 25 RE * 5 TLE * 1 |
ソースコード
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; } }