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 C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	const double eps=1e-11;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		int n=s[0];
		var h=new int[n][];
		var sat=new Tsat(n);
		for(int i = 0;i<n;i++) {
			h[i]=sc.Ia;
			for(int j = i-1;j>=0;j--) {
				if(Abs(h[i][0]-h[j][0])<s[1]){
					sat.Edge(i,0,j,1);
					sat.Edge(j,0,i,1);
				}
				if(Abs(h[i][0]-h[j][1])<s[1]){
					sat.Edge(i,0,j,0);
					sat.Edge(j,1,i,1);
				}
				if(Abs(h[i][1]-h[j][0])<s[1]){
					sat.Edge(i,1,j,1);
					sat.Edge(j,0,i,0);
				}
				if(Abs(h[i][1]-h[j][1])<s[1]){
					sat.Edge(i,1,j,0);
					sat.Edge(j,1,i,0);
				}
			}
		}
		sat.Scc();
		StringBuilder sb=new StringBuilder();
		for(int i = 0;i<n;i++) {
			var e=sat.Ck(i);
			if(e==-1){
				Console.WriteLine("No");
				return;
			}
			else{sb.Append(h[i][e]+"\n");}
		}
		Console.WriteLine("Yes");
		Console.Write(sb);
	}
}
public class Tsat{
	public List<int>[] li,lim;
	public int[] h;
	private int n;
	public Tsat(int n){
		this.n=n<<1;
		lim=new List<int>[this.n];
		for(int i=0;i<this.n;i++){lim[i]=new List<int>();}
	}
	public void Edge(int a,int b){
		lim[a].Add(b);
	}
	public void Edge(int a,int c,int b,int d){
		a=(a<<1)+c;b=(b<<1)+d;
		lim[a].Add(b);
	}
	public int Ck(int a){
		a<<=1;
		return h[a]==h[a+1]?-1:(h[a]<h[a+1]?0:1);
	}
	public void Scc(){
		int n=lim.Length,p=1;
		var b=new bool[n];
		var b2=new bool[n];
		var b3=new bool[n];
		var low=new int[n];
		h=new int[n];
		var sk=new Stack<int>();
		var lid=new List<List<int>>();
		for(int i = 0;i<n;i++) {if(!b[i]){Fud(i);}}
		void Fud(int a){
			b[a]=true;
			low[a]=p++;
			sk.Push(a);
			for(int i=0;i<lim[a].Count;i++){
				if(b2[lim[a][i]]){continue;}
				if(!b[lim[a][i]]){Fud(lim[a][i]);}
				if(low[a]>low[lim[a][i]]){
					low[a]=low[lim[a][i]];
					b3[a]=true;
				}
			}
			if(b3[a]){return;}
			int v=-1;
			lid.Add(new List<int>());
			while(v!=a){
				v=sk.Pop();
				lid[lid.Count-1].Add(v);
				b2[v]=true;
				h[v]=lid.Count-1;
			}
		}
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);}
	public int[] Ia3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);}
	public long[] La3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}