Problem p02762 (python)

Fixed submission:
import sys
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def main():
    n, m, k = map(int, input().split())
    fgraph = [list() for i in range(n)]
    bgraph = [list() for i in range(n)]
    cgraph = [list() for i in range(n)]
    
    rks = [None] *n
    sys.setrecursionlimit(10**9)
    # r: どの連結成分に存在するかをメモするid
    def dfs(graph,s,r):
        if rks[s] is None:
            rks[s] = r
        for i in range(len(graph[s])):
            if rks[graph[s][i]-1] is not None:
                continue
            dfs(graph, graph[s][i]-1,r)

    for i in range(m):
        a,b = map(int, input().split())
        fgraph[a-1].append(b)
        fgraph[b-1].append(a)

    # print(fgraph)
    #print(bgraph)
    # 連結成分に対してid的なindexをつける
    c = -1
    for i in range(n):
        if rks[i] is None:
            c += 1
            dfs(fgraph, i, c)
    
    for i in range(k):
        c,d = map(int, input().split())
        if rks[c-1] == rks[d-1]:
            bgraph[d-1].append(c)
            bgraph[c-1].append(d)

    counter = Counter(rks)
    for i in range(n):
        # counter[rks[i]] 連結成分のサイズ
        # len(fgraph[i]) 友達の数
        # len(bgraph[i]) 連結成分内のブロックの数
        # 1 自分
        print(counter[rks[i]] - len(fgraph[i]) - len(bgraph[i]) - 1)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
n, m, k = map(int, input().split())

F = [[] for i in range(n)]   #Friend list
for i in range(m):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    F[a].append(b)
    F[b].append(a)


gnum = 1    #group number
#DFS
def DFS_func(a:int, gnum:int):   #Target, Data
        if G[a] == 0:
            G[a] = gnum
            for i in range(len(F[a])):
                DFS_func(F[a][i], gnum)
        else:
            pass
G = [0 for i in range(n)]   #it has Group numbers       
for i in range(n):
    if G[i] == 0:
        DFS_func(i, gnum)
        gnum += 1
    else:
        pass

B = [0 for i in range(n)]   #How many Blocks in the same Group
for i in range(k):
    c, d = map(int, input().split())
    c, d = c-1, d-1
    if G[c] == G[d]:
        B[c] , B[d] = B[c]+1, B[d]+1
    else:
        pass

GS = [0 for i in range(gnum)]   #Group Size
for i in range(n):
    GS[G[i]] += 1

for i in range(n):
    if G[i] == 0:
        ans = 0
    else:
        ans = GS[G[i]]-len(F[i])-B[i]-1
    print(ans, end = ' ')

Buggy submission (verdict: Runtime Error):
#Union Findデータ構造
class UnionFind:
    def __init__(self, n):
        #Data: 正:親要素のノード番号, 負:サイズ
        self.D = [-1 for i in range(n)]
    
    def find(self, x:int)->int:
        if self.D[x] < 0:
            return x
        else:
            self.D[x] = self.find(self.D[x])
            return self.D[x]

    
    def unite(self, x:int, y:int)->bool:
        x, y = self.find(x), self.find(y)
        if x == y:
            return False
        else:
            if self.D[x] > self.D[y]:
                self.D[x], self.D[y] = self.D[y], self.D[x]
            else:
                pass
            self.D[x] += self.D[y]
            self.D[y] = x
            return True

    def same(self, x:int, y:int)->bool:
        return self.find(x) == self.find(y)

    def size(self, x:int)->int:
        return -self.D[self.find(x)]

n, m, k = map(int, input().split())

uf = UnionFind(n)  

CF = [0] * n #Count Friend
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    CF[a] += 1
    CF[b] += 1
    uf.unite(a, b)

CBinG = [0] * n #Count Block in Group
for i in range(k):
    c, d = map(int, input().split())
    c -= 1
    d -= 1
    if uf.same(c, d) == True:
        CBinG[c] += 1
        CBinG[d] += 1
    else:
        pass

for i in range(n):
    ans = uf.size(i) - 1 - CF[i] - CBinG[i]
    print(ans, end=' ')

Buggy submission (verdict: Runtime Error):
#Union Findデータ構造
class UnionFind:
    def __init__(self, n):
        #Data: 正:親要素のノード番号, 負:サイズ
        self.D = [-1 for i in range(n)]
    
    def find(self, x:int)->int:
        if self.D[x] < 0:
            return x
        else:
            self.D[x] = self.find(self.D[x])
            return self.D[x]
    
    def unite(self, x:int, y:int)->bool:
        x, y = self.find(x), self.find(y)
        if x == y:
            return False
        else:
            if self.D[x] > self.D[y]:
                self.D[x], self.D[y] = self.D[y], self.D[x]
            else:
                pass
            self.D[x] += self.D[y]
            self.D[y] = x
            return True

    def same(self, x:int, y:int)->bool:
        return self.find(x) == self.find(y)

    def size(self, x:int)->int:
        return -self.D[self.find(x)]

n, m, k = map(int, input().split())

uf = UnionFind(n)  

CF = [0] * n #Count Friend
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    CF[a] += 1
    CF[b] += 1
    uf.unite(a, b)

CBinG = [0] * n #Count Block in Group
for i in range(k):
    c, d = map(int, input().split())
    c -= 1
    d -= 1
    if uf.same(c, d) == True:
        CBinG[c] += 1
        CBinG[d] += 1

for i in range(n):
    ans = uf.size(i) - 1 - CF[i] - CBinG[i]
    print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque
n, m, k = map(int, input().split())
F = [deque([]) for i in range(n)] #Friend
B = [deque([]) for i in range(n)] #Block
#友達関係
for i in range(m):
    a, b = map(int, input().split())
    F[a-1].append(b)
    F[b-1].append(a)

#ブロック関係
for i in range(k):
    c, d = map(int, input().split())
    B[c-1].append(d)
    B[d-1].append(c)
#print('F=',F)
#print('B=',B)

def using_DFS(p):
    if G[p] == 0:
        G[p] = num
        for j in F[p]:
            using_DFS(j-1)
    else:
        pass

#DFSで連結部分ごとのグループに分ける
G = [0] * n #Group
S = deque([])   #Size
num = 0
for i in range(n):
    if G[i] == 0:
        num += 1
        t = 0
        using_DFS(i)
        S.append(G.count(num))
    else:
        pass
#print('G=',G)
#print('S=',S)

#答えの計算
for i in range(n):
    ans = S[G[i]-1] - 1
    #ブロック関係がこの中で何人いるか
    for j in B[i]:
        if G[j-1] == G[i]:
            ans -= 1
        else:
            pass
    #直接の友達関係がこの中で何人いるか
    for k in F[i]:
        if G[k-1] == G[i]:
            ans -= 1
        else:
            pass
    print(ans)