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:
def main():
    n,m = [int(v) for v in input().split()]
    groups = []
    g = [-1]*(n+1)
    for _ in range(m):
        a,b = [int(v) for v in input().split()]
        if g[a]!=-1 and g[b]!=-1:
            if g[a]==g[b]: continue
            gb = g[b]
            for no in groups[gb]:
                g[no] = g[a]
            groups[gb] = {}
        elif g[a]!=-1:
            g[b] = g[a]
            groups[g[a]].append(b)
        elif g[b]!=-1:
            g[a] = g[b]
            groups[g[b]].append(a)
        else:
            g[a] = g[b] = len(groups)
            groups.append([a,b])
    # print(groups, g)
    ans = max([len(v) for v in groups])
    print(ans)

main()

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
l = [-1 for i in range (n)]
L = []
for i in range (m):
    a,b = map(int,input().split())
    if l[a-1] == l[b-1] == -1:
        L.append([a,b])
        l[a-1] = l[b-1] = len(L)-1
    elif l[a-1] == -1:
        num = l[b-1]
        L[num].append(a)
        l[a-1] = num
    elif l[b-1] == -1:
        num = l[a-1]
        L[num].append(b)
        l[b-1] = num
ANS = []
for i in range (len(L)):
    ANS.append(len(L[i]))
sorted(ANS)
print(ANS[0])

Submission status: Runtime Error

Solution code:
import networkx as nx

n, m = map(int, input().split())
pairs = [tuple(map(int, input().split())) for _ in range(m)]
graph = nx.from_edgelist(pairs)
groups = list(nx.connected_components(graph))

print(max(len(group) for group in groups))

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
ls = [0 for _ in range(N)]
flg = 1
for i in range(M):
    a,b = map(int,input().split())
    if ls[a-1] == 0 and ls[b-1]==0:
        ls[a-1]=flg
        ls[b-1]=flg
        flg+=1
    elif ls[a-1] == 0:
        ls[a-1] = ls[b-1]
    elif ls[b-1] == 0:
        ls[b-1] = ls[a-1]
    else:
        check = min(ls[a-1],ls[b-1])
        ls[a-1] = check
        ls[b-1] = check
from statistics import mode
com = mode(ls)
print(ls.count(com))

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
N,M = map(int,input().split())
count = [[0]*N for _ in range(N)]
fr = [[] for _ in range(N)]
for _ in range(M):
    a,b = map(int,input().split())
    a,b = a-1,b-1
    if count[a][b] == 1:
        continue
    count[a][b] = 1
    count[b][a] = 1
    fr[a].append(b)
    fr[b].append(a)
used = [-1]*N #-1なら未探索。数字はrenketuのindex
renketu = []
index = 0
def dfs(fr,cur,parent,index,li):
    children = fr[cur]
    for chi in children:
        if chi == parent or used[chi] != -1:
            continue
        used[chi] = index
        li.append(chi)
        dfs(fr,chi,cur,index,li)
for i in range(N):
    if used[i] != -1:
        continue
    li = [i]
    used[i] = index
    dfs(fr,i,-1,index,li)
    index += 1
    renketu.append(li)
n = len(renketu)
MAX = 0
for i in range(n):
    lii = renketu[i]
    MAX = max(len(lii),MAX)
print(MAX)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
if m == 0:
    print(1)
    exit()
lis = [i+1 for i in range(n)]
li = list(set([sorted(list(map(int,input().split()))) for i in range(m)]))
li.sort(key=lambda x:x[1])
li.sort(key=lambda x:x[0])
for i in range(len(li)):
    lis[li[i][1]-1] = lis[li[i][0]-1]
lis.sort()
ans = 0
cnt = 1
for i in range(n-1):
    if lis[i] != lis[i+1]:
        ans = max(ans,cnt)
        cnt = 1
    else:
        cnt += 1
print(max(ans,cnt))

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
l=[set() for _ in range(n+1)]*(n+1)
mn=0
for i in range(m):
  a,b=map(int,input().split())
  l[a].add(b)
  l[b].add(a)

for i in range(n):
  aa=l[i]
  for k in l[i]:
    aa=aa|l[k]
  mn=max(mn,len(aa))
print(mn)

Submission status: Runtime Error

Solution code:
def find_par(x,p):
    if x == p[x] :
        return x

    p[x] = find_par(p[x],p)
    return p[x]


