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 Rust (1.42.0) (judge verdict: Accepted)

// template {{{
#![allow(clippy::many_single_char_names)]

#[allow(dead_code)]
mod ngtio {
    use ::std::collections::VecDeque;

    pub struct Buffer {
        buf: VecDeque<String>,
    }

    impl Buffer {
        pub fn new() -> Self {
            Self {
                buf: VecDeque::new(),
            }
        }

        fn load(&mut self) {
            while self.buf.is_empty() {
                let mut s = String::new();
                let length = ::std::io::stdin().read_line(&mut s).unwrap();
                if length == 0 {
                    break;
                }
                self.buf.extend(s.split_whitespace().map(|s| s.to_owned()));
            }
        }

        pub fn string(&mut self) -> String {
            self.load();
            self.buf
                .pop_front()
                .unwrap_or_else(|| panic!("入力が終了したのですが。"))
        }

        pub fn string_char_vec(&mut self) -> Vec<char> {
            let s = self.string();
            s.chars().collect::<Vec<_>>()
        }

        pub fn string_char_vec_trusted_len(&mut self, len: usize) -> Vec<char> {
            let s = self.string();
            let s = s.chars().collect::<Vec<_>>();
            assert_eq!(s.len(), len, "あら、思ったのと長さ違いますね……");
            s
        }

        pub fn char(&mut self) -> char {
            let string = self.string();
            let mut chars = string.chars();
            let res = chars.next().unwrap();
            assert!(
                chars.next().is_none(),
                "char で受け取りたいのも山々なのですが、さては 2 文字以上ありますね？"
            );
            res
        }

        pub fn read<T: ::std::str::FromStr>(&mut self) -> T
        where
            <T as ::std::str::FromStr>::Err: ::std::fmt::Debug,
        {
            self.string()
                .parse::<T>()
                .expect("Failed to parse the input.")
        }

        pub fn read_vec<T: ::std::str::FromStr>(&mut self, len: usize) -> Vec<T>
        where
            <T as ::std::str::FromStr>::Err: ::std::fmt::Debug,
        {
            (0..len).map(|_| self.read::<T>()).collect()
        }
    }

    macro_rules! define_primitive_reader {
        ($($ty:tt,)*) => {
            impl Buffer {
                $(
#[inline]
                    pub fn $ty(&mut self) -> $ty {
                        self.read::<$ty>()
                    }
                )*
            }
        }
    }

    define_primitive_reader! {
        u8, u16, u32, u64, usize,
        i8, i16, i32, i64, isize,
    }

    impl Default for Buffer {
        fn default() -> Self {
            Self::new()
        }
    }
}

#[allow(unused_imports)]
use std::{collections, iter, mem, ops};
// }}}
// scc {{{
#[allow(dead_code)]
mod scc {
    mod traversal {
        use std::mem;

        #[derive(Debug, Clone)]
        pub struct Traversal {
            pub index: Vec<usize>,
            pub time: Vec<usize>,
        }

        impl Traversal {
            pub fn pre_order(graph: &[Vec<usize>]) -> Traversal {
                fn dfs(x: usize, graph: &[Vec<usize>], res: &mut PermutationBuilder) {
                    res.visit(x);
                    for &y in &graph[x] {
                        if !res.on_stack(y) {
                            dfs(y, graph, res);
                        }
                    }
                }

                let mut res = PermutationBuilder::new(graph.len());
                for i in 0..graph.len() {
                    if !res.on_stack(i) {
                        dfs(i, &graph, &mut res);
                    }
                }
                res.build()
            }

            pub fn post_order(graph: &[Vec<usize>]) -> Traversal {
                fn dfs(
                    x: usize,
                    graph: &[Vec<usize>],
                    ckd: &mut [bool],
                    res: &mut PermutationBuilder,
                ) {
                    for &y in &graph[x] {
                        if !mem::replace(&mut ckd[y], true) {
                            dfs(y, graph, ckd, res);
                        }
                    }
                    res.visit(x);
                }

                let n = graph.len();
                let mut ckd = vec![false; n];
                let mut res = PermutationBuilder::new(graph.len());
                for i in 0..graph.len() {
                    if !mem::replace(&mut ckd[i], true) {
                        dfs(i, &graph, &mut ckd, &mut res);
                    }
                }
                res.build()
            }
        }

        #[derive(Debug, Clone)]
        struct PermutationBuilder {
            index: Vec<usize>,
            time: Vec<usize>,
        }

        impl PermutationBuilder {
            fn new(n: usize) -> Self {
                Self {
                    index: Vec::with_capacity(n),
                    time: vec![n; n],
                }
            }

            fn build(self) -> Traversal {
                Traversal {
                    index: self.index,
                    time: self.time,
                }
            }

            #[allow(dead_code)]
            fn is_empty(&self) -> bool {
                self.time.is_empty()
            }

            fn len(&self) -> usize {
                self.time.len()
            }

            fn time(&self) -> usize {
                self.index.len()
            }

