Problem:


Score : 400 points

Problem Statement

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?

Constraints

2 \leq N \leq 2\times 10^5

0 \leq M \leq 2\times 10^5

1\leq A_i,B_i\leq N

A_i \neq B_i

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
\vdots
A_M B_M

Output

Print the answer.

Sample Input 1

5 3
1 2
3 4
5 1

Sample Output 1

3

Dividing them into three groups such as \{1,3\}, \{2,4\}, and \{5\} achieves the goal.

Sample Input 2

4 10
1 2
2 1
1 2
2 1
1 2
1 3
1 4
2 3
2 4
3 4

Sample Output 2

4

Sample Input 3

10 4
3 1
4 1
5 9
2 6

Sample Output 3

3



Sample input:
5 3
1 2
3 4
5 1


Sample output:
3

Submission status: Runtime Error

Solution code:
N,M = tuple(map(int,input().split()))
matchlist = [tuple(map(lambda x:int(x)-1,input().split())) for i in range(M)]
#最大のグループの大きさを出す
friendlist = [0 for i in range(N)]
gplist = [[-1]]
def match(ab):
    a,b=ab
    gpa=friendlist[a]
    gpb=friendlist[b]
    if bool(gpa) and gpa==gpb:
        return
    elif not bool(gpa) and not bool(gpb):
        friendlist[a]=len(gplist)
        friendlist[b]=len(gplist)
        gplist.append([a,b])
    elif bool(gpa) and not bool(gpb):
        friendlist[b] = gpa
        gplist[gpa].append(b)
    elif not bool(gpa) and bool(gpb):
        friendlist[a] = gpb
        gplist[gpb].append(a)
    else:
        for i in gplist[gpb]:
            friendlist[i] = gpa
        gplist[gpa].extend(gplist[gpb])
        gplist[gpb] = -1
# print(gplist)
for i in range(M):
    match(matchlist[i])
    # print(gplist)
    # print(friendlist)
print(max([len(item) for item in gplist]))

Submission status: Runtime Error

Solution code:
N,M = tuple(map(int,input().split()))
matchlist = [tuple(map(lambda x:int(x)-1,input().split())) for i in range(M)]
#最大のグループの大きさを出す
friendlist = [0 for i in range(N)]
gplist = [[]]
def match(ab):
    a,b=ab
    gpa=friendlist[a]
    gpb=friendlist[b]
    if bool(gpa) and gpa==gpb:
        return
    elif not bool(gpa) and not bool(gpb):
        friendlist[a]=len(gplist)
        friendlist[b]=len(gplist)
        gplist.append([a,b])
    elif bool(gpa) and not bool(gpb):
        friendlist[b] = gpa
        gplist[gpa].append(b)
    elif not bool(gpa) and bool(gpb):
        friendlist[a] = gpb
        gplist[gpb].append(a)
    else:
        for i in gplist[gpb]:
            friendlist[i] = gpa
        gplist[gpa].extend(gplist[gpb])
        gplist.remove(gplist[gpb])
# print(gplist)
for i in range(M):
    match(matchlist[i])
    # print(gplist)
    # print(friendlist)
print(max([len(item) for item in gplist]))

Submission status: Runtime Error

