Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10**6)

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
int1 = lambda x: int(x)-1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

class TwoSat:
    def __init__(self, n):
        # to[v][u]...頂点uの値がvのときの遷移先と反転の有無
        self.n = n
        self.to = [[[] for _ in range(n)] for _ in range(2)]
        self.vals = [-1]*n
        self.log = []
        self.start = []

    # uがu_val(0 or 1)ならば、vはv_val(0 or 1)
    def add_edge(self, u, u_val, v, v_val):
        self.to[u_val][u].append((v, u_val ^ v_val))

    # 条件を満たすかどうかをboolで返す。構成はself.valsに入る
    def satisfy(self):
        u = val = 0
        while u < self.n:
            if val == 0: self.start.append(u)
            self.vals[u]=val
            self.log.append(u)
            if self.__dfs(u, val):
                u += 1
                val = 0
                while u < self.n and self.vals[u] != -1: u += 1
            else:
                if not self.start: return False
                u = self.start.pop()
                val = 1
                while 1:
                    v = self.log.pop()
                    self.vals[v] = -1
                    if v == u: break
        return True

    def __dfs(self, u, val):
        for v, x in self.to[val][u]:
            if self.vals[v]==-1:
                self.vals[v]=val^x
                self.log.append(v)
                if not self.__dfs(v, val ^ x): return False
            else:
                if self.vals[v] != val ^ x: return False
        return True

n, d = MI()
xy = LLI(n)
ts = TwoSat(n*2)
for i, (x, y) in enumerate(xy):
    ts.add_edge(i*2, 0, i*2+1, 1)
    ts.add_edge(i*2+1, 0, i*2, 1)
    ts.add_edge(i*2, 1, i*2+1, 0)
    ts.add_edge(i*2+1, 1, i*2, 0)

for i in range(n):
    x1, y1 = xy[i]
    for j in range(i):
        x2, y2 = xy[j]
        if abs(x1-x2) < d:
            ts.add_edge(i*2, 1, j*2, 0)
            ts.add_edge(j*2, 1, i*2, 0)
        if abs(x1-y2) < d:
            ts.add_edge(i*2, 1, j*2+1, 0)
            ts.add_edge(j*2+1, 1, i*2, 0)
        if abs(y1-x2) < d:
            ts.add_edge(i*2+1, 1, j*2, 0)
            ts.add_edge(j*2, 1, i*2+1, 0)
        if abs(y1-y2) < d:
            ts.add_edge(i*2+1, 1, j*2+1, 0)
            ts.add_edge(j*2+1, 1, i*2+1, 0)

if ts.satisfy():
    print("Yes")
    for i in range(n):
        if ts.vals[i*2]: print(xy[i][0])
        else: print(xy[i][1])
else:
    print("No")


Fixed submission:
from operator import itemgetter
from itertools import *
from bisect import *
from collections import *
from heapq import *
import sys

sys.setrecursionlimit(10**6)

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
int1 = lambda x: int(x)-1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def SCC(to, ot):
    n = len(to)

    def dfs(u):
        for v in to[u]:
            if com[v]: continue
            com[v] = 1
            dfs(v)
        top.append(u)

    top = []
    com = [0]*n
    for u in range(n):
        if com[u]: continue
        com[u] = 1
        dfs(u)

    def rdfs(u, k):
        for v in ot[u]:
            if com[v] != -1: continue
            com[v] = k
            rdfs(v, k)

    com = [-1]*n
    k = 0
    for u in top[::-1]:
        if com[u] != -1: continue
        com[u] = k
        rdfs(u, k)
        k += 1

    return k, com

