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)

use std::cmp::*;
use std::io::*;
use two_sat::TwoSat;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o).unwrap();
    stdout().write_all(&o).unwrap();
}

pub fn run(i: impl BufRead, o: &mut impl Write) -> std::io::Result<()> {
    let mut i = CpReader::new(i);
    let (n, d) = i.read::<(usize, usize)>();
    let xys = i.read_vec::<(usize, usize)>(n);
    let mut g = TwoSat::new(n);

    for i in 0..n {
        for j in i + 1..n {
            let xx = is_differ(xys[i].0, xys[j].0, d);
            let xy = is_differ(xys[i].0, xys[j].1, d);
            let yx = is_differ(xys[i].1, xys[j].0, d);
            let yy = is_differ(xys[i].1, xys[j].1, d);
            if !xx {
                g.add_claure(i, false, j, false);
            }
            if !xy {
                g.add_claure(i, false, j, true);
            }
            if !yx {
                g.add_claure(i, true, j, false);
            }
            if !yy {
                g.add_claure(i, true, j, true);
            }
        }
    }
    if g.satisfiable() {
        writeln!(o, "Yes")?;
        let ans = g.answer();
        for i in 0..n {
            let p = if ans[i] { xys[i].0 } else { xys[i].1 };
            writeln!(o, "{}", p)?;
        }
    } else {
        writeln!(o, "No")?;
    }
    Ok(())
}
fn is_differ(a: usize, b: usize, d: usize) -> bool {
    max(a, b) - min(a, b) >= d
}

pub mod utils {
    use super::*;

    pub struct CpReader<R: BufRead> {
        r: R,
        b: Vec<u8>,
    }
    impl<R: BufRead> CpReader<R> {
        pub fn new(r: R) -> Self {
            CpReader {
                r: r,
                b: Vec::new(),
            }
        }
        pub fn read_word(&mut self) -> &[u8] {
            self.b.clear();
            loop {
                let b = self.r.fill_buf().unwrap();
                assert!(!b.is_empty());
                if let Some(p) = b.iter().position(|&x| x == b' ' || x == b'\n') {
                    self.b.extend_from_slice(&b[..p]);
                    self.r.consume(p + 1);
                    return &self.b;
                }
                self.b.extend_from_slice(b);
                let b_len = b.len();
                self.r.consume(b_len);
            }
        }
        pub fn read_word_str(&mut self) -> &str {
            unsafe { std::str::from_utf8_unchecked(self.read_word()) }
        }

        pub fn read_line(&mut self) -> &[u8] {
            self.b.clear();
            self.r.read_until(b'\n', &mut self.b).unwrap();
            if self.b.last() == Some(&b'\n') {
                &self.b[..self.b.len() - 1]
            } else {
                &self.b
            }
        }
        pub fn read_line_str(&mut self) -> &str {
            unsafe { std::str::from_utf8_unchecked(self.read_line()) }
        }

        pub fn read<T: CpIn>(&mut self) -> T {
            T::read_from(self)
        }