Solution code:
class UnionFind():
    def __init__(self,n):
        self.parents = [-1]*n

    def find(self,x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x,y = y,x
            
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
N,M = map(int,input().split())    
uf = UnionFind(N)

for i in range(M):
    A,B = map(int,input().split())
    uf.union(A,B)
    #print(uf.parents)

print(abs(min(uf.parents)))

Submission status: Runtime Error

Solution code:
n, m = list(map(int, input().rstrip().split(" ")))
members = [1 for i in range(n)]
max_idx = 0
for mm in range(m):
  print(members)
  a0, b0 = list(map(int, input().rstrip().split(" ")))
  a = a0
  b = b0
  while members[a-1] < 0:
    a = -members[a-1]
    members[a0-1] = -a
  while members[b-1] < 0:
    b = -members[b-1]
    members[b0-1] = -b
  if a != b:
    print(a, b, members[a-1], members[b-1])
    members[a-1] += members[b-1]
    members[b-1] = -a
    if members[a-1] > members[max_idx]:
      max_idx = a-1
print(members[max_idx])

Submission status: Runtime Error

Solution code:
n, m = list(map(int, input().rstrip().split(" ")))
members = [1 for i in range(n)]
max_idx = 0
for mm in range(m):
  a0, b0 = list(map(int, input().rstrip().split(" ")))
  a = a0
  b = b0
  while members[a-1] < 0:
    a = -members[a-1]
  while members[b-1] < 0:
    b = -members[b-1]
  members[a0-1] = -a
  members[b0-1] = -b
  if a != b:
    members[a-1] += members[b-1]
    members[b-1] = -a
    if members[a-1] > members[max_idx]:
      max_idx = a-1
print(members[max_idx])

Submission status: Runtime Error

Solution code:
def get_group_root(x):
    if parent[x] < 0:
        return x
    return get_group_root(parent[x])


[N, M] = map(int, input().split())
parent = [-1] * N  # 親の友人
for i in range(M):
    [A,B] = map(int, input().split())
    A -= 1
    B -= 1

    groupA = get_group_root(A)
    groupB = get_group_root(B)

    if groupA == groupB:
        continue
    elif parent[groupA] < parent[groupB]:
        parent[B] = A
        parent[groupA] -= 1
    else:
        parent[A] = B
        parent[groupB] -= 1

print(-min(parent))

Submission status: Runtime Error

Solution code:
from collections import Counter

class UnionFind:
    def __init__(self, N: int) -> None:
        self.par = [i for i in range(N)]
    
    def root(self, x: int) -> bool:
        if self.par[x] == x: #親が自分自身なら頂点
            return x
        else:
            self.par[x] = self.root(self.par[x])
            return self.par[x]

    def unite(self, x: int, y: int) -> None:
        rx = self.root(x)
        ry = self.root(y)
        if rx == ry:
            return
        else:
            self.par[rx] = ry
    
    def same(self, x: int, y: int) -> bool:
        return self.root(x) == self.root(y)


def main():
    N, M = map(int, input().split())
    uf = UnionFind(N)
    for _ in range(M):
        a, b = map(int, input().split())
        uf.unite(a-1, b-1)
    
    g = Counter(uf.root(x) for x in range(N))
    ans = g.most_common(1)[0][1]
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:

N, M = map(int, input().split())  # M pieces of information for N people

# if a parent, -1 * number of members
# else index of its parent
UF = [-1] * N


def root(x):
    if UF[x] < 0:
        return x
    else:
        return root(UF[x])

for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    a = root(a)
    b = root(b)

    if a == b:
        continue
    else:
        UF[a] += UF[b]
        UF[b] = a

print(-1 * min(UF))

Submission status: Runtime Error

Solution code:
import copy

def dfs(k,E,A,B):
#    print("lenA is "+str(len(A)))
    for i in range(len(A)):
#        print("i is "+str(i))
#        print("k is "+str(k))
        if i >= len(A):
#            print("damedesu")
            break
#        print("A[i] is "+str(A[i]))
#        print("B[i] is "+str(B[i]))

        if k == A[i]:
            l = B[i]
            if l in E:
#                print("dame")
            else:    
                E.append(l)
                C = copy.copy(A)
                D = copy.copy(B)
                C.pop(i)
                D.pop(i)
                dfs(l,E,C,D)
        elif k == B[i]:
            l = A[i]
            if l in E:
#                print("dame")
            else:    
                E.append(l)
                C = copy.copy(A)
                D = copy.copy(B)
                C.pop(i)
                D.pop(i)
                dfs(l,E,C,D)
    return E
            
import numpy as np
N, M = map(int, input().split())
A = []
B = []
for _ in range(M):
    AA, BB = map(int, input().split())
    A.append(AA)
    B.append(BB)
    
C = copy.copy(A)
D = copy.copy(B)
num = np.arange(1,N+1)
maxi = 0
for i in range(N):
#    print("ni is "+str(i))
#    print("lennum is "+str(len(num)))
    if i >= len(num):
#        print("damedame")
        break
    k = num[i]
    E = [k]
    a = dfs(k,E,C,D)
    maxi = max(maxi,len(E))  
#    print("maxi is "+str(maxi))      
    
print(maxi)

Submission status: Runtime Error

Solution code:
N, M = map(int,input().split())
pairs = []
for i in [0]*M:
    inp = sorted(list(map(int,input().split())))
    if inp not in pairs:
        pairs.append(inp)
def solver(N,M,pairs):
    group_list = []
    for i in pairs:
        pair = sorted(i)
        for j in range(len(group_list)):
            if pair[1] in group_list[j]:
                break
            elif pair[0] in group_list[j]:
                group_list[j].append(pair[1])
                break
        else:
            group_list.append(pair)
    return max([len(i) for i in group_list])
print(solver(N,M,pairs))

Submission status: Runtime Error

Solution code:
import collections
import sys
import resource
sys.setrecursionlimit(200000)

n, m = map(int, input().split(' '))

f = [x for x in range(n)]

def find(x):
  if f[x] == x:
    return x
  root = find(f[x])
  f[x] = root
  return root

for i in range(m):
  a, b = map(int, input().split(' '))
  a -= 1
  b -= 1
  if find(a) != find(b):
    f[find(a)] = find(b)

for i in range(n):
  find(i)
  
print(max(collections.Counter(f).values()))

Submission status: Runtime Error

Solution code:
from collections import deque

N, M = map(int, input().split())
friends = [[] for _ in range(N)]

for _ in range(M):
  a, b = map(int, input().split())
  a, b = a-1, b-1
  friends[a].append(b)
  friends[b].append(a)

check = [False] * N
ans = 0

for i in range(N):
  if check[i] == False:
    check[i] = True
    D = deque(friends[i])
    tmp = 1
    while D:
      f = D.popleft()
      if check[f] == False:
        check[f] = True
        tmp += 1
        for j in friends[j]:
          D.append(j)
    ans = max(ans, tmp)

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

N, M = map(int, input().split())
friends = [[] for _ in range(N)]

for _ in range(M):
  a, b = map(int, input().split())
  a, b = a-1, b-1
  friends[a].append(b)
  friends[b].append(a)

check = [False] * N
ans = 0

for i in range(N):
  if check[i] == False:
    check[i] = True
    D = deque(friends[i])
    tmp = 1
    while D:
      f = D.popleft()
      if check[f] == False:
        check[f] = True
        tmp += 1
        for j in friend[j]:
          D.append(j)
    ans = max(ans, tmp)

print(ans)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
g=[]
for i in range(n+10):
    g.append([])

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

ans=0 
vis=[False]*(n+10) 
#vis[1]=True
def dfs(n):
    ret=0
    if vis[n]:
        return 0
    else:
        vis[n]=True
        ret+=1 
        for i in g[n]:
            ret+=dfs(i)
        return ret
        
    


for i in range(1,n+1):
    if vis[i] is False:
        loc=dfs(i)
        ans=max(ans,loc)
print(ans)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
g=[]
for i in range(n+1):
    g.append([])

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

ans=0 
vis=[False]*(n+1) 
#vis[1]=True
def dfs(n):
    ret=0
    if vis[n]:
        return 0
    else:
        vis[n]=True
        ret+=1 
        for i in g[n]:
            ret+=dfs(i)
        return ret
        
    


for i in range(1,n+1):
    if vis[i] is False:
        loc=dfs(i)
        ans=max(ans,loc)
print(ans)

Submission status: Runtime Error

Solution code:
import collections
import sys

n, m = map(int, input().split(' '))

if m == 0:
  print(1)
  sys.exit()

f = [x for x in range(n)]

def find(x):
  if f[x] == x:
    return x
  root = find(f[x])
  f[x] = root
  return root

for i in range(m):
  a, b = map(int, input().split(' '))
  a -= 1
  b -= 1
  if find(a) != find(b):
    f[find(a)] = find(b)

for i in range(n):
  find(i)
  
print(max(collections.Counter(f).values()))

Submission status: Runtime Error

Solution code:
import collections

n, m = map(int, input().split(' '))

f = [x for x in range(n)]

def find(x):
  if f[x] == x:
    return x
  root = find(f[x])
  f[x] = root
  return root

for i in range(m):
  a, b = map(int, input().split(' '))
  a -= 1
  b -= 1
  if find(a) != find(b):
    f[find(a)] = find(b)

for i in range(n):
  find(i)
  
print(max(collections.Counter(f).values()))
print(f)

Submission status: Runtime Error

Solution code:
def readInt():
    return list(map(int, input().split()))

n, m = readInt()

root = [-1] * n

def r(x):
    # print("root[x]=",root[x])
    if root[x] < 0:
        # print("r(x)=",x)
        return x
    else:
        root[x] = r(root[x])
        # print(root[x])
        return root[x]

def unite(x, y):
    x = r(x)
    y = r(y)
    if x == y:
        return
    root[x] += root[y]
    root[y] = x
    # print(root)

def size(x):
    x = r(x)
    # print(x, root[x])
    return -root[x]

for i in range(m):
    x, y = readInt()
    x -= 1
    y -= 1
    unite(x, y)

max_size = 0

for i in range(n):
    max_size = max(max_size, size(i))

print(max_size)

Submission status: Runtime Error

Solution code:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    
    # xの属する木の根を求める
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    
    # xとyの属する集合のマージ
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
 
        if x == y:
            return
 
        if self.parents[x] > self.parents[y]:
            x, y = y, x
 
        self.parents[x] += self.parents[y]
        self.parents[y] = x
    
    # xの属する集合の大きさを返す
    def size(self, x):
        return -self.parents[self.find(x)]
    
    # xとyが同じ集合に属するかを判定
    def same(self, x, y):
        return self.find(x) == self.find(y)
    
    # xと同じグループを返す
    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]
    
    # 根ノードを返す
    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]
    
    def group_count(self):
        return len(self.roots())
 
    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}
 
    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