            fn visit(&mut self, x: usize) {
                assert!(!self.on_stack(x));
                self.time[x] = self.time();
                self.index.push(x);
            }

            fn on_stack(&self, x: usize) -> bool {
                self.time[x] != self.len()
            }
        }
    }
    mod white_stack {
        #[derive(Debug, Clone)]
        pub struct WhiteStack {
            pub on_stack: Vec<bool>,
            pub stack: Vec<usize>,
        }

        impl WhiteStack {
            pub fn new(len: usize) -> Self {
                Self {
                    on_stack: vec![false; len],
                    stack: Vec::with_capacity(len),
                }
            }

            pub fn push(&mut self, i: usize) {
                assert!(i < self.on_stack.len());
                assert!(!self.on_stack[i]);
                self.on_stack[i] = true;
                self.stack.push(i);
            }

            pub fn pop(&mut self) -> Option<usize> {
                if let Some(i) = self.stack.pop() {
                    assert!(self.on_stack[i]);
                    Some(i)
                } else {
                    None
                }
            }

            pub fn peek(&self) -> Option<&usize> {
                self.stack.last()
            }
        }
    }

    // TODO: この2つは別ライブラリに切り出しても良いかもです。
    pub use traversal::*;
    pub use white_stack::*;

    use std::cmp;

    #[derive(Debug, Clone)]
    pub struct Scc {
        forward: Vec<Vec<usize>>, // TODO: フラットグラフも試します。
        backward: Vec<Vec<usize>>,
    }

    impl Scc {
        pub fn new(len: usize) -> Self {
            Self {
                forward: vec![Vec::new(); len],
                backward: vec![Vec::new(); len],
            }
        }
        pub fn add_edge(&mut self, u: usize, v: usize) {
            self.forward[u].push(v);
            self.backward[v].push(u);
        }
        pub fn solve(&self) -> (usize, Vec<usize>) {
            fn dfs(x: usize, g: &[Vec<usize>], res: &mut [Option<usize>]) {
                for &y in &g[x] {
                    if res[y].is_none() {
                        res[y] = res[x];
                        dfs(y, g, res);
                    }
                }
            }
            let n = self.forward.len();
            let post_backward = Traversal::post_order(&self.backward);
            let mut res = vec![None; n];
            let mut counter = 0;
            for &x in post_backward.index.iter().rev() {
                if res[x].is_none() {
                    res[x] = Some(counter);
                    dfs(x, &self.forward, &mut res);
                    counter += 1;
                }
            }
            (
                counter,
                res.iter().map(|x| counter - 1 - x.unwrap()).collect(),
            )
        }
        // TODO: 答えをいい感じにキャッシュするようにしても良いかもです。
        pub fn groups(&self) -> Vec<Vec<usize>> {
            let (count, id) = self.solve();
            let mut res = vec![Vec::new(); count];
            for (i, &x) in id.iter().enumerate() {
                res[x].push(i);
            }
            res
        }
    }

    #[derive(Debug, Clone)]
    pub struct TwoSat {
        len: usize,
        scc: Scc,
    }
    impl TwoSat {
        pub fn new(len: usize) -> Self {
            Self {
                len,
                scc: Scc::new(2 * len),
            }
        }
        pub fn add_clause(&mut self, i: usize, f: bool, j: usize, g: bool) {
            self.scc.add_edge(2 * i + !f as usize, 2 * j + g as usize);
            self.scc.add_edge(2 * j + !g as usize, 2 * i + f as usize);
        }
        pub fn solve(&mut self) -> Option<Vec<bool>> {
            self.scc
                .solve()
                .1
                .chunks_exact(2)
                .map(|v| {
                    use cmp::Ordering::*;
                    match v[0].cmp(&v[1]) {
                        Equal => None,
                        Less => Some(true),
                        Greater => Some(false),
                    }
                })
                .collect()
        }
    }
}
// }}}

fn main() {
    let mut buf = ngtio::Buffer::new();
    let n = buf.usize();
    let d = buf.i32();
    let mut two_sat = scc::TwoSat::new(n);
    let (x, y): (Vec<i32>, Vec<i32>) = iter::repeat_with(|| (buf.i32(), buf.i32())).take(n).unzip();
    for i in 0..n {
        for j in i + 1..n {
            if (x[i] - x[j]).abs() < d {
                two_sat.add_clause(i, true, j, true);
            }
            if (x[i] - y[j]).abs() < d {
                two_sat.add_clause(i, true, j, false);
            }
            if (y[i] - x[j]).abs() < d {
                two_sat.add_clause(i, false, j, true);
            }
            if (y[i] - y[j]).abs() < d {
                two_sat.add_clause(i, false, j, false);
            }
        }
    }
    if let Some(ans) = two_sat.solve() {
        println!("Yes");
        for (i, &b) in ans.iter().enumerate() {
            println!("{}", if b { y[i] } else { x[i] });
        }
    } else {
        println!("No");
    }
}