def union(x,y,p,size):
    xp = find_par(x,p)
    yp= find_par(y,p)

    if xp != yp:
        p[yp] = xp
        size[xp] += size[yp]
    
    
n,m=[int(c) for c in input().split()]
p=[i for i in range(n+1)]
size =[1 for i in range(n+1)]
for i in range(m):
    a,b =[int(c) for c in input().split()]
    union(a,b,p,size)

roots= set()

for i in range(1,n+1):
    par = find_par(i,p)
    roots.add(par)

ans = float("-inf")
for pp in roots:
    ans = max(ans,size[pp])

print(ans)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
# your code goes here


v=defaultdict(list)
n,m=map(int,input().split())
for i in range(m):
    a,b=map(int,input().split())
    v[a-1].append(b-1)
    v[b-1].append(a-1)
    

	
	
	
    
    
vis=[0]*n

def dfs(node):
    vis[node]=1
    for j in v[node]:
        #print(j,v[node])
        if vis[j]==0:
            a.append(j+1)
            dfs(j)
ans=0      
for i in range(n):
    if vis[i]==0:
        a=[i]
        dfs(i)
        ans=max(ans,len(a))
    

print(ans)

Submission status: Runtime Error

Solution code:
N,M = [int(i) for i in input().split()]

groups = []

for i in range(M):
    a,b = [int(i) for i in input().split()]
    flag = True
    for j,g in enumerate(groups):
        if a in g or b in g:
            groups[j] = np.unique(np.append(g,[a,b]))
            flag = False
    if flag:
        groups.append([a,b])
        

maxMember = 0
for g in groups:
    maxMember = max(maxMember,len(g))

print(maxMember)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())

m = 2
A, B = map(int, input().split())
s = [[A, B]]
for i in range(M - 1):
    A, B = map(int, input().split())
    flag = False
    for sins in s:
        if A in sins and not B in sins:
            sins.append(B)
            flag = True
        if B in sins and not A in sins:
            sins.append(A)
            flag = True
        if len(sins) > m:
            m = len(sins)
    if not flag:
        s.append([A, B])
print(m)

Submission status: Runtime Error

Solution code:
from sys import stdin,stdout
from collections import defaultdict
# n=int(stdin.readline())
def dfs(src):
    global sz
    vis[src]=1
    sz+=1
    for neigh in g[src]:
        if not vis[neigh]:
            dfs(neigh)

n,eg=list(map(int,stdin.readline().split()))
g=defaultdict(list)
for i in range(eg):
    A,B=map(int,stdin.readline().split())
    g[A]+=[B]
    g[B]+=[A]
vis=[0]*(n+1);ans=0
for node in range(1,n+1):
    if not vis[node]:
        sz=0
        dfs(node)
        ans=max(ans,sz)
print(ans)

Submission status: Runtime Error

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

mod = 10**9 + 7
at = tuple([a[i] % mod for i in range(n)])

ans = 0
for i in range(n):
  ans += sum(at[i+1:])*at[i]
print(ans % mod)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
UF = [-1] * (N+1)
def find(x):
    global UF
    if UF[x] == -1:
        return x
    else:
        UF[x] = find(UF[x])
        return UF[x]
def union(x,y):
    global UF
    xx = find(x)
    yy = find(y)
    if UF[x] == UF[y] == -1:
        UF[y] = xx
        return
    elif xx == yy:
        return 
    else:
        UF[yy] = xx
        return 
    
for _ in range(M):
    x, y = map(int, input().split())
    union(x,y)
    
cnt = [0] * (N+1)

for i in range(1,N+1):
    z = find(i)
    if z == -1:
        continue
    cnt[z] += 1

print(max(cnt))

Submission status: Runtime Error

Solution code:
import sys
from collections import Counter

input = lambda : sys.stdin.readline().rstrip()

n, m = map(int, input().split(" "))
pairs = set([tuple(sorted(map(int, input().split(" ")))) for i in range(m)])

graph = {}
for node in range(1, n+1):
    graph[node] = []

for a, b in pairs:
    graph[a].append(b)
    graph[b].append(a)

gids = [None] * (n+1)

def get_gid(in_i, cur_gid):
    gids[in_i] = cur_gid
    kids = graph[in_i]
    for kid in kids:
        if gids[kid] is None:
            get_gid(kid, cur_gid)


cur_gid = 0
for i in range(1, n+1):
    if gids[i] is None:
        get_gid(i, cur_gid)
        cur_gid += 1