N, M = map(int, input().split())

union = UnionFind(N)

for _ in range(M):
    a, b = map(int, input().split())
    union.union(a-1, b-1)

ans = 0

for i in range(N):
    ans = max(union.size(i))

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
N,M = map(int,input().split())
li = np.zeros((N,N)).tolist()

for i in range(M):
    Ai, Bi =map(int,input().split())	
    li[Ai-1][Bi-1] = 1
    li[Bi-1][Ai-1] = 1

que = list()
idx = list(range(0,N)) #index list for second loop

maxC = 0

while True:
    if len(idx) == 0:
        break
    startfriend = idx.pop(0)
    que.append(startfriend)
    count = 0# firends num
    while True:
        #print(idx)
        if len(que) == 0:
            break
        num =  que.pop(0)
        #print(str(num)+"の友達探す")
        for j in reversed( range(len(idx))):
            if li[num][idx[j]] == 1 :
                #print("友達"+str(idx[j]))
                que.append(idx[j])
                idx.pop(j)
                count += 1 
        #pop tempidx
    
    #print("count:"+str(count))
    if maxC < count:
        maxC = count
     
print(maxC+1)

Submission status: Runtime Error

Solution code:
import numpy as np
N,M = map(int,input().split())
li = np.zeros((N,N)).tolist()

