# Here your code ! G = [] Vertex = Struct.new(:value,:edges) def G.add_vert(value) G.push(Vertex.new(value,[])) G.size - 1 end def G.add_edge(u,v) G[u].edges.push(v) G[v].edges.push(u) end def G.matching G.size.times.each_with_object({}) do |v,matched| find_path(v,matched) unless matched[v] end end def G.find_path(v,matched,visited = {}) visited[v] = true self[v].edges.find do |u| w = matched[u] if !w || (!visited[w] && find_path(w,matched,visited)) matched[v] = u matched[u] = v end end end n = gets.to_i U = n.times.map {|u| G.add_vert(u)} V = n.times.map {|v| G.add_vert(v)} A = n.times.map { gets.to_i } n.times do |u| n.times do |v| G.add_edge(U[u],V[v]) unless v == A[u] end end m = G.matching if m.size == n * 2 puts U.map {|u| m[u]}.map {|v| G[v].value } else puts -1 end