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