for i in range(M):
    Ai, Bi =map(int,input().split())	
    li[Ai-1][Bi-1] = 1
    li[Bi-1][Ai-1] = 1

que = list()
idx = list(range(0,N)) #index list for second loop

maxC = 0

while True:
    if len(idx) == 0:
        break
    startfriend = idx.pop(0)
    que.append(startfriend)
    count = 0# firends num
    while True:
        print(idx)
        if len(que) == 0:
            break
        num =  que.pop(0)
        #print(str(num)+"の友達探す")
        for j in reversed( range(len(idx))):
            if li[num][idx[j]] == 1 :
                #print("友達"+str(idx[j]))
                que.append(idx[j])
                idx.pop(j)
                count += 1 
        #pop tempidx
    
    #print("count:"+str(count))
    if maxC < count:
        maxC = count
     
print(maxC+1)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
data2 = []
for i in range(m):
    data2.append(input().split())


graph = [[] for i in range(n)]
for i in range(m):
    a, b = map(lambda z: int(z) - 1, data2[m])
    graph[a].append(b)
    graph[b].append(a)

print(graph)
size = [0 for i in range(n)]
check = [True for i in range(n)]
for i in range(n):
    if check[i]:
        tmp = 1
        stack = [i]
        check[i] = False
        while stack:
            now = stack.pop()
            size[now] = tmp
            print(size)
            tmp += 1
            for to in graph[now]:
                if check[to]:
                    check[to] = False
                    stack.append(to)
