Problem p02573 (python)

Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip('\r\n')

class DSU(object):
    
    def __init__(self, n):
        self.parent = [i for i in range(n)]
        self.rank = [1] * n
        self.cycle = False

    def find(self, key):
        if self.parent[key] != key:
            self.parent[key] = self.find(self.parent[key])
        return self.parent[key]

    def union(self, a, b):
        s1, s2 = self.find(a), self.find(b)
        if s1 != s2:
            if self.rank[s1] < self.rank[s2]:
                self.parent[s1] = s2
                self.rank[s2] += self.rank[s1]
            else:
                self.parent[s2] = s1
                self.rank[s1] += self.rank[s2]
        else:
            self.cycle = True
        
    def has_cycle(self):
        return self.cycle
    
    def show_parent(self):
        return self.parent

    def show_rank(self):
        return self.rank

def solve():
    n, m = map(int, input().split())
    d = DSU(n + 1)
    if m > 0:
        for _ in range(m):
            a, b = map(int, input().split())
            d.union(a, b)
        # print(d.show_parent())
        print(max(d.show_rank()))
    else:
        print(1)

solve()

Buggy submission (verdict: Runtime Error):
import sys
from collections import Counter
input = lambda: sys.stdin.readline().rstrip('\r\n')

# Disjoint Set Union

class DSU(object):

    def __init__(self, n):
        self.parent = [-1] * n

    def find(self, key):
        if self.parent[key] == -1:
            return key
        return self.find(self.parent[key])

    def union(self, a, b):
        s1, s2 = self.find(a), self.find(b)
        if s2 != s1:
            self.parent[s2] = s1

    def show(self):
        print(self.parent[1:])

def solve():
    n, m = map(int, input().split())
    d = DSU(n + 1)
    if m:
        for _ in range(m):
           a, b = map(int, input().split())
           d.union(a, b)
        ans = []
        for i in range(1, n + 1):
            ans.append(d.find(i))
        mp = Counter(ans)
        print(max(mp.values()))
    else:
        print(1)
    

solve()

Buggy submission (verdict: Runtime Error):
import sys
import collections
input = lambda: sys.stdin.readline().rstrip("\r\n")

## taken from GFG

class Graph:
    
	def __init__(self,V): 
		self.V = V 
		self.adj = [[] for i in range(V)] 

	def DFSUtil(self, temp, v, visited): 
		visited[v] = True
		temp.append(v) 
		for i in self.adj[v]: 
			if visited[i] == False: 
				temp = self.DFSUtil(temp, i, visited) 
		return temp
        
	def addEdge(self, v, w): 
		self.adj[v].append(w) 
		self.adj[w].append(v) 

	def connectedComponents(self): 
		visited = [] 
		cc = [] 
		for i in range(self.V): 
			visited.append(False) 
		for v in range(self.V): 
			if visited[v] == False: 
				temp = [] 
				cc.append(self.DFSUtil(temp, v, visited)) 
		return cc 

def solve():
    n, m = map(int, input().split())
    g = Graph(n)
    if m:
        for _ in range(m):
            u, v = map(int, input().split())
            g.addEdge(u - 1, v - 1)
        ans = g.connectedComponents()
        res = - 10 ** 10
        for i in ans:
            res = max(res, len(i))
        print(res)
    else:
        print(1)

if __name__=="__main__": 
	solve()

Buggy submission (verdict: Runtime Error):
import sys
import collections
input = lambda: sys.stdin.readline().rstrip("\r\n")

class DFS:

    def __init__(self):
        self.adj = collections.defaultdict(list)
        self.ans = []

    def addEdge(self, u, v):
        self.adj[u].append(v)
        self.adj[v].append(u)

    def dfs_helper(self, src, visited):
        self.ans.append(src)
        visited[src] = True
        for i in self.adj[src]:
            if not visited[i]:
                self.dfs_helper(i, visited)

    def dfs(self, src):
        res = []
        visited = collections.defaultdict(lambda: False)
        for k, v in self.adj.items():
            visited[k] = False
        for k, v in self.adj.items():
            if not visited[k]:
                self.dfs_helper(k, visited)
                res.append(self.ans)
                self.ans = []
        return res


def solve():
    d = DFS()
    n, m = map(int, input().split())
    if m != 0:
        for _ in range(m):
            u, v = map(int, input().split())
            d.addEdge(u, v)
        ans = d.dfs(1)
        print(len(max(ans, key=lambda i: len(i))))
    else:
        print(1)

solve()

Buggy submission (verdict: Runtime Error):
import sys
import collections
input = lambda: sys.stdin.readline().rstrip("\r\n")

class DFS:

    def __init__(self):
        self.adj = collections.defaultdict(list)
        self.ans = []

    def addEdge(self, u, v):
        self.adj[u].append(v)
        self.adj[v].append(u)

    def dfs_helper(self, src, visited):
        self.ans.append(src)
        visited[src] = True
        for i in self.adj[src]:
            if not visited[i]:
                self.dfs_helper(i, visited)

    def dfs(self, src):
        res = []
        visited = collections.defaultdict(lambda: False)
        for k, v in self.adj.items():
            visited[k] = False
        for k, v in self.adj.items():
            if not visited[k]:
                self.dfs_helper(k, visited)
                res.append(self.ans)
                self.ans = []
        return res


def solve():
    d = DFS()
    n, m = map(int, input().split())
    for _ in range(m):
        u, v = map(int, input().split())
        d.addEdge(u, v)
    ans = d.dfs(1)
    print(len(max(ans, key=lambda i: len(i))))

solve()

Buggy submission (verdict: Runtime Error):
import sys
import collections
input = lambda: sys.stdin.readline().rstrip("\r\n")

class DFS:

    def __init__(self):
        self.adj = collections.defaultdict(list)
        self.ans = 0

    def addEdge(self, u, v):
        self.adj[u].append(v)
        self.adj[v].append(u)

    def dfs_helper(self, src, visited):
        self.ans += 1
        visited[src] = True
        for i in self.adj[src]:
            if not visited[i]:
                self.dfs_helper(i, visited)

    def dfs(self, src):
        res = - 10 ** 10
        visited = collections.defaultdict(lambda: False)
        for k, v in self.adj.items():
            visited[k] = False
        for k, v in self.adj.items():
            if not visited[k]:
                self.dfs_helper(k, visited)
                res = max(res, self.ans)
                self.ans = 0
        return res


def solve():
    d = DFS()
    n, m = map(int, input().split())
    for _ in range(m):
        u, v = map(int, input().split())
        d.addEdge(u, v)
    print(d.dfs(1))

solve()