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: Wrong Answer)

#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<i64>();
    let (n, d) = (v[0] as usize, v[1]);
    let mut cands = vec![];
    for i in 0..n {
        let v = read_vec::<i64>();
        let (x, y) = (v[0], v[1]);
        cands.push((x, y));
    }
    // [0, n): x (not y)
    // [n, 2 * n): y (not x)
    let mut g = vec![vec![]; 2 * n];
    let mut rg = vec![vec![]; 2 * n];
    for i in 0..n {
        let (xi, yi) = cands[i];
        for j in i + 1..n {
            let (xj, yj) = cands[j];

            if (xi - xj).abs() < d {
                add_edge(2 * i, 2 * j + 1, &mut g, &mut rg);
                add_edge(2 * j, 2 * i + 1, &mut g, &mut rg);
            }
            if (xi - yj).abs() < d {
                add_edge(2 * i, 2 * j, &mut g, &mut rg);
                add_edge(2 * j + 1, 2 * i + 1, &mut g, &mut rg);
            }
            if (yi - xj).abs() < d {
                add_edge(2 * i + 1, 2 * j + 1, &mut g, &mut rg);
                add_edge(2 * j, 2 * i, &mut g, &mut rg);
            }
            if (yi - yj).abs() < d {
                add_edge(2 * i + 1, 2 * j, &mut g, &mut rg);
                add_edge(2 * j + 1, i, &mut g, &mut rg);
            }
        }
    }
    let cmp = scc(&g, &rg);
    if (0..n).any(|i| cmp[2 * i] == cmp[2 * i + 1]) {
        println!("No");
        return;
    }
    println!("Yes");
    for i in 0..n {
        if cmp[2 * i] < cmp[2 * i + 1] {
            println!("{}", cands[i].1);
        } else {
            println!("{}", cands[i].0);
        }
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

type Graph = Vec<Vec<usize>>;

fn dfs(v: usize, used: &mut Vec<bool>, g: &Graph, vs: &mut Vec<usize>) {
    used[v] = true;
    for &to in g[v].iter() {
        if !used[to] {
            dfs(to, used, g, vs);
        }
    }
    vs.push(v);
}

fn rdfs(v: usize, k: usize, used: &mut Vec<bool>, rg: &Graph, cmp: &mut Vec<usize>) {
    used[v] = true;
    cmp[v] = k;
    for &to in rg[v].iter() {
        if !used[to] {
            rdfs(to, k, used, rg, cmp);
        }
    }
}

fn add_edge(from: usize, to: usize, g: &mut Graph, rg: &mut Graph) {
    g[from].push(to);
    rg[to].push(from);
}

fn scc(g: &Graph, rg: &Graph) -> Vec<usize> {
    let mut used = vec![false; g.len()];
    let mut vs = vec![];
    for v in 0..g.len() {
        if !used[v] {
            dfs(v, &mut used, &g, &mut vs);
        }
    }
    let mut used = vec![false; g.len()];
    let mut cmp = vec![0usize; g.len()];
    let mut k = 0;
    for &i in vs.iter().rev() {
        if !used[i] {
            rdfs(i, k, &mut used, &rg, &mut cmp);
            k += 1;
        }
    }
    cmp
}