print(max(size))

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())

friendship = [0] * (n+1)

set_mark = 0
for i in range(m):
  p1,p2 = map(int,input().split())
  if friendship[p1] == 0 and friendship[p2] == 0:
    set_mark += 1
    friendship[p1] = friendship[p2] = set_mark
  elif friendship[p1] == 0 and friendship[p2] != 0:
    friendship[p1] = friendship[p2]
  elif friendship[p2] == 0 and friendship[p1] != 0:
   	friendship[p2] = friendship[p1]
    
count = {}
for mark in friendship:
  if mark != 0:
    if mark not in count:
      count[mark] = 1
    else:
   	  count[mark] += 1
    
times = list(count.values())
ans = max(times)
print(ans)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())

friendship = [0] * (n+1)

set_mark = 0
for i in range(m):
  p1,p2 = map(int,input().split())
  if friendship[p1] == 0 and friendship[p2] == 0:
    set_mark += 1
    friendship[p1] = friendship[p2] = set_mark
  elif friendship[p1] == 0 and friendship[p2] != 0:
    friendship[p1] = friendship[p2]
  elif friendship[p2] == 0 and friendship[p1] != 0:
   	friendship[p2] = friendship[p1]
    
count = {}
for mark in friendship:
  if mark != 0:
    if mark not in count:
      count[mark] = 1
    else:
   	  count[mark] += 1
    