out = Counter(gids).most_common(1)[0][1]
print(out)

Submission status: Runtime Error

Solution code:
import os
import heapq
import sys,threading
import math
import operator
from collections import defaultdict
from io import BytesIO, IOBase

sys.setrecursionlimit(10 ** 5)
threading.stack_size(2**27)


def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)


"""def pw(a,b):
    result=1
    while(b>0):
        if(b%2==1): result*=a
        a*=a
        b//=2
    return result"""


def inpt():
    return [int(k) for k in input().split()]

global ct

def dfs(d,visited,s):
    global ct
    ct+=1
    visited[s]=True

    for i in d[s]:
        if(not visited[i]):
            dfs(d,visited,i)

def main():
    n,m=inpt()
    st=set()
    d=defaultdict(list)
    for _ in range(m):
        a,b=inpt()
        if((min(a,b),max(a,b)) not in st):
            st.add((min(a,b),max(a,b)))
            d[a].append(b)
            d[b].append(a)

    visited=[False]*(n+1)
    ans=0
    for i in range(1,n+1):
        global ct
        ct=0
        if(not visited[i]):
            dfs(d,visited,i)
            ans=max(ans,ct)
    print(ans)







BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == "__main__":
    main()
    #threading.Thread(target=main).start()

Submission status: Runtime Error

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
input = sys.stdin.readline
INF = float("inf")


def dfs(G, visited, v, cnt):
    visited[v] = True
    cnt += 1

    for nv in G[v]:
        if visited[nv]:
            continue
        cnt = dfs(G, visited, nv, cnt)
    
    return cnt


# 処理内容
def main():
    N, M = map(int, input().split())

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

    visited = [False] * N

    ans = 0
    for i in range(N):
        if visited[i]:
            continue
        ans = max(ans, dfs(G, visited, 0, 0))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N,M  = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(M)]

#xの木の根を求める
def root(x,li): 
    if li[x] == x:
        return x
    else:
        li[x] = root(li[x],li)
        return li[x]

#xとyが同じ集合に属するかの判定
def find(x,y,li):
    return root(x,li) == root(y,li)

#xとｙを併合
def union(x,y,li):
    x = root(x,li)
    y = root(y,li)
    if x==y:
        return
    else:
        li[x] = y

li = [i for i in range(N+20)]

for i in range(len(AB)):
    AB[i][0] -= 1
    AB[i][1] -= 1
    union(AB[i][0], AB[i][1], li)

for i in range(N):
    root(i, li)

cnt = [0 for i in range(N+20)]
for i in range(N):
    cnt[li[i]] += 1
print(max(cnt))

Submission status: Runtime Error

Solution code:
mod = 10**9 + 7
def go(v):
    global tmp
    tmp += 1
    used[v] = True
    for i in range(len(g[v])):
        if not used[g[v][i]]:
            go(g[v][i])
def solve():
    n, m = map(int, input().split())
    global g, used, tmp
    tmp = 0
    g = [[] for i in range(n + 1)]
    ans = 0
    d = {}
    used = [False for i in range(n + 1)]
    for i in range(m):
        x, y = map(int, input().split())
        if (x, y) not in d:
            g[x].append(y)
            g[y].append(x)
            d[(x, y)] = 1
    for i in range(1, n + 1):
        if not used[i]:
            tmp = 0
            go(i)
            ans = max(ans, tmp)
    print(ans)
t = 1
while t > 0:
    solve()
    t -= 1

Submission status: Runtime Error

Solution code:
import collections


class unionFind:
    parent = []

    def __init__(self, N):
        self.parent = [i for i in range(N)]

    def root(self, x):
        if self.parent[x] == x:
            return(x)
        else:
            self.parent[x] = self.root(self.parent[x])
            return(self.parent[x])

    def same(self, x, y):
        x, y = x-1, y-1
        return(self.root(x) == self.root(y))

    def unite(self, x, y):
        x, y = x-1, y-1
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            self.parent[x] = y
            return


N, M = map(int, input().split())
G = unionFind(N)
for i in range(M):
    a, b = map(int, input().split())
    G.unite(a, b)

for i in range(N):
    G.root(i)
print(1)
# C = collections.Counter(G.parent).most_common()

# print(C[0][1])

Submission status: Runtime Error

Solution code:
import bisect
import sys
import math
input = sys.stdin.readline
import functools

from collections import defaultdict

