Problem:
Score : 100 points

Problem Statement

Consider placing N flags on a line. Flags are numbered through 1 to N.

Flag i can be placed on the coordinate X_i or Y_i.
For any two different flags, the distance between them should be at least D.

Decide whether it is possible to place all N flags. If it is possible, print such a configulation.

Constraints

1 \leq N \leq 1000

0 \leq D \leq 10^9

0 \leq X_i < Y_i \leq 10^9

All values in Input are integer.

Input

Input is given from Standard Input in the following format:

N D
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Print No if it is impossible to place N flags.

If it is possible, print Yes first.
After that, print N lines. i-th line of them should contain the coodinate of flag i.

Sample Input 1

3 2
1 4
2 5
0 6

Sample Output 1

Yes
4
2
0

Sample Input 2

3 3
1 4
2 5
0 6

Sample Output 2

No

Solution in Ruby (2.7.1) (judge verdict: Accepted)

# frozen_string_literal: true

# https://github.com/universato/ac-library-rb/blob/master/src/scc.rb
class SCCGraph
  # initialize graph with n vertices
  def initialize(n = 0)
    @n, @edges = n, []
  end

  # add directed edge
  def add_edge(from, to)
    raise "invalid params" unless (0...@n).include? from and (0...@n).include? to

    @edges << [from, to]
  end

  # returns list of strongly connected components
  # the components are sorted in topological order
  # O(@n + @edges.size)
  def scc
    group_num, ids = scc_ids
    counts = [0] * group_num
    ids.each { |x| counts[x] += 1 }
    groups = Array.new(group_num) { [] }
    ids.each_with_index { |x, i| groups[x] << i }
    groups
  end

  private

  def scc_ids
    start, elist = csr
    now_ord = group_num = 0
    visited, low, ord, ids = [], [], [-1] * @n, []
    dfs = ->(v) {
      low[v] = ord[v] = now_ord
      now_ord += 1
      visited << v
      (start[v]...start[v + 1]).each do |i|
        to = elist[i]
        low[v] = if ord[to] == -1
                   dfs.(to)
                   [low[v], low[to]].min
                 else
                   [low[v], ord[to]].min
                 end
      end
      if low[v] == ord[v]
        loop do
          u = visited.pop
          ord[u] = @n
          ids[u] = group_num
          break if u == v
        end
        group_num += 1
      end
    }
    @n.times { |i| dfs.(i) if ord[i] == -1 }
    ids = ids.map { |x| group_num - 1 - x }
    [group_num, ids]
  end

  def csr
    start, elist = [0] * (@n + 1), [nil] * @edges.size
    @edges.each { |(i, _)| start[i + 1] += 1 }
    @n.times { |i| start[i + 1] += start[i] }
    counter = start.dup
    @edges.each do |(i, j)|
      elist[counter[i]] = j
      counter[i] += 1
    end
    [start, elist]
  end
end

# TwoSat
# Reference: https://github.com/atcoder/ac-library/blob/master/atcoder/twosat.hpp
class TwoSat
  def initialize(n = 0)
    @n = n
    @answer = Array.new(n)
    @scc = SCCGraph.new(2 * n)
  end

  attr_reader :answer

  def add_clause(i, f, j, g)
    raise RangeError unless (0...@n).cover?(i) && (0...@n).cover?(j)

    @scc.add_edge(2 * i + (f ? 0 : 1), 2 * j + (g ? 1 : 0))
    @scc.add_edge(2 * j + (g ? 0 : 1), 2 * i + (f ? 1 : 0))
    nil
  end

  def satisfiable
    id = @scc.send(:scc_ids)[1]
    @n.times do |i|
      return false if id[2 * i] == id[2 * i + 1]

      @answer[i] = id[2 * i] < id[2 * i + 1]
    end
    true
  end
end


n, d = gets.split.map(&:to_i)
x, y = n.times.map { gets.split.map(&:to_i) }.transpose
ts = TwoSat.new(n)

n.times do |i|
  (i + 1...n).each do |j|
    ts.add_clause(i, false, j, false) if (x[i] - x[j]).abs < d
    ts.add_clause(i, false, j, true) if (x[i] - y[j]).abs < d
    ts.add_clause(i, true, j, false) if (y[i] - x[j]).abs < d
    ts.add_clause(i, true, j, true) if (y[i] - y[j]).abs < d
  end
end

if ts.satisfiable
  puts 'Yes'
  ts.answer.each_with_index do |ans, i|
    puts ans ? x[i] : y[i]
  end
else
  puts 'No'
end