Problem p02762 (python)

Fixed submission:
import sys
from math import *
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)]
    
    is_all_seen = [False]*n
    cnt = 0
    seen = [None]*n
    rks = [None] *n
    sys.setrecursionlimit(10**9)
    def dfs(graph,s,r):
        seen[s] = s+1
        if rks[s] is None:
            rks[s] = r

        for i in range(len(graph[s])):
            if seen[graph[s][i]-1]:
                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):
import sys
from math import *
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)]


    seen = [None]*n
    def dfs(graph,s):
        seen[s] = s+1
        for i in range(len(graph[s])):
            if seen[graph[s][i]-1]:
                continue
            dfs(graph, graph[s][i]-1)

    for i in range(m):
        a,b = map(int, input().split())

        fgraph[a-1].append(b)
        fgraph[b-1].append(a)

    for i in range(k):
        c,d = map(int, input().split())
        bgraph[d-1].append(c)
        bgraph[c-1].append(d)

    # print(fgraph)
    #print(bgraph)
    rk = [list()]*n
    for i in range(n):
        seen = [None]*n
        dfs(fgraph, i)
        rk[i] = seen
    
    # print(rk)
    # print(fgraph)
    for i in range(n):
        non = {None}
        me = {i+1}
        srk = set(rk[i])-non
        bg = set(bgraph[i])
        fg = set(fgraph[i])

        print(len(srk-bg-fg-me))
        
    return

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from math import *
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)]


    seen = [False]*n
    def dfs(graph,s,t):
        seen[s] = True
        if t+1 in graph[s]:
            return True
        for i in range(len(graph[s])):
            if seen[graph[s][i]-1]:
                continue
            if dfs(graph, graph[s][i]-1, t):
                return True
        return False

    for i in range(m):
        a,b = map(int, input().split())

        fgraph[a-1].append(b)
        fgraph[b-1].append(a)

    for i in range(k):
        c,d = map(int, input().split())
        bgraph[d-1].append(c)
        bgraph[c-1].append(d)

    # print(fgraph)
    #print(bgraph)

    for i in range(n):
        for j in range(i,n):
            if j == i or j+1 in bgraph[i] or j+1 in fgraph[i]:
                continue
            # print(i+1,j+1)
            seen = [False]*n
            if dfs(fgraph, i, j):
                cgraph[i].append(j)
                cgraph[j].append(i)
            # i+1 から j+1 への経路の存在を探索する
    print(cgraph)    
    for i in range(n):

        print(len(set(cgraph[i])))
    
    return

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from math import *
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)]


    seen = [False]*n
    def dfs(graph,s,t):
        seen[s] = True
        if t+1 in graph[s]:
            return True
        for i in range(len(graph[s])):
            if seen[graph[s][i]-1]:
                continue
            if dfs(graph, graph[s][i]-1, t):
                return True
        return False

    for i in range(m):
        a,b = map(int, input().split())

        fgraph[a-1].append(b)
        fgraph[b-1].append(a)

    for i in range(k):
        c,d = map(int, input().split())
        bgraph[d-1].append(c)
        bgraph[c-1].append(d)

    # print(fgraph)
    #print(bgraph)

    for i in range(n):
        for j in range(i,n):
            if j == i or j+1 in bgraph[i] or j+1 in fgraph[i]:
                continue
            # print(i+1,j+1)
            seen = [False]*n
            if dfs(fgraph, i, j):
                cgraph[i].append(j)
                cgraph[j].append(i)
            # i+1 から j+1 への経路の存在を探索する
    #print(cgraph)    
    for i in range(n):

        print(len(set(cgraph[i])))
    
    return

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from math import *
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)]


    seen = [False]*n
    def dfs(graph,s,t):
        seen[s] = True
        if t+1 in graph[s]:
            return True
        for i in range(len(graph[s])):
            if seen[graph[s][i]-1]:
                continue
            if dfs(graph, graph[s][i]-1, t):
                return True
        return False

    for i in range(m):
        a,b = map(int, input().split())

        fgraph[a-1].append(b)
        fgraph[b-1].append(a)

    for i in range(k):
        c,d = map(int, input().split())
        bgraph[d-1].append(c)
        bgraph[c-1].append(d)

    # print(fgraph)
    #print(bgraph)

    for i in range(n):
        for j in range(n):
            if j == i or j+1 in bgraph[i] or j+1 in fgraph[i]:
                continue
            # print(i+1,j+1)
            seen = [False]*n
            if dfs(fgraph, i, j):
                cgraph[i].append(j)
                cgraph[j].append(i)
            # i+1 から j+1 への経路の存在を探索する
    #print(cgraph)    
    for i in range(n):

        print(len(set(cgraph[i])))
    
    return

if __name__ == '__main__':
    main()