############ ---- Input Functions ---- ############

def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(list(s[:len(s) - 1]))
def invr():
    return(map(int,input().split()))

############ ---- Solution ---- ############

def diff(S, i, T):
    res = 0
    for j in range(len(T)):
        if S[i + j] != T[j]:
            res += 1
    return res

def solve():
    [N, M] = inlt()
    uf = [i for i in range(N)]

    def root(i):
        if uf[i] == i:
            return i
        res = root(uf[i])
        uf[i] = res
        return res

    def union(i, j):
        uf[root(i)] = root(j)

    for i in range(M):
        [a, b] = inlt()
        union(a - 1, b - 1)

    groups = defaultdict(int)
    res = 0
    for i in range(N):
        r = root(i)
        groups[r] += 1
        res = max(res, groups[r])
    
    return res

    

if len(sys.argv) > 1 and sys.argv[1].startswith("input"):
    f = open("./" + sys.argv[1], 'r')
    input = f.readline

res = solve()
print(str(res))

Submission status: Runtime Error

Solution code:
mod = 10**9 + 7
def go(v):
    global tmp
    tmp += 1
    used[v] = True
    for i in range(len(g[v])):
        if not used[g[v][i]]:
            go(g[v][i])
def solve():
    n, m = map(int, input().split())
    global g, used, tmp
    tmp = 0
    g = [[] for i in range(n + 1)]
    ans = 0
    used = [False for i in range(n + 1)]
    for i in range(m):
        x, y = map(int, input().split())
        if y not in g[x]:
            g[x].append(y)
            g[y].append(x)
    for i in range(1, n + 1):
        if not used[i]:
            tmp = 0
            go(i)
            ans = max(ans, tmp)
    print(ans)
t = 1
while t > 0:
    solve()
    t -= 1

Submission status: Runtime Error

Solution code:
import collections


class unionFind:
    parent = []

    def __init__(self, N):
        self.parent = [i for i in range(N)]

    def root(self, x):
        if self.parent[x] == x:
            return(x)
        else:
            self.parent[x] = self.root(self.parent[x])
            return(self.parent[x])

    def same(self, x, y):
        x, y = x-1, y-1
        return(self.root(x) == self.root(y))

    def unite(self, x, y):
        x, y = x-1, y-1
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            self.parent[x] = y
            return


N, M = map(int, input().split())
G = unionFind(N)
for i in range(M):
    a, b = map(int, input().split())
    G.unite(a, b)

for i in range(N):
    G.root(i)

C = collections.Counter(G.parent).most_common()

print(1)
# print(C[0][1])

Submission status: Runtime Error

Solution code:
N,M  = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(M)]

#xの木の根を求める
def root(x,li): 
    if li[x] == x:
        return x
    else:
        li[x] = root(li[x],li)
        return li[x]

#xとyが同じ集合に属するかの判定
def find(x,y,li):
    return root(x,li) == root(y,li)

#xとｙを併合
def union(x,y,li):
    x = root(x,li)
    y = root(y,li)
    if x==y:
        return
    else:
        li[x] = y

li = [i for i in range(N)]

for i in range(len(AB)):
    AB[i][0] -= 1
    AB[i][1] -= 1
    union(AB[i][0], AB[i][1], li)

for i in range(N):
    root(i, li)

cnt = [0 for i in range(N)]
for i in range(N):
    cnt[li[i]] += 1
print(max(cnt))

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
l = [-1 for i in range (n)]
L = []
for i in range (m):
    a,b = map(int,input().split())
    if l[a-1] == l[b-1] == -1:
        L.append([a,b])
        l[a-1] = l[b-1] = len(L)-1
    elif l[a-1] == -1:
        num = l[b-1]
        L[num].append(a)
        l[a-1] = num
    elif l[b-1] == -1:
        num = l[a-1]
        L[num].append(b)
        l[b-1] = num
sorted(L)
print(len(L[0]))

Submission status: Runtime Error

Solution code:
mod = 10**9 + 7
def go(v):
    global tmp
    tmp += 1
    used[v] = True
    for i in range(len(g[v])):
        if not used[g[v][i]]:
            go(g[v][i])
def solve():
    n, m = map(int, input().split())
    global g, used, tmp
    tmp = 0
    g = [[] for i in range(n + 1)]
    ans = 0
    used = [False for i in range(n + 1)]
    for i in range(m):
        x, y = map(int, input().split())
        if y not in x:
            g[x].append(y)
            g[y].append(x)
    for i in range(1, n + 1):
        if not used[i]:
            tmp = 0
            go(i)
            ans = max(ans, tmp)
    print(ans)
