Problem: There are N persons called Person 1 through Person N.  You are given M facts that "Person A_i and Person B_i are friends." The same fact may be given multiple times.  If X and Y are friends, and Y and Z are friends, then X and Z are also friends. There is no friendship that cannot be derived from the M given facts.  Takahashi the evil wants to divide the N persons into some number of groups so that every person has no friend in his/her group.  At least how many groups does he need to make?

Submission attempts (in order):
n,m = map(int,input().split())
branch = set(tuple(map(int,input().split())) for i in range(m))
print(branch)
que = range(n)
flag = -1
friend = [{0}]

for a,b in branch:
    for i in range(len(friend)):
        if (a in friend[i]) or (b in friend[i]):
            friend[i].add(a)
            friend[i].add(b)
            if flag >= 0:
                friend[flag] = friend[flag]|friend[i]
                friend.pop(i)
                break
            flag = i
    if flag == -1:
        friend.append({a,b})
    else: flag = -1
    print(friend)

len_friend = list(len(i) for i in friend)
print(max(len_friend))

n,m = map(int,input().split())
branch = set(tuple(map(int,input().split())) for i in range(m))
print(branch)
que = range(n)
flag = -1
friend = [{0}]

for a,b in branch:
    for i in range(len(friend)):
        if (a in friend[i]) or (b in friend[i]):
            friend[i].add(a)
            friend[i].add(b)
            if flag >= 0:
                friend[flag] = friend[flag]|friend[i]
                friend.pop(i)
                break
            flag = i
    if flag == -1:
        friend.append({a,b})
    else: flag = -1

len_friend = list(len(i) for i in friend)
print(max(len_friend))

n,m = map(int,input().split())
branch = list(tuple(map(int,input().split())) for i in range(m))
que = range(n)
flag = -1
friend = [{1}]

for a,b in branch:
    for i in range(len(friend)):
        if (a in friend[i]) or (b in friend[i]):
            friend[i].add(a)
            friend[i].add(b)
            if flag >= 0:
                friend[flag] = friend[flag]|friend[i]
                friend.pop(i)
            flag = i
    if flag == -1:
        friend.append({a,b})
    else: flag = -1

print(friend)
len_friend = list(len(i) for i in friend)
print(max(len_friend))

sys.setrecursionlimit(10000)

class Graph:
    def __init__(self,n,side):
        self.side = side
        self.neighbor = [set() for i in range(n+1)]
        self.visited = [False]*(n+1)
        self.group =[0]*n
        self.product()
    def product(self):
        for a,b in self.side:
            self.neighbor[a].add(b)
            self.neighbor[b].add(a)
    def dfs(self,x,a):
        self.group[a] += 1
        self.visited[x] = True
        for i in self.neighbor[x]:
            if self.visited[i]:continue
            self.dfs(i,a)
    def all_dfs(self):
        a = 0
        for i in range(1,n+1):
            if not self.visited[i]:
                self.dfs(i,a)
                a += 1

n,m = map(int,input().split())
side = list(tuple(map(int,input().split()))for i in range(m))
graph1 = Graph(n,side)
graph1.all_dfs()
print((graph1.group))

import sys
sys.setrecursionlimit(10000000)

class Graph:
    def __init__(self,n,side):
        self.side = side
        self.neighbor = [set() for i in range(n+1)]
        self.visited = [False]*(n+1)
        self.group =[0]*n
        self.product()
    def product(self):
        for a,b in self.side:
            self.neighbor[a].add(b)
            self.neighbor[b].add(a)
    def dfs(self,x,a):
        self.group[a] += 1
        self.visited[x] = True
        for i in self.neighbor[x]:
            if self.visited[i]:continue
            self.dfs(i,a)
    def all_dfs(self):
        a = 0
        for i in range(1,n+1):
            if not self.visited[i]:
                self.dfs(i,a)
                a += 1

n,m = map(int,input().split())
side = list(tuple(map(int,input().split()))for i in range(m))
graph1 = Graph(n,side)
graph1.all_dfs()
print(max(graph1.group))


Verdicts: Runtime Error

Wrong Answer

Runtime Error

Runtime Error

Accepted