Problem p02573 (python)

Fixed submission:
from sys import stdin
import collections
def ip(): return [int(i) for i in stdin.readline().split()]
def sp(): return [str(i) for i in stdin.readline().split()]

# 4 6
# 1 2
# 1 3
# 1 4
# 2 3
# 2 4
# 3 4

def find(d,x):
    root = x
    while root != d[root]:
        root = d[root]

    while x != root:
        nxt = d[x]
        d[x] = root
        x = nxt
    return d,root


def merge(d,s,x,y):
    d,x = find(d,x)
    d,y = find(d,y)
    if x == y: return d,s
    if s[x] > s[y]:
        s[x] += s[y]
        d[y] = d[x]
    else:
        s[y] += s[x]
        d[x] = d[y]
    return d,s


n,m = ip()
d = {i+1: i+1 for i in range(n)}
s = {i+1: 1 for i in range(n)}
seen = set()
for _ in range(m):
    # print(d,s)
    x,y = ip()
    if (x,y) in seen or (y,x) in seen: continue
    seen.add((x,y))
    seen.add((y,x))
    d,s = merge(d,s,x,y)
ans = 0
for i in s:
    ans = max(ans, s[i])
print(ans)

Buggy submission (verdict: Runtime Error):
from sys import stdin
import collections
def ip(): return [int(i) for i in stdin.readline().split()]
def sp(): return [str(i) for i in stdin.readline().split()]

n,m = ip()
# d = collections.defaultdict(list)
d = {}
seen = set()
for i in range(m):
    x,y = ip()
    if (x,y) in seen or (y,x) in seen:
        continue
    # d[x].append(y)
    # d[y].append(x)
    if x not in d:
        d[x] = [y]
    else:
        d[x].append(y)
    if y not in d:
        d[y] = [x]
    else:
        d[y].append(x)
    seen.add((x,y))
    seen.add((y,x))

def dfs(k, i, d, visited):
    k += 1
    visited.add(i)
    if i not in d: return visited, k
    for j in d[i]:
        if j not in visited:
            visited, k = dfs(k, j, d, visited)
    return visited, k

visited = set()
ans = 0
for i in range(1, n+1):
    if i in visited: continue
    visited, k = dfs(0, i, d, visited)
    # print(f'{visited=} | {k=} | {ans=}')
    ans = max(ans, k)
print(ans)

Buggy submission (verdict: Runtime Error):
from sys import stdin
import collections
def ip(): return [int(i) for i in stdin.readline().split()]
def sp(): return [str(i) for i in stdin.readline().split()]

n,m = ip()
d = collections.defaultdict(list)
seen = set()
for i in range(m):
    x,y = ip()
    if (x,y) in seen or (y,x) in seen:
        continue
    d[x].append(y)
    d[y].append(x)
    seen.add((x,y))
    seen.add((y,x))

def dfs(k, i, d, visited):
    k += 1
    visited.add(i)
    if i not in d: return visited, k
    for j in d[i]:
        if j not in visited:
            visited, k = dfs(k, j, d, visited)
    return visited, k

visited = set()
ans = 0
for i in range(1, n+1):
    if i in visited: continue
    visited, k = dfs(0, i, d, visited)
    # print(f'{visited=} | {k=} | {ans=}')
    ans = max(ans, k)
print(ans)

Buggy submission (verdict: Runtime Error):
from sys import stdin
import collections
def ip(): return [int(i) for i in stdin.readline().split()]
def sp(): return [str(i) for i in stdin.readline().split()]

n,m = ip()
d = collections.defaultdict(list)
seen = set()
for i in range(m):
    x,y = ip()
    if (x,y) in seen or (y,x) in seen:
        continue
    d[x].append(y)
    d[y].append(x)
    seen.add((x,y))
    seen.add((y,x))

def dfs(k, i, d, visited):
    k += 1
    visited.add(i)
    for j in d[i]:
        if j not in visited:
            visited, k = dfs(k, j, d, visited)
    return visited, k

visited = set()
ans = 0
for i in range(1, n+1):
    if i in visited: continue
    if i in d: visited, k = dfs(0, i, d, visited)
    # print(f'{visited=} | {k=} | {ans=}')
    ans = max(ans, k)
print(ans)

Buggy submission (verdict: Runtime Error):
from sys import stdin
import collections
def ip(): return [int(i) for i in stdin.readline().split()]
def sp(): return [str(i) for i in stdin.readline().split()]

n,m = ip()
d = collections.defaultdict(list)
seen = set()
for i in range(m):
    x,y = ip()
    if (x,y) in seen or (y,x) in seen:
        continue
    d[x].append(y)
    d[y].append(x)
    seen.add((x,y))
    seen.add((y,x))

def dfs(k, i, d, visited):
    k += 1
    visited.add(i)
    for j in d[i]:
        if j not in visited:
            visited, k = dfs(k, j, d, visited)
    return visited, k

visited = set()
ans = 0
for i in range(1, n+1):
    if i in visited: continue
    visited, k = dfs(0, i, d, visited)
    # print(f'{visited=} | {k=} | {ans=}')
    ans = max(ans, k)
print(ans)