times = list(count.values())
ans = max(times)
print(ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

N, M = map(int, input().split())
F = defaultdict(list)

max_size = 0


for _ in range(M):

    A, B = map(int, input().split())

    if B not in F[A]:
        F[A].append(B)
    if A not in F[B]:
        F[B].append(A)

def dfs(i):
    vis.append(i)
    size = 0
    for j in F[i]:
        if j not in vis:
            size += dfs(j)
    return size + 1


vis = list()

ret = 0

for i in range(N):
    if i+1 not in vis:
        size = dfs(i+1)
        if size > ret:
            ret = size

print('{}'.format(ret))

Submission status: Runtime Error

Solution code:
#ABC177
#D - Friends

N,M=map(int,input().split())

friends=[]
member=[]

for i in range(M):
  x,y=map(lambda x :int(x)-1,input().split())
  j=sorted[x,y]
  if j in friends:
    friends.append(j)
    member.extend([x,y])



member2=[i for i in set(member) if member.count(i)>1 ]

for i in member2:
  a=[k for j in friends if i in j for k in j]
  c=[j for j in friends if i not in j]
  friends=[list(set(a))]+c

group=[len(i) for i in friends]    

if len(group)==0:
  print(1)
else:
  print(max(group))

Submission status: Runtime Error

Solution code:
#ABC177
#D - Friends

N,M=map(int,input().split())

friends=[[i] for i in range(N)]
member=[]

for i in range(M):
  x,y=map(lambda x :int(x)-1,input().split())
  friends[x].append(y)
  friends[y].append(x)
  member.extend([x,y])

friends=[sorted(set(i)) for i in friends if len(i)!=1]

member=sorted(set(member))

for i in member:
  a=[]
  c=[]
  for j in range(len(friends)):
    b=friends[j]
    if i in b:
      a.extend(b)
    else:
      c.append(b)
  friends=[list(set(a))]+c
  
group=[len(i) for i in friends]    


print(max(group))

Submission status: Runtime Error

Solution code:
class UnionFind():
    def __init__(self, n):
        self._n = n
        self._table = [-1]*n

    def _root(self, x):
        stack = []
        while self._table[x] >= 0:
            stack.append(x)
            x = self._table[x]
        for y in stack:
            self._table[y] = x
        return x
    
    def unite(self, x, y):
        x, y = self._root(x), self._root(y)
        if x == y:
            return
        if x > y:
            x, y = y, x
        self._table[x] += self._table[y]
        self._table[y] = x
    
    def same(self, x, y):
        return self._root(x) == self._root(y)

    def count_members(self, x):
        return -self._table[self._root(x)]
    
    def count_groups(self):
        return len({self._root(i) for i in range(self._n)})
    
    def __str__(self):
        return str([self._root(i) for i in range(self._n)])
    
    def __repr__(self):           
        return repr([self._root(i) for i in range(self._n)])

n, m, *AB = map(int, open(0).read().split())
uf = UnionFind(n)
for a, b in zip(AB[::2], AB[1::2]):
    uf.unite(a, b)
s = 0
for group in {uf._root(i) for i in range(n)}:
    s += uf.count_members(group)-1
print(s)

Submission status: Runtime Error

Solution code:
from collections import defaultdict

g=defaultdict(set)
n,m=map(int,input().split())
count=n
visited = [True]*(n+1)
visited1=[False]*(n+1)
for i in range(m):
    u,v=map(int,input().split())
    visited[u]=False
    visited[v]=False
    if visited1[u]==False:
        visited1[u]=True
        count-=1
    if visited1[v]==False:
        visited1[v] = True
        count -= 1
    g[u].add(v)
    g[v].add(u)
def dfs(node):
    lst=[]
    queue=[]
    queue.append(node)
    while len(queue):
        node = queue.pop(0)
        if visited[node]:
            continue
        visited[node]=True
        lst.append(node)
        for i in g[node]:
            if not visited[i]:
                queue.append(i)
from collections import defaultdict

g=defaultdict(set)
n,m=map(int,input().split())
count=n
visited = [True]*(n+1)
visited1=[False]*(n+1)
for i in range(m):
    u,v=map(int,input().split())
    visited[u]=False
    visited[v]=False
    if visited1[u]==False:
        visited1[u]=True
        count-=1
    if visited1[v]==False:
        visited1[v] = True
        count -= 1
    g[u].add(v)
    g[v].add(u)
def dfs(node):
    lst=[]
    queue=[]
    queue.append(node)
    while len(queue):
        node = queue.pop(0)
        if visited[node]:
            continue
        visited[node]=True
        lst.append(node)
        for i in g[node]:
            if not visited[i]:
                queue.append(i)

    return len(lst)

k=dfs(1)

for i in range(1,len(visited)):
    if not visited[i]:
        m=dfs(i)
        if m>k:
            k=m

print(max(count,k))


    return len(lst)

k=dfs(1)

for i in range(1,len(visited)):
    if not visited[i]:
        m=dfs(i)
        if m>k:
            k=m

print(max(count,k))

Submission status: Runtime Error

Solution code:
class union:
  def __init__(self, box):
    self.r = []
    for i in range(box):
      self.r.append(-1)
    
  def root(self, x):
    if self.r[x] < 0:
      return x
    return self.r[x] = root(self.r[x])

  def add(self, a, b):
    a = root(a)
    b = root(b)
    if a == b:
      return False
    if self.r[a] > self.r[b]:
      a, b = b, a
    self.r[a] += self.r[b]
    self.r[b] = a
    return True
  
  def size(self, x):
    return self.r[x]

n, m = map(int, input().split())
uf = union(m)
ans = 0
for i in range(m):
  uf.add(map(int, input().split()))

for i in range(n):
  ans = max(ans, uf.size(i))
print(ans)

Submission status: Runtime Error

Solution code:
class UnionFind:
    """素集合を木構造として管理する"""
    def __init__(self, n):
        self.parent = [-1] * n
        self.cnt = n

    def root(self, x):
        """要素xの根を求める"""
        if self.parent[x] < 0:
            return x
        else:
            self.parent[x] = self.root(self.parent[x])
            return self.parent[x]

    def merge(self, x, y):
        """要素xを含む集合と要素yを含む集合を統合する"""
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        if self.parent[x] > self.parent[y]:
            x, y = y, x
        self.parent[x] += self.parent[y]
        self.parent[y] = x
        self.cnt -= 1

    def is_same(self, x, y):
        """要素x, yが同じ集合に属するかどうかを求める"""
        return self.root(x) == self.root(y)

    def get_size(self, x):
        """要素xを含む集合の要素数を求める"""
        return -self.parent[self.root(x)]

    def get_cnt(self):
        """集合の個数を求める"""
        return self.cnt


n, m = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(m)]


