結果

問題 No.1254 補強への架け橋
ユーザー yuruhiya
提出日時 2020-10-11 12:42:58
言語 Crystal
(1.14.0)
結果
RE  
実行時間 -
コード長 1,038 bytes
コンパイル時間 12,364 ms
コンパイル使用メモリ 297,788 KB
実行使用メモリ 28,928 KB
最終ジャッジ日時 2024-06-30 21:19:20
合計ジャッジ時間 23,501 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 15 RE * 108
権限があれば一括ダウンロードができます

ソースコード

diff #

lib C
  fun strtoll(s : UInt8*, p : UInt8**, b : Int32) : Int64
end

class String
  def to_i64
    C.strtoll(self, nil, 10)
  end
end

class Solve
  @n : Int32
  @g = Array(Array(Tuple(Int32, Int32))).new
  @edges = [] of Tuple(Int32, Int32)
  @in_loop = -1

  def initialize
    @n = read_line.to_i
    @g = (1..@n).map { Array(Tuple(Int32, Int32)).new }
    @n.times do |i|
      a, b = read_line.split.map(&.to_i.pred)
      @g[a] << {b, i}
      @g[b] << {a, i}
      @edges << {a, b}
    end
    @flag = Array(Int32?).new(@n, nil)
  end

  def dfs(v, prev)
    @g[v].each do |(u, i)|
      next if u == prev
      if @flag[u]
        @in_loop = u
        return
      end
      @flag[u] = i
      dfs(u, v)
    end
  end

  def solve
    dfs(0, -1)
    ans = [] of Int32
    now = @in_loop
    while now != @in_loop || ans.empty?
      edge_i = @flag[now].not_nil!
      ans << edge_i + 1
      u, v = @edges[edge_i]
      now = u == now ? v : u
    end
    puts ans.size, ans.sort.join(' ')
  end
end

solve = Solve.new
solve.solve
0