N, M = gets.split.take(2).map(&:to_i) AB = $<.map{|s| s.split.take(2).map(&:to_i).map(&:pred) } numbers = [1] * N team = [*0...N] f = -> a,b{ to = team[a] from = team[b] team.map!{|t| t == from ? to : t } sum = numbers[to] + numbers[from] numbers[to] = sum numbers[from] = sum } AB.each{|a, b| next if team[a] == team[b] if numbers[a] > numbers[b] f[a, b] elsif numbers[b] > numbers[a] f[b, a] elsif team[a] < team[b] f[a, b] else f[b, a] end # p numbers # p team } puts team.map(&:succ)