uf = UnionFind(n)
for u, v in edges:
    u -= 1
    v -= 1
    uf.merge(u, v)

ans = 0
for i in range(n):
    ans = max(uf.get_size(u), ans)
print(ans)

Submission status: Runtime Error

Solution code:
import sys, io, os, re
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from pprint import pprint
from math import sin, cos, pi, radians, sqrt, floor, ceil
from copy import copy, deepcopy
from collections import deque, defaultdict
from fractions import gcd
from functools import reduce
from itertools import groupby, combinations
from heapq import heapify, heappush, heappop

#sys.setrecursionlimit(30000)

n1 = lambda: int(sys.stdin.readline().strip())
nn = lambda: list(map(int, sys.stdin.readline().strip().split()))
f1 = lambda: float(sys.stdin.readline().strip())
fn = lambda: list(map(float, sys.stdin.readline().strip().split()))
s1 = lambda: sys.stdin.readline().strip()
sn = lambda: list(sys.stdin.readline().strip().split())
nl = lambda n: [n1() for _ in range(n)]
fl = lambda n: [f1() for _ in range(n)]
sl = lambda n: [s1() for _ in range(n)]
nm = lambda n: [nn() for _ in range(n)]
fm = lambda n: [fn() for _ in range(n)]
sm = lambda n: [sn() for _ in range(n)]

def array1(n, d=0): return [d] * n
def array2(n, m, d=0): return [[d] * m for x in range(n)]
def array3(n, m, l, d=0): return [[[d] * l for y in xrange(m)] for x in xrange(n)]
def linc(A, d=1): return list(map(lambda x: x + d, A))
def ldec(A, d=1): return list(map(lambda x: x - d, A))

N, M = nn()
T = nm(M)

for i in range(M):
    T[i][0] -= 1
    T[i][1] -= 1

dic = defaultdict(list)

#m = {}
for i in range(M):
    #if (T[i][0], T[i][1]) in m:
    #    continue
    m[(T[i][0], T[i][1])] = True
    dic[T[i][0]].append(T[i][1])
    dic[T[i][1]].append(T[i][0])

def search(n):
    ret = 1
    deq = deque()

    memo[n] = 1
    for t in dic[n]:
        if memo[t] == 0:
            memo[t] = 1
            deq.append(t)

    while len(deq) != 0:
        d = deq.pop()
        ret += 1
        for t in dic[d]:
            if memo[t] == 0:
                memo[t] = 1
                deq.append(t)

    return ret

memo = array1(N)

ans = 0
for i in range(N):
    if memo[i] == 1:
        continue

    ans = max(search(i), ans)

print(ans)

Submission status: Runtime Error

