line = gets.chomp! abort "format error" unless line=~/^\d+ \d+$/ abort "end of line error" unless !gets n,m = line.split.map(&:to_i) abort "3 <= N <= 10^5" unless 3 <= n && n <= 100000 abort "3 <= M <= 10^5" unless 2 <= m && m <= 100000 def connect(*arg) puts arg*" " end na = n/2 mbest = na*(n-na) unless n-1 <= m && m <= mbest puts "NO" else puts "YES" puts (1..n).to_a*" " # 連結になるように辺を加える connect(1, na+1) 2.upto(na){|i| connect(i, na+i-1) connect(i, na+i) } (na*2+1).upto(n){|i| connect(na, i) } m -= n-1 # 残りの辺を加える ai = 1 bi = 1 m.times{ # インクリメント bi += 1 if (bi > n-na) ai += 1 bi = 1 end # 既に辺があるなら加えない redo if (ai == bi || ai == bi + 1) connect(ai,na + bi) } end