        pub fn read_vec<T: CpIn>(&mut self, n: usize) -> Vec<T> {
            (0..n).map(|_| self.read()).collect()
        }
        pub fn read_iter<'a, T: CpIn>(&'a mut self, n: usize) -> CpIter<'a, R, T> {
            CpIter {
                r: self,
                n: n,
                _pd: Default::default(),
            }
        }
    }
    pub struct CpIter<'a, R: BufRead + 'a, T> {
        r: &'a mut CpReader<R>,
        n: usize,
        _pd: std::marker::PhantomData<fn() -> T>,
    }
    impl<'a, R: BufRead, T: CpIn> Iterator for CpIter<'a, R, T> {
        type Item = T;
        fn next(&mut self) -> Option<T> {
            if self.n == 0 {
                None
            } else {
                self.n -= 1;
                Some(self.r.read())
            }
        }
        fn size_hint(&self) -> (usize, Option<usize>) {
            (self.n, Some(self.n))
        }
    }
    impl<'a, R: BufRead, T: CpIn> ExactSizeIterator for CpIter<'a, R, T> {}

    pub trait CpIn {
        fn read_from<R: BufRead>(r: &mut CpReader<R>) -> Self;
    }

    impl CpIn for u64 {
        fn read_from<R: BufRead>(r: &mut CpReader<R>) -> Self {
            read_u64_fast(&mut r.r)
        }
    }
    impl CpIn for i64 {
        fn read_from<R: BufRead>(r: &mut CpReader<R>) -> Self {
            read_i64_fast(&mut r.r)
        }
    }
    impl CpIn for char {
        fn read_from<R: BufRead>(r: &mut CpReader<R>) -> Self {
            let b = r.r.fill_buf().unwrap()[0] as char;
            r.r.consume(1);
            let s = r.r.fill_buf().unwrap()[0];
            assert!(s == b' ' || s == b'\n');
            r.r.consume(1);
            b
        }
    }

    macro_rules! cpin_tuple {
        ($($t:ident),*) => {
            impl<$($t: CpIn),*> CpIn for ($($t),*) {
                fn read_from<R: BufRead>(r: &mut CpReader<R>) -> Self {
                    ($($t::read_from(r)),*)
                }
            }
        };
    }

    macro_rules! cpin_cast {
        ($t_self:ty, $t_read:ty) => {
            impl CpIn for $t_self {
                fn read_from<R: BufRead>(r: &mut CpReader<R>) -> Self {
                    <$t_read>::read_from(r) as $t_self
                }
            }
        };
    }
    macro_rules! cpin_parse {
        ($t:ty) => {
            impl CpIn for $t {
                fn read_from<R: BufRead>(r: &mut CpReader<R>) -> Self {
                    r.read_word_str().parse().unwrap()
                }
            }
        };
    }
    cpin_cast!(usize, u64);
    cpin_cast!(u32, u64);
    cpin_cast!(u16, u64);
    cpin_cast!(i32, i64);
    cpin_cast!(i16, i64);
    cpin_cast!(i8, i64);

    cpin_parse!(f64);

    cpin_tuple!(T1, T2);
    cpin_tuple!(T1, T2, T3);
    cpin_tuple!(T1, T2, T3, T4);
    cpin_tuple!(T1, T2, T3, T4, T5);

    fn read_u64_fast<R: BufRead>(r: &mut R) -> u64 {
        let mut value = 0;
        loop {
            let buf = r.fill_buf().unwrap();
            assert!(!buf.is_empty());
            let mut idx = 0;
            while let Some(&c) = buf.get(idx) {
                if b'0' <= c && c <= b'9' {
                    value = value * 10 + (c - b'0') as u64;
                    idx += 1;
                } else {
                    r.consume(idx + 1);
                    return value;
                }
            }
            r.consume(idx);
        }
    }
    fn read_i64_fast<R: BufRead>(r: &mut R) -> i64 {
        let sign = match r.fill_buf().unwrap()[0] {
            b'+' => {
                r.consume(1);
                1
            }
            b'-' => {
                r.consume(1);
                -1
            }
            _ => 1,
        };
        read_u64_fast(r) as i64 * sign
    }
}
mod scc {
    #![allow(dead_code)]
    use std::cmp::min;

    pub struct SccGraph {
        n: usize,
        edges: Vec<Edge>,
    }
    struct Edge {
        from: usize,
        to: usize,
    }

