Problem p03411 (python)

Fixed submission:
# C
N = int(input())

red_list = []
blue_list = []

for _ in range(N):
    x, y = map(int, input().split())
    red_list.append([x, y])

for _ in range(N):
    x, y = map(int, input().split())
    blue_list.append([x, y])
    
GR = dict()
GB = dict()

for i in range(N):
    GR[i+1] = [0, [], 0]
    GB[i+1] = [0, [], 0]

for i in range(N):
    for j in range(N):
        if red_list[i][0] < blue_list[j][0] and red_list[i][1] < blue_list[j][1]:
            GR[i+1][1].append(j+1)
            GB[j+1][1].append(i+1)
            
def find_path(GR_, GB_, N):
    GR = GR_.copy()
    GB = GB_.copy()

    done_red = [0]*(N+1)
    can_red = [0]*(N+1)
    done_blue = [0]*(N+1)

    # start from RED
    for i in range(1, N+1): 
        if GR[i][0] == 0:
            can_red[i] = 1
        GR[i][2] = -1
        GB[i][2] = -1

    while True:

        go_flg = 0
        for i in range(1, N+1):
            if can_red[i] == 1 and done_red[i] == 0:
                go_flg = 1

        if go_flg == 0:
            return []

        for i in range(1, N+1): 
            if can_red[i] == 1 and done_red[i] == 0:
                done_red[i] = 1
                for j in GR[i][1]:
                    if done_blue[j] == 0:
                        GB[j][2] = i
                        done_blue[j] = 1
                        if GB[j][0] != 0:
                            can_red[GB[j][0]] = 1
                            GR[GB[j][0]][2] = j
                        else:
                            # found
                            res_list = [j, i]
                            while GR[res_list[-1]][2] > 0:
                                j = GR[res_list[-1]][2]
                                i = GB[j][2]
                                res_list.append(j)
                                res_list.append(i)
                            return res_list
                        
# set initial path
res = 0
for i in range(1, N+1):
    for j in GR[i][1]:
        if GB[j][0] == 0:
            GR[i][0] = j
            GB[j][0] = i
            res += 1
            break

while True:
    new_path = find_path(GR, GB, N)
    if len(new_path) == 0:
        break
    else:
        for l in range(len(new_path) // 2):
            j, i = new_path[2*l], new_path[2*l+1]
            GB[j][0] = i
            GR[i][0] = j
        res += 1

print(res)

Buggy submission (verdict: Runtime Error):
# C
N = int(input())

red_list = []
blue_list = []

for _ in range(N):
    x, y = map(int, input().split())
    red_list.append([x, y])

for _ in range(N):
    x, y = map(int, input().split())
    blue_list.append([x, y])
    
GR = dict()
GB = dict()

for i in range(N):
    GR[i+1] = [0, [], 0]
    GB[i+1] = [0, [], 0]

for i in range(N):
    for j in range(N):
        if red_list[i][0] < blue_list[j][0] and red_list[i][1] < blue_list[j][1]:
            GR[i+1][1].append(j+1)
            GB[j+1][1].append(i+1)
            
def find_path(GR_, GB_, N):
    GR = GR_.copy()
    GB = GB_.copy()

    done_red = [0]*(N+1)
    can_red = [0]*(N+1)
    done_blue = [0]*(N+1)

    # start from RED
    for i in range(1, N+1): 
        if GR[i][0] == 0:
            can_red[i] = 1
        GR[i][2] = -1
        GB[i][2] = -1

    while True:

        go_flg = 0
        for i in range(1, N+1):
            if can_red[i] == 1 and done_red[i] == 0:
                go_flg = 1

        if go_flg == 0:
            return []

        for i in range(1, N+1): 
            if can_red[i] == 1 and done_red[i] == 0:
                done_red[i] = 1
                for j in GR[i][1]:
                    if done_blue[j] == 0:
                        GB[j][2] = i
                        done_blue[j] = 1
                        if GB[j][0] != 0:
                            can_red[GB[i][0]] = 1
                            GR[GB[j][0]][2] = j
                        else:
                            # found
                            res_list = [j, i]
                            while res_list[-1] > 0:
                                j = GR[res_list[-1]][2]
                                i = GB[j][2]
                                res_list.append(j)
                                res_list.append(i)
                            return res_list
                        
# set initial path
res = 0
for i in range(1, N+1):
    for j in GR[i][1]:
        if GB[j][0] == 0:
            GR[i][0] = j
            GB[j][0] = i
            res += 1
            break

while True:
    new_path = find_path(GR, GB, N)
    if len(new_path) == 0:
        break
    else:
        for l in len(new_path) // 2:
            j, i = 2*l, 2*l+1
            GB[j][0] = i
            GB[i][0] = j
        res += 1

print(res)

Buggy submission (verdict: Runtime Error):
# C
N = int(input())

red_list = []
blue_list = []

for _ in range(N):
    x, y = map(int, input().split())
    red_list.append([x, y])

for _ in range(N):
    x, y = map(int, input().split())
    blue_list.append([x, y])
    
GR = dict()
GB = dict()

for i in range(N):
    GR[i+1] = [0, [], 0]
    GB[i+1] = [0, [], 0]

for i in range(N):
    for j in range(N):
        if red_list[i][0] < blue_list[j][0] and red_list[i][1] < blue_list[j][1]:
            GR[i+1][1].append(j+1)
            GB[j+1][1].append(i+1)
            
def find_path(GR_, GB_, N):
    GR = GR_.copy()
    GB = GB_.copy()

    done_red = [0]*(N+1)
    can_red = [0]*(N+1)
    done_blue = [0]*(N+1)

    # start from RED
    for i in range(1, N+1): 
        if GR[i][0] == 0:
            can_red[i] = 1
        GR[i][2] = -1
        GB[i][2] = -1

    while True:

        go_flg = 0
        for i in range(1, N+1):
            if can_red[i] == 1 and done_red[i] == 0:
                go_flg = 1

        if go_flg == 0:
            return []

        for i in range(1, N+1): 
            if can_red[i] == 1 and done_red[i] == 0:
                done_red[i] = 1
                for j in GR[i][1]:
                    if done_blue[j] == 0:
                        GB[j][2] = i
                        done_blue[j] = 1
                        if GB[j][0] != 0:
                            can_red[GB[i][0]] = 1
                        else:
                            # found
                            res_list = [j, i]
                            while res_list[-1] > 0:
                                j = GR[res_list[-1]][2]
                                i = GB[j][2]
                                res_list.append(j)
                                res_list.append(i)
                            return res_list
                        
# set initial path
res = 0
for i in range(1, N+1):
    for j in GR[i][1]:
        if GB[j][0] == 0:
            GR[i][0] = j
            GB[j][0] = i
            res += 1
            break

while True:
    new_path = find_path(GR, GB, N)
    if len(new_path) == 0:
        break
    else:
        for l in len(new_path) // 2:
            j, i = 2*l, 2*l+1
            GB[j][0] = i
            GB[i][0] = j
        res += 1

print(res)