class TwoSat:
    def __init__(self, n):
        # to[v][u]...頂点uの値がvのときの遷移先と反転の有無
        self.n = n
        self.to = [[] for _ in range(n*2)]
        self.ot = [[] for _ in range(n*2)]
        self.vals = []

    # uがu_val(0 or 1)ならばvはv_val(0 or 1)
    def add_edge(self, u, u_val, v, v_val):
        self.to[u*2+u_val].append(v*2+v_val)
        self.ot[v*2+v_val].append(u*2+u_val)

    # 条件を満たすかどうかをboolで返す。構成はself.valsに入る
    def satisfy(self):
        k, com = SCC(self.to, self.ot)
        for u in range(self.n):
            if com[u*2]==com[u*2+1]:return False
            self.vals.append(com[u*2]<com[u*2+1])
        return True

n, d = MI()
xy = LLI(n)
ts = TwoSat(n)

for i in range(n):
    x1, y1 = xy[i]
    for j in range(i):
        x2, y2 = xy[j]
        if abs(x1-x2) < d:
            ts.add_edge(i, 0, j, 1)
            ts.add_edge(j, 0, i, 1)
        if abs(x1-y2) < d:
            ts.add_edge(i, 0, j, 0)
            ts.add_edge(j, 1, i, 1)
        if abs(y1-x2) < d:
            ts.add_edge(i, 1, j, 1)
            ts.add_edge(j, 0, i, 0)
        if abs(y1-y2) < d:
            ts.add_edge(i, 1, j, 0)
            ts.add_edge(j, 1, i, 0)

if ts.satisfy():
    print("Yes")
    for j,xyi in zip(ts.vals,xy):print(xyi[j])
else:print("No")

Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10**6)

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
int1 = lambda x: int(x)-1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

class TwoSat:
    def __init__(self, n):
        # to[v][u]...頂点uの値がvのときの遷移先と反転の有無
        self.n = n
        self.to = [[[] for _ in range(n)] for _ in range(2)]
        self.vals = [-1]*n
        self.log = []
        self.start = []

    # uがu_valならば、vはv_val
    def add_edge(self, u, u_val, v, v_val):
        self.to[u_val][u].append((v, u_val ^ v_val))

    # 条件を満たすかどうかをboolで返す。構成はself.valsに入る
    def satisfy(self):
        u = val = 0
        while u < self.n:
            if val == 0: self.start.append(u)
            if self.__dfs(u, val):
                u += 1
                val = 0
                while u < self.n and self.vals[u] != -1: u += 1
            else:
                if not self.start: return False
                u = self.start.pop()
                val = 1
                while 1:
                    v = self.log.pop()
                    self.vals[v] = -1
                    if v == u: break
        return True

    def __dfs(self, u, val):
        if self.vals[u] != -1:
            if self.vals[u] == val: return True
            return False
        self.vals[u] = val
        self.log.append(u)
        for v, x in self.to[val][u]:
            if not self.__dfs(v, val ^ x): return False
        return True

n, d = MI()
xy = LLI(n)
ts = TwoSat(n*2)
for i, (x, y) in enumerate(xy):
    ts.add_edge(i*2, 0, i*2+1, 1)
    ts.add_edge(i*2+1, 0, i*2, 1)
    ts.add_edge(i*2, 1, i*2+1, 0)
    ts.add_edge(i*2+1, 1, i*2, 0)

for i in range(n):
    x1, y1 = xy[i]
    for j in range(i):
        x2, y2 = xy[j]
        if abs(x1-x2) < d:
            ts.add_edge(i*2, 1, j*2, 0)
            ts.add_edge(j*2, 1, i*2, 0)
        if abs(x1-y2) < d:
            ts.add_edge(i*2, 1, j*2+1, 0)
            ts.add_edge(j*2+1, 1, i*2, 0)
        if abs(y1-x2) < d:
            ts.add_edge(i*2+1, 1, j*2, 0)
            ts.add_edge(j*2, 1, i*2+1, 0)
        if abs(y1-y2) < d:
            ts.add_edge(i*2+1, 1, j*2+1, 0)
            ts.add_edge(j*2+1, 1, i*2+1, 0)

if ts.satisfy():
    print("Yes")
    for i in range(n):
        if ts.vals[i*2]: print(xy[i][0])
        else: print(xy[i][1])