Solution code:
import sys, io, os, re
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from pprint import pprint
from math import sin, cos, pi, radians, sqrt, floor, ceil
from copy import copy, deepcopy
from collections import deque, defaultdict
from fractions import gcd
from functools import reduce
from itertools import groupby, combinations
from heapq import heapify, heappush, heappop

sys.setrecursionlimit(30000)

n1 = lambda: int(sys.stdin.readline().strip())
nn = lambda: list(map(int, sys.stdin.readline().strip().split()))
f1 = lambda: float(sys.stdin.readline().strip())
fn = lambda: list(map(float, sys.stdin.readline().strip().split()))
s1 = lambda: sys.stdin.readline().strip()
sn = lambda: list(sys.stdin.readline().strip().split())
nl = lambda n: [n1() for _ in range(n)]
fl = lambda n: [f1() for _ in range(n)]
sl = lambda n: [s1() for _ in range(n)]
nm = lambda n: [nn() for _ in range(n)]
fm = lambda n: [fn() for _ in range(n)]
sm = lambda n: [sn() for _ in range(n)]

def array1(n, d=0): return [d] * n
def array2(n, m, d=0): return [[d] * m for x in range(n)]
def array3(n, m, l, d=0): return [[[d] * l for y in xrange(m)] for x in xrange(n)]
def linc(A, d=1): return list(map(lambda x: x + d, A))
def ldec(A, d=1): return list(map(lambda x: x - d, A))

N, M = nn()
T = nm(M)

for i in range(M):
    T[i][0] -= 1
    T[i][1] -= 1

dic = defaultdict(list)

for i in range(M):
    dic[T[i][0]].append(T[i][1])
    dic[T[i][1]].append(T[i][0])

def search(n):
    ret = 1
    memo[n] = 1

    for d in dic[n]:
        if memo[d] == 0:
            ret += search(d)

    return ret

memo = array1(N)

ans = 0
for i in range(N):
    if memo[i] == 1:
        continue

    tmp = search(i)
    if tmp > ans:
        ans = tmp

print(ans)

Submission status: Runtime Error

Solution code:
import sys, io, os, re
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from pprint import pprint
from math import sin, cos, pi, radians, sqrt, floor, ceil
from copy import copy, deepcopy
from collections import deque, defaultdict
from fractions import gcd
from functools import reduce
from itertools import groupby, combinations
from heapq import heapify, heappush, heappop

sys.setrecursionlimit(21000)

n1 = lambda: int(sys.stdin.readline().strip())
nn = lambda: list(map(int, sys.stdin.readline().strip().split()))
f1 = lambda: float(sys.stdin.readline().strip())
fn = lambda: list(map(float, sys.stdin.readline().strip().split()))
s1 = lambda: sys.stdin.readline().strip()
sn = lambda: list(sys.stdin.readline().strip().split())
nl = lambda n: [n1() for _ in range(n)]
fl = lambda n: [f1() for _ in range(n)]
sl = lambda n: [s1() for _ in range(n)]
nm = lambda n: [nn() for _ in range(n)]
fm = lambda n: [fn() for _ in range(n)]
sm = lambda n: [sn() for _ in range(n)]

def array1(n, d=0): return [d] * n
def array2(n, m, d=0): return [[d] * m for x in range(n)]
def array3(n, m, l, d=0): return [[[d] * l for y in xrange(m)] for x in xrange(n)]
def linc(A, d=1): return list(map(lambda x: x + d, A))
def ldec(A, d=1): return list(map(lambda x: x - d, A))

N, M = nn()
T = nm(M)

for i in range(M):
    T[i][0] -= 1
    T[i][1] -= 1

dic = defaultdict(list)

for i in range(M):
    dic[T[i][0]].append(T[i][1])
    dic[T[i][1]].append(T[i][0])

def search(n):
    ret = 1
    memo[n] = 1

    for d in dic[n]:
        if memo[d] == 0:
            ret += search(d)

    return ret

memo = array1(N)

ans = 0
for i in range(N):
    if memo[i] == 1:
        continue

    tmp = search(i)
    if tmp > ans:
        ans = tmp

print(ans)