Problem p02573 (python)

Fixed submission:
# -*- coding: utf-8 -*-
import sys
from collections import Counter


def main():
    N,M = list(map(int, sys.stdin.readline().split()))
    AB_list = [ list(map(int, sys.stdin.readline().split())) for _ in range(M) ]


    ##############################
    # Union Find
    parents = [-1] * (N + 1)

    def find(x :int) -> int:
        if parents[x] < 0:
            return x
        
        parents[x] = find( parents[x] )
        return parents[x]

    def union(x,y):
        root_x = find(x)
        root_y = find(y)

        if root_x == root_y:
            return
        
        depth_x = parents[root_x]
        depth_y = parents[root_y]

        if depth_x <= depth_y:
            parents[root_y] = root_x

            if depth_x == depth_y:
                parents[root_x] -= 1

        else:
            parents[root_x] = root_y
    ##############################


    for a,b in AB_list:
        union(a,b)
    
    group = [ find(num) for num in range(1,N+1) ]
    cnt = Counter(group)
    
    print(cnt.most_common(1)[0][1])


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys


def main():
    N,M = list(map(int, sys.stdin.readline().split()))
    AB_list = [ list(map(int, sys.stdin.readline().split())) for _ in range(M) ]


    edge = {}
    # { int : set }
    # { the number of human : {friends} }

    for a,b in AB_list:
        edge.setdefault(a, set())
        edge[a].add(b)

        edge.setdefault(b, set())
        edge[b].add(a)
    

    visited = [None] + [0]*N
    #  0: not visited,  1:visited

    max_relation = 0

    for i in range(1, N+1):
        if visited[i] == 0:
            visited[i] = 1
            ret = dfs(edge, visited, i)
            max_relation = max(max_relation, ret)
    
    
    print( max_relation )


def dfs(edge, visited, num):
    res =[]

    if num in edge:
        for next_node in edge[num]:
            if visited[next_node] == 0:
                visited[next_node] = 1
                
                d = dfs(edge, visited, next_node)
                res.append(d)


    if res:
        return 1 + sum(res)
    else:
        return 1


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import sys


def main():
    N,M = list(map(int, sys.stdin.readline().split()))
    AB_list = [ list(map(int, sys.stdin.readline().split())) for _ in range(M) ]


    edge = {}  
    # { int : set }
    # { the number of human : {friends} }

    for a,b in AB_list:
        edge.setdefault(a, set())
        edge[a].add(b)

        edge.setdefault(b, set())
        edge[b].add(a)
    

    visited = [None] + [0]*N
    #  0: not visited,  1:visited


    max_relation = 0

    for i in range(1, N+1):
        if visited[i] == 0:
            visited[i] = 1
            ret = dfs(edge, visited, i, 0)
            max_relation = max(max_relation, ret)
    
    
    print( max_relation + 1 )


def dfs(edge, visited, num, cnt):
    max_cnt = cnt

    if num in edge:
        for next_node in edge[num]:
            if visited[next_node] == 0:
                visited[next_node] = 1
                cnt += 1
                ret = dfs(edge, visited, next_node, cnt)
                max_cnt = max(max_cnt, ret)

    return max_cnt


if __name__ == "__main__":
    main()