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)

macro_rules! read_line_to_tuple {
    ( $( $t:ty ),* ) => {{
        let mut input = String::new();
        std::io::stdin().read_line(&mut input).unwrap();
        let mut iter = input.split_whitespace();
        ( $( iter.next().unwrap().parse::<$t>().unwrap() ),* )
    }};
}

struct Scc {
    n: usize,
    graph: Vec<Vec<usize>>,
    rev_graph: Vec<Vec<usize>>,
    post_order: Vec<usize>,
    component: Vec<usize>,
}

impl Scc {
    fn new(n: usize) -> Scc {
        Scc {
            n,
            graph: vec![Vec::new(); n],
            rev_graph: vec![Vec::new(); n],
            component: vec![n; n],
            post_order: Vec::with_capacity(n),
        }
    }

    fn add_edge(&mut self, from: usize, to: usize) {
        self.graph[from].push(to);
        self.rev_graph[to].push(from);
    }

    fn dfs(&mut self, from: usize) {
        self.component[from] = self.n + 1;
        for to in self.graph[from].clone() {
            if self.component[to] == self.n {
                self.dfs(to);
            }
        }
        self.post_order.push(from);
    }
    
    fn rev_dfs(&mut self, from: usize, k: usize) {
        self.component[from] = k;
        for to in self.rev_graph[from].clone() {
            if self.component[to] > self.n {
                self.rev_dfs(to, k);
            }
        }
    }

    fn solve(&mut self) {
        for i in 0..self.n {
            if self.component[i] == self.n {
                self.dfs(i);
            }
        }

        let mut k: usize = 0;
        for i in (0..self.n).rev() {
            if self.component[self.post_order[i]] > self.n {
                self.rev_dfs(self.post_order[i], k);
                k += 1;
            }
        }
    }
}

struct TwoSat {
    n: usize,
    scc: Scc,
}

impl TwoSat {
    fn new(n: usize) -> TwoSat {
        TwoSat {
            n,
            scc: Scc::new(n * 2),
        }
    }

    fn add_closure(&mut self, x: usize, y: usize) {
        self.scc.add_edge((x + self.n) % (self.n * 2), y);
        self.scc.add_edge((y + self.n) % (self.n * 2), x);
    }

    fn solve(&mut self) -> Option<Vec<bool>> {
        self.scc.solve();

        let mut res = vec![false; self.n];
        for i in 0..self.n {
            if self.scc.component[i] == self.scc.component[i + self.n] {
                return None;
            }
            res[i] = self.scc.component[i] > self.scc.component[i + self.n];
        }

        Some(res)
    }
}

fn main() {
    let (n, d) = read_line_to_tuple!(usize, i64);

    let mut ts = TwoSat::new(n);
    let (mut x, mut y) = (vec![0; n], vec![0; n]);
    for i in 0..n {
        let (a, b) = read_line_to_tuple!(i64, i64);
        x[i] = a;
        y[i] = b;
    }

    for i in 0..n {
        for j in (i + 1)..n {
            if (x[i] - x[j]).abs() < d {
                ts.add_closure(i + n, j + n);
            }
            if (x[i] - y[j]).abs() < d {
                ts.add_closure(i + n, j);
            }
            if (y[i] - x[j]).abs() < d {
                ts.add_closure(i, j + n);
            }
            if (y[i] - y[j]).abs() < d {
                ts.add_closure(i, j);
            }
        }
    }

    let res = ts.solve();
    if let Some(res) = res {
        let mut ans = Vec::with_capacity(n + 1);
        ans.push("Yes".to_string());
        for i in 0..n {
            if res[i] {
                ans.push(x[i].to_string());
            } else {
                ans.push(y[i].to_string());
            }
        }
        println!("{}", ans.join("\n"));
    } else {
        println!("No");
    }
}