else:
    print("No")


Fixed submission:
from operator import itemgetter
from itertools import *
from bisect import *
from collections import *
from heapq import *
import sys

sys.setrecursionlimit(10**6)

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
int1 = lambda x: int(x)-1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def SCC(to, ot):
    n = len(to)

    def dfs(u):
        for v in to[u]:
            if com[v]: continue
            com[v] = 1
            dfs(v)
        top.append(u)

    top = []
    com = [0]*n
    for u in range(n):
        if com[u]: continue
        com[u] = 1
        dfs(u)

    def rdfs(u, k):
        for v in ot[u]:
            if com[v] != -1: continue
            com[v] = k
            rdfs(v, k)

    com = [-1]*n
    k = 0
    for u in top[::-1]:
        if com[u] != -1: continue
        com[u] = k
        rdfs(u, k)
        k += 1

    return k, com

class TwoSat:
    def __init__(self, n):
        # to[v][u]...頂点uの値がvのときの遷移先と反転の有無
        self.n = n
        self.to = [[] for _ in range(n*2)]
        self.ot = [[] for _ in range(n*2)]
        self.vals = []

    # uがu_val(0 or 1)ならばvはv_val(0 or 1)
    def add_edge(self, u, u_val, v, v_val):
        self.to[u*2+u_val].append(v*2+v_val)
        self.ot[v*2+v_val].append(u*2+u_val)

    # 条件を満たすかどうかをboolで返す。構成はself.valsに入る
    def satisfy(self):
        k, com = SCC(self.to, self.ot)
        for u in range(self.n):
            if com[u*2]==com[u*2+1]:return False
            self.vals.append(com[u*2]<com[u*2+1])
        return True

n, d = MI()
xy = LLI(n)
ts = TwoSat(n)

for i in range(n):
    x1, y1 = xy[i]
    for j in range(i):
        x2, y2 = xy[j]
        if abs(x1-x2) < d:
            ts.add_edge(i, 0, j, 1)
            ts.add_edge(j, 0, i, 1)
        if abs(x1-y2) < d:
            ts.add_edge(i, 0, j, 0)
            ts.add_edge(j, 1, i, 1)
        if abs(y1-x2) < d:
            ts.add_edge(i, 1, j, 1)
            ts.add_edge(j, 0, i, 0)
        if abs(y1-y2) < d:
            ts.add_edge(i, 1, j, 0)
            ts.add_edge(j, 1, i, 0)

if ts.satisfy():
    print("Yes")
    for j,xyi in zip(ts.vals,xy):print(xyi[j])
else:print("No")

Buggy python submission (verdict: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10**6)

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
int1 = lambda x: int(x)-1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

class TwoSat:
    def __init__(self, n):
        # to[v][u]...頂点uの値がvのときの遷移先と反転の有無
        self.n = n
        self.to = [[[] for _ in range(n)] for _ in range(2)]
        self.vals = [-1]*n
        self.log = []
        self.start = []

    # uがu_valならば、vはv_val
    def add_edge(self, u, u_val, v, v_val):
        self.to[u_val][u].append((v, u_val ^ v_val))

    # 条件を満たすかどうかをboolで返す。構成はself.valsに入る
    def satisfy(self):
        u = val = 0
        while u < self.n:
            if val == 0: self.start.append(u)
            if self.__dfs(u, val):
                u += 1
                val = 0
                while u < self.n and self.vals[u] != -1: u += 1
            else:
                if not self.start: return False
                u = self.start.pop()
                val = 1
                while 1:
                    v = self.log.pop()
                    self.vals[v] = -1
                    if v == u: break
        return True

    def __dfs(self, u, val):
        if self.vals[u] != -1:
            if self.vals[u] == val: return True
            return False
        self.vals[u] = val
        self.log.append(u)
        for v, x in self.to[val][u]:
            if not self.__dfs(v, val ^ x): return False
        return True