t = 1
while t > 0:
    solve()
    t -= 1

Submission status: Runtime Error

Solution code:
import sys 
sys.setrecursionlimit(100000)

class UF:
    def __init__(self, n):
        self.parent = list(range(n))
    def find(self,n):
        if self.parent[n] == n :
            return n
        self.parent[n] = self.find(self.parent[n])
        return self.parent[n]

    def union(self, i,j ):
        self.parent[self.find(i)] = self.find(j)


N, M = map(int, input().split(" "))
uf = UF(N)
for _ in range(M):
    i,j = map(lambda x: int(x)-1,input().split(" "))
    uf.union(i,j)

from collections import defaultdict
counter = defaultdict(int)
res = 0
for i in range(N): 
    counter[uf.find(i)] += 1
    res = max(res, counter[uf.find(i)])

print(res)

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

N = 10**5

*p, = range(N)
size = [1]*N
def root(x):
    if x == p[x]:
        return x
    p[x] = y = root(p[x])
    return y
def unite(x, y):
    px = root(x); py = root(y)
    if px == py:
        return 0
    sx = size[px]; sy = size[py]
    if sy < sx:
        p[py] = px
        size[px] += sy
    else:
        p[px] = py
        size[py] += sx
    return 1

n,m = na()
ans = 0

for i in range(m):
    a,b = na()
    unite(a,b)

print(max(size))

Submission status: Runtime Error

Solution code:
import sys 
sys.setrecursionlimit(60000)

class UF:
    def __init__(self, n):
        self.parent = list(range(n))
    def find(self,n):
        if self.parent[n] == n :
            return n
        self.parent[n] = self.find(self.parent[n])
        return self.parent[n]

    def union(self, i,j ):
        self.parent[self.find(i)] = self.find(j)


N, M = map(int, input().split(" "))
uf = UF(N)
for _ in range(M):
    i,j = map(lambda x: int(x)-1,input().split(" "))
    uf.union(i,j)

from collections import defaultdict
counter = defaultdict(int)
res = 0
for i in range(N): 
    counter[uf.find(i)] += 1
    res = max(res, counter[uf.find(i)])

print(res)

Submission status: Runtime Error

Solution code:
#dt = {} for i in x: dt[i] = dt.get(i,0)+1
import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

import resource,sys
resource.setrlimit(resource.RLIMIT_STACK,(2**29,-1))
sys.setrecursionlimit(10**6)

def dfs(u):
    global graph,vis
    vis.add(u)
    for i in graph[u]:
        if i not in vis:
            dfs(i)

n,m = ip()
graph = {i:[] for i in range(1,n+1)}
for i in range(m):
    a,b = ip()
    graph[a].append(b)
    graph[b].append(a)
for i in graph:
    graph[i] = list(set(graph[i]))
vis = set()
ans = -1
prev = 0
for i in range(1,n+1):
    if i not in vis:
        dfs(i)
        t = len(vis)
        ans = max(ans,t-prev)
        prev = t
print(ans)

Submission status: Runtime Error

Solution code:
import collections


class unionFind:
    parent = []

    def __init__(self, N):
        self.parent = [i for i in range(N)]

    def root(self, x):
        if self.parent[x] == x:
            return(x)
        else:
            self.parent[x] = self.root(self.parent[x])
            return(self.parent[x])

    def same(self, x, y):
        x, y = x-1, y-1
        return(self.root(x) == self.root(y))

    def unite(self, x, y):
        x, y = x-1, y-1
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            self.parent[x] = y
            return


N, M = map(int, input().split())
G = unionFind(N)
for i in range(M):
    a, b = map(int, input().split())
    G.unite(a, b)


for i in range(N):
    G.root(i)

C = collections.Counter(G.parent).most_common()


print(C[0][1])

Submission status: Runtime Error

Solution code:
from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math
from  itertools import accumulate

def get_int():return int(stdin.readline().strip())
def get_tuple():return map(int, stdin.readline().split())
def get_list():return list(map(int, stdin.readline().split()))


