Problem p02762 (python)

Fixed submission:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
import pprint
sys.setrecursionlimit(10 ** 9)


INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


class UnionFind:
    def __init__(self, n):
        # 負  : 根であることを示す。絶対値はランクを示す
        # 非負: 根でないことを示す。値は親を示す
        self.table = [-1] * n
        self.size = [1] * n
        self.group_num = n

    def root(self, x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.root(self.table[x])
            return self.table[x]

    def get_size(self, x):
        r = self.root(x)
        return self.size[r]

    def is_same(self, x, y):
        return self.root(x) == self.root(y)

    def union(self, x, y):
        r1 = self.root(x)
        r2 = self.root(y)
        if r1 == r2:
            return
        # ランクの取得
        d1 = self.table[r1]
        d2 = self.table[r2]
        if d1 <= d2:
            self.table[r2] = r1
            self.size[r1] += self.size[r2]
            if d1 == d2:
                self.table[r1] -= 1
        else:
            self.table[r1] = r2
            self.size[r2] += self.size[r1]
        self.group_num -= 1

n, m, k = LI()
G = [set() for _ in range(n)]
G2 = [set() for _ in range(n)]
U = UnionFind(n)
for _ in range(m):
    a, b = LI()
    U.union(a - 1, b - 1)
    G2[a - 1].add(b - 1)
    G2[b - 1].add(a - 1)

for _ in range(k):
    a, b = LI()
    G[a - 1].add(b - 1)
    G[b - 1].add(a - 1)


D = defaultdict(lambda:set())
for i in range(n):
    D[U.root(i)].add(i)

for i in range(n):
    i_set = D[U.root(i)]
    ret = len(i_set) - 1
    for j in G2[i]:
        if j in i_set:
            ret -= 1
    for k in G[i]:
        if k in i_set:
            ret -= 1
    print(ret, end='\t')

Buggy submission (verdict: Runtime Error):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
import pprint
sys.setrecursionlimit(10 ** 9)


INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m, k = LI()
G1 = [set() for _ in range(n)]
G2 = [set() for _ in range(n)]
for _ in range(m):
    a, b = LI()
    G1[a - 1].add(b - 1)
    G1[b - 1].add(a - 1)

for _ in range(k):
    a, b = LI()
    G1[a - 1].add(b - 1)
    G1[b - 1].add(a - 1)

flg = 0
li = 0
ri = 0
L = [(-INF, -INF)]
for i in range(n - 1):
    if flg == 0 and i + 1 in G1[i]:
        flg = 1
        li = i
        ri = i + 1
    elif flg:
        if i + 1 in G1[i]:
            ri += 1
        else:
            flg = 0
            L += [(li, ri)]
if flg:
    L += [(li, ri)]


ans = []
for i in range(n):
    l, r = L[bisect_right(L, (i, _)) - 1]
    if l <= i <= r:
        ret = r - l
        for v in G1[i]:
            if l <= v <= r:
                ret -= 1
        for v in G2[i]:
            if l <= v <= r:
                ret -= 1
        ans += [ret]
    else:
        ans += [0]

print(*ans, sep=' ')