N = gets.to_i O = Hash.new { |h, k| h[k] = {} } E = Array.new(N + 1) { [] } OE = [] N.times do a, b = gets.split.map(&:to_i) E[a] << b E[b] << a OE << [a, b] end def find_loop(v, par, path, visited) if visited[v] from = path.index(v) return [path[from..], true] end visited[v] = true path << v E[v].each do |u| next if u == par res, found = find_loop(u, v, path, visited) if found return [res, found] end end path.pop end path = [] visited = Array.new(N + 1, false) loop_path, f = find_loop(1, nil, path, visited) queue = [] visited = Array.new(N + 1, false) n = loop_path.size n.times do |i| a = loop_path[i] b = loop_path[(i + 1) % n] queue << a visited[a] = true O[a][b] = 1 end until queue.empty? v = queue.shift E[v].each do |u| next if visited[u] visited[u] = true O[v][u] = 1 queue << u end end 1.upto(N) do |n| next if visited[n] queue << n until queue.empty? v = queue.shift E[v].each do |u| next if visited[u] visited[u] = true O[v][u] = 1 end end end counter = Hash.new(0) OE.each do |a, b| if O[a][b] counter[b] += 1 puts '<-' else counter[a] += 1 puts '->' end end