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)

// https://atcoder.jp/contests/practice2/tasks/practice2_h
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

// Input macros.
// Original by tanakh: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! read_line {
    ($t:tt) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            s.trim_right().parse::<$t>().unwrap()
        }
    }
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

// ===

type DirectedGraph = Vec<Vec<usize>>;

struct StronglyConnectedComponent {
    n: usize,
    graph: DirectedGraph,
    reversed_graph: DirectedGraph,
    component_ids: Vec<usize>
}

impl StronglyConnectedComponent {
    fn new(g: DirectedGraph) -> Self {
        let n = g.len();
        let mut rg = vec![vec![]; n];
        for i in 0..n {
            for &to in &g[i] {
                rg[to].push(i);
            }
        }
        Self {
            n: g.len(),
            graph: g,
            reversed_graph: rg,
            component_ids: vec![]
        }
    }

    fn add_edge(&mut self, i: usize, j: usize) {
        self.graph[i].push(j);
        self.reversed_graph[j].push(i);
    }

    fn decompose(&mut self) -> usize {
        let mut visited = vec![false; self.n];
        let mut rev_order = vec![];
        for i in 0..self.n {
            if !visited[i] {
                self.dfs(i, &mut visited, &mut rev_order);
            }
        }

        let mut visited = vec![false; self.n];
        let mut id = 0;
        let mut component_ids = vec![0; self.n];
        for &r in rev_order.iter().rev() {
            if !visited[r] {
                self.rdfs(r, id, &mut visited, &mut component_ids);
                id += 1;
            }
        }
        self.component_ids = component_ids;
        id
    }

    fn dfs(&self, now: usize, visited: &mut Vec<bool>, rev_order: &mut Vec<usize>) {
        visited[now] = true;
        for &to in &self.graph[now] {
            if !visited[to] {
                self.dfs(to, visited, rev_order);
            }
        }
        rev_order.push(now);
    }

    fn rdfs(&self, now: usize, id: usize, visited: &mut Vec<bool>, component_ids: &mut Vec<usize>) {
        visited[now] = true;
        component_ids[now] = id;
        for &to in &self.reversed_graph[now] {
            if !visited[to] {
                self.rdfs(to, id, visited, component_ids);
            }
        }
    }
}


struct TwoSat {
    n: usize,
    scc: StronglyConnectedComponent
}

impl TwoSat {
    fn new(n: usize) -> Self {
        Self {
            n,
            scc: StronglyConnectedComponent::new(vec![vec![]; 2*n])
        }
    }

    /// Adds clause (a = f) v (b = g).
    fn add_clause(&mut self, a: usize, f: bool, b: usize, g: bool) {
        let f = ifv!(f, 0, 1);
        let g = ifv!(g, 0, 1);
        self.scc.add_edge(2 * a + f, 2 * b + g ^ 1);
        self.scc.add_edge(2 * b + g, 2 * a + f ^ 1);
    }

    fn compute(&mut self) -> Option<Vec<bool>> {
        self.scc.decompose();
        let component_ids = &self.scc.component_ids;
        let mut ans = vec![];
        for i in 0..self.n {
            if component_ids[i*2] == component_ids[i*2+1] {
                return None;
            }
            ans.push(component_ids[i*2] < component_ids[i*2+1]);
        }
        Some(ans)
    }
}

fn main() {
    input! {
        n: usize, d: i64,
        choose: [[i64; 2]; n]
    };

    let mut twosat = TwoSat::new(n);
    for i in 0..n {
        for j in i+1..n {
            for k in 0..2 {
                for l in 0..2 {
                    if (choose[i][k] - choose[j][l]).abs() < d {
                        twosat.add_clause(i, k == 1, j, l == 1);
                    }
                }
            }

        }
    }
    if let Some(v) = twosat.compute() {
        println!("Yes");
        for i in 0..n {
            println!("{}", ifv!(v[i], choose[i][0], choose[i][1]));
        }
    } else {
        println!("No");
    }
}