# Python program to print connected
# components in an undirected graph
class Graph:

    # init function to declare class variables
    def __init__(self, V):
        self.V = V
        self.adj = [[] for i in range(V)]

    def DFSUtil(self, cnt, v, visited):

        # Mark the current vertex as visited
        visited[v] = True

        # Store the vertex to list
        cnt += 1

        # Repeat for all vertices adjacent
        # to this vertex v
        for i in self.adj[v]:
            if visited[i] == False:
                # Update the list
                cnt = self.DFSUtil(cnt, i, visited)
        return cnt

    # method to add an undirected edge
    def addEdge(self, v, w):
        self.adj[v].append(w)
        self.adj[w].append(v)

    # Method to retrieve connected components
    # in an undirected graph
    def connectedComponents(self):
        visited = []
        cc = []
        for i in range(self.V):
            visited.append(False)
        for v in range(self.V):
            if visited[v] == False:
                cnt = 0
                cc.append(self.DFSUtil(cnt, v, visited))
        return cc

n,m = get_tuple()
g = Graph(n)
for _ in range(m):
    a,b = get_tuple()
    g.addEdge(a-1,b-1)
cc = g.connectedComponents()
print(max(cc))

Submission status: Runtime Error

Solution code:
mod = 10**9 + 7
def go(v):
    global tmp
    tmp += 1
    used[v] = True
    for i in range(len(g[v])):
        if not used[g[v][i]]:
            go(g[v][i])
def solve():
    n, m = map(int, input().split())
    global g, used, tmp
    tmp = 0
    g = [[] for i in range(n + 1)]
    ans = 0
    used = [False for i in range(n + 1)]
    for i in range(m):
        x, y = map(int, input().split())
        g[x].append(y)
        g[y].append(x)
    for i in range(1, n + 1):
        if not used[i]:
            tmp = 0
            go(i)
            ans = max(ans, tmp)
    print(ans)
t = 1
while t > 0:
    solve()
    t -= 1

Submission status: Runtime Error

Solution code:
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**9+7

from math import gcd

Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

n,m = Ri()
par = [i for i in range(n)]
val = [1 for i in range(n)]

def find(chi):
    if par[chi] == chi:
        return chi
    temp = find(par[chi])
    par[chi] = temp
    return temp

def union(a,b):
    pa,pb = find(a),find(b)
    par[pb] = pa
    val[pa]+=val[pb]

ncc= n
for i in range(m):
    a,b = Ri();a-=1;b-=1
    pa,pb = find(a),find(b)
    if pa != pb:
        ncc-=1
        union(a,b)

lis = []
se = set()
for i in range(n):
    p = find(i)
    if p not in se:
        se.add(p)
        lis.append(val[p])
lis.sort()
ans = 0
summ = 0
for i in range(len(lis)):
    val = (lis[i]-summ)
    ans+=val
    summ+=val
print(ans)

Submission status: Runtime Error

Solution code:
class UF:
    def __init__(self, n):
        self.parent = list(range(n))
    def find(self,n):
        if self.parent[n] == n :
            return n
        self.parent[n] = self.find(self.parent[n])
        return self.parent[n]

    def union(self, i,j ):
        self.parent[self.find(i)] = self.find(j)


N, M = map(int, input().split(" "))
uf = UF(N)
for _ in range(M):
    i,j = map(lambda x: int(x)-1,input().split(" "))
    uf.union(i,j)

from collections import defaultdict
counter = defaultdict(int)
res = 0
for i in range(N): 
    counter[uf.find(i)] += 1
    res = max(res, counter[uf.find(i)])

print(res)

Submission status: Runtime Error

Solution code:
from collections import Counter
import sys


def _ia(): return map(int, sys.stdin.readline().strip().split())


class UnionFind:
    def __init__(self, n: int) -> None:
        self._par = [i for i in range(n)]
        self._rank = [0] * n

    def root(self, n: int) -> int:
        p = self._par[n]
        if p == n:
            return p
        else:
            # compress
            self._par[n] = self.root(p)
            return self._par[n]

    def same(self, n: int, m: int) -> bool:
        return self.root(n) == self.root(m)

    def unite(self, n: int, m: int) -> None:
        pn = self.root(n)
        pm = self.root(m)
        if pn == pm:
            return
        self._par[pm] = pn


def main():
    n, m = _ia()
    uf = UnionFind(n)
    for _ in range(m):
        a, b = map(lambda x: x-1, _ia())
        uf.unite(a, b)
    c = Counter([uf.root(i) for i in range(n)])
    return c.most_common(1)[0][1]


if __name__ == "__main__":
    print(main())