n, d = MI()
xy = LLI(n)
ts = TwoSat(n*2)
for i, (x, y) in enumerate(xy):
    ts.add_edge(i*2, 0, i*2+1, 1)
    ts.add_edge(i*2+1, 0, i*2, 1)
    ts.add_edge(i*2, 1, i*2+1, 0)
    ts.add_edge(i*2+1, 1, i*2, 0)

for i in range(n):
    x1, y1 = xy[i]
    for j in range(i):
        x2, y2 = xy[j]
        if abs(x1-x2) < d:
            ts.add_edge(i*2, 1, j*2, 0)
            ts.add_edge(j*2, 1, i*2, 0)
        if abs(x1-y2) < d:
            ts.add_edge(i*2, 1, j*2+1, 0)
            ts.add_edge(j*2+1, 1, i*2, 0)
        if abs(y1-x2) < d:
            ts.add_edge(i*2+1, 1, j*2, 0)
            ts.add_edge(j*2, 1, i*2+1, 0)
        if abs(y1-y2) < d:
            ts.add_edge(i*2+1, 1, j*2+1, 0)
            ts.add_edge(j*2+1, 1, i*2+1, 0)

if ts.satisfy():
    print("Yes")
    for i in range(n):
        if ts.vals[i*2]: print(xy[i][0])
        else: print(xy[i][1])
else:
    print("No")


Fixed submission:
from operator import itemgetter
from itertools import *
from bisect import *
from collections import *
from heapq import *
import sys

sys.setrecursionlimit(10**6)

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
int1 = lambda x: int(x)-1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def SCC(to, ot):
    n = len(to)

    def dfs(u):
        for v in to[u]:
            if com[v]: continue
            com[v] = 1
            dfs(v)
        top.append(u)

    top = []
    com = [0]*n
    for u in range(n):
        if com[u]: continue
        com[u] = 1
        dfs(u)

    def rdfs(u, k):
        for v in ot[u]:
            if com[v] != -1: continue
            com[v] = k
            rdfs(v, k)

    com = [-1]*n
    k = 0
    for u in top[::-1]:
        if com[u] != -1: continue
        com[u] = k
        rdfs(u, k)
        k += 1

    return k, com

class TwoSat:
    def __init__(self, n):
        # to[v][u]...頂点uの値がvのときの遷移先と反転の有無
        self.n = n
        self.to = [[] for _ in range(n*2)]
        self.ot = [[] for _ in range(n*2)]
        self.vals = []

    # uがu_val(0 or 1)ならばvはv_val(0 or 1)
    def add_edge(self, u, u_val, v, v_val):
        self.to[u*2+u_val].append(v*2+v_val)
        self.ot[v*2+v_val].append(u*2+u_val)

    # 条件を満たすかどうかをboolで返す。構成はself.valsに入る
    def satisfy(self):
        k, com = SCC(self.to, self.ot)
        for u in range(self.n):
            if com[u*2]==com[u*2+1]:return False
            self.vals.append(com[u*2]<com[u*2+1])
        return True

n, d = MI()
xy = LLI(n)
ts = TwoSat(n)

for i in range(n):
    x1, y1 = xy[i]
    for j in range(i):
        x2, y2 = xy[j]
        if abs(x1-x2) < d:
            ts.add_edge(i, 0, j, 1)
            ts.add_edge(j, 0, i, 1)
        if abs(x1-y2) < d:
            ts.add_edge(i, 0, j, 0)
            ts.add_edge(j, 1, i, 1)
        if abs(y1-x2) < d:
            ts.add_edge(i, 1, j, 1)
            ts.add_edge(j, 0, i, 0)
        if abs(y1-y2) < d:
            ts.add_edge(i, 1, j, 0)
            ts.add_edge(j, 1, i, 0)

if ts.satisfy():
    print("Yes")
    for j,xyi in zip(ts.vals,xy):print(xyi[j])
else:print("No")