結果

問題 No.1647 Travel in Mitaru city 2
ユーザー yuruhiyayuruhiya
提出日時 2021-08-19 15:56:37
言語 Crystal
(1.11.2)
結果
AC  
実行時間 104 ms / 2,500 ms
コード長 850 bytes
コンパイル時間 11,383 ms
コンパイル使用メモリ 295,044 KB
実行使用メモリ 33,740 KB
最終ジャッジ日時 2024-04-21 05:05:03
合計ジャッジ時間 18,009 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 3 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 4 ms
6,940 KB
testcase_08 AC 89 ms
30,460 KB
testcase_09 AC 81 ms
29,752 KB
testcase_10 AC 85 ms
30,544 KB
testcase_11 AC 70 ms
27,160 KB
testcase_12 AC 81 ms
25,764 KB
testcase_13 AC 75 ms
25,940 KB
testcase_14 AC 92 ms
31,724 KB
testcase_15 AC 85 ms
30,132 KB
testcase_16 AC 84 ms
29,704 KB
testcase_17 AC 79 ms
25,836 KB
testcase_18 AC 79 ms
26,592 KB
testcase_19 AC 87 ms
31,620 KB
testcase_20 AC 79 ms
30,336 KB
testcase_21 AC 90 ms
32,396 KB
testcase_22 AC 95 ms
32,856 KB
testcase_23 AC 89 ms
32,516 KB
testcase_24 AC 90 ms
29,148 KB
testcase_25 AC 89 ms
31,032 KB
testcase_26 AC 90 ms
32,588 KB
testcase_27 AC 96 ms
32,944 KB
testcase_28 AC 96 ms
33,228 KB
testcase_29 AC 104 ms
33,132 KB
testcase_30 AC 91 ms
31,668 KB
testcase_31 AC 95 ms
33,700 KB
testcase_32 AC 94 ms
31,080 KB
testcase_33 AC 84 ms
31,580 KB
testcase_34 AC 90 ms
33,264 KB
testcase_35 AC 87 ms
30,964 KB
testcase_36 AC 96 ms
33,740 KB
testcase_37 AC 89 ms
33,016 KB
testcase_38 AC 86 ms
30,956 KB
testcase_39 AC 82 ms
28,548 KB
testcase_40 AC 103 ms
29,240 KB
testcase_41 AC 85 ms
27,988 KB
testcase_42 AC 86 ms
29,124 KB
testcase_43 AC 2 ms
6,944 KB
testcase_44 AC 15 ms
14,400 KB
testcase_45 AC 80 ms
25,860 KB
testcase_46 AC 89 ms
25,184 KB
testcase_47 AC 71 ms
27,320 KB
testcase_48 AC 79 ms
26,156 KB
testcase_49 AC 74 ms
27,040 KB
testcase_50 AC 69 ms
26,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Solve
  def initialize
    h, w, n = read_line.split.map(&.to_i)
    @xy = Array(Array(Int32)).new(n) { read_line.split.map(&.to_i.pred) }
    @graph = Array(Array({Int32, Int32})).new(h + w) { [] of {Int32, Int32} }
    @xy.each_with_index do |(x, y), i|
      @graph[x] << {h + y, i}
      @graph[h + y] << {x, i}
    end
    @cycle = [] of Int32
    @index = Array(Int32?).new(h + w, nil)
  end

  def dfs(v, p)
    if i = @index[v]
      ans = @cycle[i..]
      ans.rotate! if @xy[ans[0]][1] != @xy[ans[1]][1]
      puts ans.size, ans.join(' ', &.succ)
      exit
    end
    @index[v] = @cycle.size
    @graph[v].each do |u, i|
      next if u == p
      @cycle.push i
      dfs(u, v)
      @cycle.pop
    end
  end

  def solve
    @graph.size.times do |i|
      dfs(i, p) if @index[i].nil?
    end
    puts -1
  end
end

Solve.new.solve
0