    impl SccGraph {
        pub fn new(n: usize) -> Self {
            Self {
                n,
                edges: Vec::new(),
            }
        }
        pub fn add_edge(&mut self, from: usize, to: usize) {
            self.edges.push(Edge { from, to })
        }
        pub(crate) fn scc_ids(&self) -> (usize, Vec<usize>) {
            let g = Csr::new(self.n, &self.edges);
            let mut new_ord = 0;
            let mut group_num = 0;
            let mut visited = Vec::with_capacity(self.n);
            let mut low = vec![0; self.n];
            let mut ord = vec![usize::max_value(); self.n];
            let mut ids = vec![0; self.n];

            fn dfs(
                v: usize,
                n: usize,
                g: &Csr,
                visited: &mut Vec<usize>,
                low: &mut [usize],
                ord: &mut [usize],
                ids: &mut [usize],
                now_ord: &mut usize,
                group_num: &mut usize,
            ) {
                low[v] = *now_ord;
                ord[v] = *now_ord;
                *now_ord += 1;
                visited.push(v);
                for i in g.start[v]..g.start[v + 1] {
                    let to = g.elist[i];
                    if ord[to] == usize::max_value() {
                        dfs(to, n, g, visited, low, ord, ids, now_ord, group_num);
                        low[v] = min(low[v], low[to]);
                    } else {
                        low[v] = min(low[v], ord[to]);
                    }
                }
                if low[v] == ord[v] {
                    loop {
                        let u = visited.pop().unwrap();
                        ord[u] = n;
                        ids[u] = *group_num;
                        if u == v {
                            break;
                        }
                    }
                    *group_num += 1;
                }
            }
            for i in 0..self.n {
                if ord[i] == usize::max_value() {
                    dfs(
                        i,
                        self.n,
                        &g,
                        &mut visited,
                        &mut low,
                        &mut ord,
                        &mut ids,
                        &mut new_ord,
                        &mut group_num,
                    );
                }
            }
            for x in &mut ids {
                *x = group_num - 1 - *x;
            }
            (group_num, ids)
        }
        pub fn scc(&self) -> Vec<Vec<usize>> {
            let ids = self.scc_ids();
            let group_num = ids.0;
            let mut counts = vec![0; group_num];
            for &x in &ids.1 {
                counts[x] += 1;
            }
            let mut groups = vec![Vec::new(); group_num];
            for i in 0..group_num {
                groups[i].reserve(counts[i]);
            }
            for i in 0..self.n {
                groups[(ids.1)[i]].push(i);
            }
            groups
        }
    }

    #[derive(Debug)]
    struct Csr {
        start: Vec<usize>,
        elist: Vec<usize>,
    }
    impl Csr {
        fn new(n: usize, edges: &[Edge]) -> Self {
            let mut start = vec![0; n + 1];
            let mut elist = vec![0; edges.len()];

            for e in edges {
                start[e.from + 1] += 1;
            }
            for i in 1..=n {
                start[i] += start[i - 1];
            }
            let mut counter = start.clone();
            for e in edges {
                elist[counter[e.from]] = e.to;
                counter[e.from] += 1;
            }
            Self { start, elist }
        }
    }
}
mod two_sat {

    use crate::scc::SccGraph;
    pub struct TwoSat {
        n: usize,
        answer: Vec<bool>,
        scc: SccGraph,
    }
    impl TwoSat {
        pub fn new(n: usize) -> Self {
            Self {
                n,
                answer: vec![false; n],
                scc: SccGraph::new(2 * n),
            }
        }
        pub fn add_claure(&mut self, i: usize, f: bool, j: usize, g: bool) {
            debug_assert!(i < self.n);
            debug_assert!(j < self.n);
            self.scc.add_edge(to_index(i, !f), to_index(j, g));
            self.scc.add_edge(to_index(j, !g), to_index(i, f))
        }
        pub fn satisfiable(&mut self) -> bool {
            let id = self.scc.scc_ids().1;
            for i in 0..self.n {
                if id[2 * i] == id[2 * i + 1] {
                    return false;
                }
                self.answer[i] = id[2 * i] < id[2 * i + 1];
            }
            true
        }

        pub fn answer(&self) -> &[bool] {
            &self.answer
        }
    }

    fn to_index(i: usize, f: bool) -> usize {
        2 * i + if f { 1 } else { 0 }
    }
}