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

Solution (Python):
class UF:
    
    def __init__(self,n):
        self.n = n
        self.parents = [-1] * (n+1) 
        self.rnks = [0] * (n+1)

    def find(self,n):
        if self.parents[n] < 0:
            return n
        else:
            return self.find(self.parents[n])


    def union(self,a,b):
        a_par = self.find(a)
        b_par = self.find(b)

        if a_par == b_par:
            return
        
        
        
        
        
        if self.size(a_par) < self.size(b_par): 
            a_par,b_par = b_par,a_par
        self.parents[a_par] += self.parents[b_par]
        self.parents[b_par] = a_par

    def size(self,n):
        par = self.find(n)
        return abs(self.parents[par])

    def is_same(self,a,b):
        return self.find(a) == self.find(b)

def main():
    n,m = map(int,input().split())
    uf = UF(n)
    for i in range(m):
        a,b = map(int,input().split())
        uf.union(a,b)
    mx = -1
    for i in range(n):
        mx = max(uf.size(i),mx)
    print(mx)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict

from heapq import heappop ,heappush
def bfs(k):
    
    q=[k]
    vis[k]=1
    
    while len(q):
        
        
        curr=heappop(q)
        p.append(curr+1)
        
        for c in v[curr+1]:
            
            if vis[c-1]==0:
                a.append(c)
                
                heappush(q,c-1)
                vis[c-1]=1
            
    
n,m=map(int,input().split())
v=defaultdict(list)
vis=[0]*n
p=[]

for i in range(m):
    a,b=map(int,input().split())
    v[a].append(b)
    v[b].append(a)
ans=0      
for i in range(n):
    if vis[i]==0:
        a=[i+1]
        bfs(i)
        ans=max(ans,len(a))
        
    

print(ans)

Solution (Python):
N,M=map(int,input().split())
x=[list(map(int,input().split())) for i in range(M)]

class UnionFind():
    def __init__(self, n):
        self.n = 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

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    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())

uf=UnionFind(N)
for i in range(M):
    uf.union(x[i][0]-1,x[i][1]-1)

ans=0
for i in uf.parents:
    gsize=uf.size(i)
    ans=max(gsize,ans)

print(ans)

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

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


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

        if xp != yp:
            p[yp] = xp
            size[xp] += size[yp]
    except:
        pass
    
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:
    try:
        ans = max(ans,size[pp])
    except:
        pass

print(ans)

Solution (Python):
class UnionFind():
    def __init__(self, n):
        self.n = 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

    def size(self, x):
        return -self.parents[self.find(x)]


N, M = map(int, input().split())
Groups = UnionFind(N)
for i in range(M):
    a, b = map(int, input().split())
    Groups.union(a-1, b-1)

ans = 0
for i in range(N):
    ans = max(Groups.size(i), ans)

print(ans)

Solution (Python):


import sys
import math
import time

import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq
import itertools


class UnionFind():
    def __init__(self, n):
        self.n = 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

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    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())


sys.setrecursionlimit(10**7)






N, M = map(int, input().split())
AB =  [list(map(int, input().split())) for _ in range(M)]
ans = 0
uf = UnionFind(N)

for i in range(M):
    uf.union(AB[i][0]-1, AB[i][1]-1)

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

print(ans)

Solution (Python):
from collections import Counter
class UnionFind():
    def __init__(self, n):
        self.n = 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 unite(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
    
    def size(self, x):
        return -self.parents[self.find(x)]
        
    def same(self, x, y):
        return self.find(x) == self.find(y)
    
    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 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())
def main():
    n,m = map(int,input().split())
    U = UnionFind(n)
    for _ in range(m):
        a,b = map(int,input().split())
        U.unite(a-1,b-1)
    
    d = Counter()
    for i in range(n):
        d[U.find(i)]+=1
    
    print(max(d.values()))
main()

Solution (Python):

import sys,math,collections,itertools
input = sys.stdin.readline

N,M=list(map(int,input().split()))
frend=[[] for _ in range(N+1)]
for _ in range(M):
    A,B=list(map(int,input().split()))
    frend[A].append(B)
    frend[B].append(A)


group=[0]*(N+1)
q = collections.deque([])
no_seen = set(range(1,N+1))
No=0
while no_seen:
    q.append(no_seen.pop())
    No += 1
    while q:
        now = q.popleft()
        group[now]=No
        for member in frend[now]:
            if member in no_seen:
                q.append(member)
                no_seen.discard(member)

gc=collections.Counter(group)
print(gc.most_common()[0][1])

Solution (Python):
N, M = map(int, input().split())
friends = [set() for _ in range(N)]
for __ in range(M):
    a, b = map(int, input().split())
    if b-1 not in friends[a-1]:
        friends[a-1] |= {b-1}
    if a-1 not in friends[b-1]:
        friends[b-1] |= {a-1}
check = [0 for _ in range(N)]
ans = 0
for i in range(N):
    if check[i]:
        continue
    count = 1
    next = [i]
    check[i] = 1
    while next:
        fri = next.pop()
        for F in friends[fri]:
            if check[F]:
                continue
            count += 1
            check[F] = 1
            next.append(F)
    ans = max(ans, count)
print(ans)

Solution (Python):
class UnionFind():
    def __init__(self, n):
        self.n = 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

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    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())
uf = UnionFind(N)
for i in range(M):
  a,b = map(int, input().split())
  uf.union(a-1,b-1)
res=-1
for i in range(N):
  res = max(res,uf.size(i))
print(res)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)

def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')







class UnionFind():
    def __init__(self, n):
        self.n = 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

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    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())

def solve():
    N, M = MI()

    uf = UnionFind(N)
    for i in range(M):
        a, b = MI1()
        uf.union(a, b)
    
    tmp = []
    
    for r in uf.roots():
        
        tmp.append(uf.size(r))
    s = sorted(tmp, reverse=True)
    
    if len(s) == 1:
        print(s[0])
    else:
        print(s[1] + s[0] - s[1])


if __name__ == '__main__':
    solve()

Solution (Python):
from sys import stdin
N,M = map(int,input().split())
F = [list(map(int,stdin.readline().split())) for i in [0]*M]
G = [[] for i in [0]*(N+1)]
for i in F:
    G[i[0]].append(i[1])
    G[i[1]].append(i[0])

def main(n,m,g):
    visited = [False for i in [0]*(n+1)]
    ans = 1
    for i in range(n+1):
        if visited[i]:
            continue
        q = [i]
        temp = 1
        while q:
            
            
            node = q.pop(0)
            visited[node] = True
            adj = g[node]
            for j in adj:
                if visited[j]:
                    continue
                visited[j] = True
                temp += 1
                q.append(j)
        ans = max(ans,temp)
    print(ans)

if __name__=="__main__":
    main(N,M,G)

Solution (Python):




import sys
from io import StringIO
import unittest

class UnionFind():
    
    
    def __init__(self, n):
        self.n = n
        
        
        self.root = [-1]*(n+1)
        
        self.rnk = [0]*(n+1)

    
    def Find_Root(self, x):
        if(self.root[x] < 0):
            return x
        else:
            
            self.root[x] = self.Find_Root(self.root[x])
            return self.root[x]
    
    def Unite(self, x, y):
        
        x = self.Find_Root(x)
        y = self.Find_Root(y)
        
        if(x == y):
            return 
        
        elif(self.rnk[x] > self.rnk[y]):
            self.root[x] += self.root[y]
            self.root[y] = x

        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            
            if(self.rnk[x] == self.rnk[y]):
                self.rnk[y] += 1
    
    def isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)

    
    def Count(self, x):
        return -self.root[self.Find_Root(x)]

def resolve():
    int1 = lambda x: int(x) - 1
    readline=sys.stdin.readline

    n,m=map(int, readline().strip().split())

    uf = UnionFind(n)

    tb=[0]*n

    for i in range(m):
        a,b=map(int1, readline().strip().split())
        uf.Unite(a,b)

    for i in range(n):
        tb[uf.Find_Root(i)]+=1
    print(max(tb))
if 'doTest' not in globals():
    resolve()
    sys.exit()

Solution (Python):
class UnionFind():
    def __init__(self, n):
        self.n = 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

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    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]


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

L=set(uf.roots())
ans=0
for k in L:
    ans=max(ans,uf.size(k))
print(ans)

Solution (Python):
from collections import deque
n, m = map(int, input().split())
fiend_memo = [[] for _ in range(n + 1)]
for _ in range(m):
    a, b = map(int, input().split())
    fiend_memo[a].append(b)
    fiend_memo[b].append(a)

make_group = []
ans = 0
d = deque()
d.append(1)
seen = {}

for person in range(1, n + 1):
    cnt = 0
    if person not in seen.keys():
        d.append(person)
        seen[person] = 1
        cnt += 1
    while d:
        now_person = d.popleft()
        for next_person in fiend_memo[now_person]:
            if next_person not in seen.keys():
                d.append(next_person)
                seen[next_person] = 1
                cnt += 1
    ans = max(ans, cnt)
print(ans)

Solution (Python):
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]

def unite(x,y):
    x, y = find(x), find(y)
    if x == y:
        return False
    
    if par[x] > par[y]:
        x,y = y,x
    par[x] += par[y]
    par[y] = x
    return True

def same(x,y): 
    return find(x) == find(y)

def size(x): 
    return -par[find(x)]
n, m = map(int, input().split())
par = [-1]*n

for _ in range(m):
    a, b = map(int, input().split())
    unite(a-1, b-1)
par = [-i for i in par]
print(max(par))

Solution (Python):
import sys
import math
import collections
import bisect
import itertools
import decimal




INF = 10 ** 20
MOD = 10 ** 9 + 7


ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))




def main():
    n, m = ns()

    e = [[] for _ in range(n)]

    for _ in range(m):
        a, b = ns()
        a, b = a - 1, b - 1
        e[a].append(b)
        e[b].append(a)

    visited = [False for _ in range(n)]

    ans = 0
    for i in range(n):
        if not visited[i]:
            visited[i] = True
            cnt = 1
            que = collections.deque()
            que.append(i)

            while que:
                q = que.popleft()

                for ei in e[q]:
                    if not visited[ei]:
                        visited[ei] = True
                        cnt += 1
                        que.append(ei)

            ans = max(ans, cnt)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

n,m=map(int, input().split())
uf1=UnionFind(n)
k=set()
for i in range(m):
    a,b=map(int, input().split())
    a,b=a-1,b-1
    if a>b:
        a,b=b,a
    k.add((a,b))
for a,b in k:
    uf1.union(a,b)

for i in range(n):uf1.find(i)
from collections import *
ans=Counter(uf1.par)

print(ans.most_common()[0][1])

Solution (Python):






class DisjointSetForest(object):  
    def __init__(self, iterable=(), **kwarg):
        
        super(DisjointSetForest, self).__init__(**kwarg)
        self._p = {}
        self._ranks = {}

        for x in iterable: self.make_set(x)

    def make_set(self, x):
        assert x not in self._p
        self._p[x] = x
        self._ranks[x] = 0

    def find(self, x):
        if self._p[x] != x:
            self._p[x] = self.find(self._p[x])
        return self._p[x]

    def union(self, x, y):
        xr, yr = self.find(x), self.find(y)

        if self._ranks[xr] > self._ranks[yr]:
            self._p[yr] = xr
        else:
            self._p[xr] = yr
            if self._ranks[xr] == self._ranks[yr]:
                self._ranks[yr] += 1





import sys
def input(itr=iter(sys.stdin.readlines())):  
    return next(itr).rstrip('\n\r')


import collections

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

dsf = DisjointSetForest(range(N))
for _ in range(M):
    A, B = map(int, input().split())
    dsf.union(A-1, B-1)

counts = collections.Counter(dsf.find(u) for u in range(N))
print(max(counts.values()))

Solution (Python):

class UnionFind():
    def __init__(self, n):
        self.n = 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
    def size(self, x):
        return -self.parents[self.find(x)]
    def same(self, x, y):
        return self.find(x) == self.find(y)
    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())
uf = UnionFind(N)
ans = 0
if M == 0:
    print(1)
    exit()
for i in range(M):
    A, B = map(int, input().split())
    uf.union(A-1,B-1)
    ans = max(ans,uf.size(A-1))
print(ans)

Solution (Python):
class UnionFind():
    def __init__(self, n):
        self.n = 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

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    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())
uf = UnionFind(n)
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    uf.union(a, b)
ans = 0    
for i in range(n):
    ans = max(uf.size(i), ans)
print(ans)

Solution (Python):
from collections import deque

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

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

adj = [[] for _ in range(n+1)]

for a, b in ab:
    adj[a].append(b)
    adj[b].append(a)

visited = [False] * (n+1)

d = 0

for i in range(1, n+1):
    if not visited[i]:
        q = deque([i])
        c = 0
        while len(q) > 0:
            v = q.pop()
            c += 1
            visited[v] = True
            for w in adj[v]:
                if not visited[w]:
                    q.append(w)
                    visited[w] = True
        d = max(d, c)

print(d)

Solution (Python):
n,m=map(int,input().split())
par=[i for i in range(n)] 
rank=[1]*n 
size=[1]*n 

def find(x):
  if par[x]==x:
    return x
  else:
    return find(par[x])
  

def unite(x,y):
  x=find(x)
  y=find(y)
  if x==y:
    return
  if rank[x]<rank[y]:
    par[x]=y
    size[y]=size[y]+size[x]
  else:
    par[y]=x
    size[x]=size[x]+size[y]
    if rank[x]==rank[y]:
      rank[x]+=1
      

def same(x,y):
  return find(x)==find(y)


for i in range(m):
  a,b=map(int,input().split())
  unite(a-1,b-1)
  
ans=0

for i in range(n):
  ans=max(ans,size[find(i)])
print(ans)

Solution (Python):
import queue

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

fri=[[] for i in range(n)]

for i in range(m):
  ai,bi=map(int,input().split())
  fri[ai-1].append(bi-1)
  fri[bi-1].append(ai-1)

seen=[0 for i in range(n)]



ans=0
for i in range(n):
  if seen[i]==1:
    continue
  cnt=1
  q=queue.Queue()
  q.put(i)
  seen[i]=1
  while not q.empty():
    p=q.get()
    for pnext in fri[p]:
      if seen[pnext]==0:
        q.put(pnext)
        seen[pnext]=1
        cnt+=1
  ans=max(ans,cnt)

print(ans)

Solution (Python):
from collections import defaultdict

N, M = map(int, input().split())
pair = defaultdict(set)
for _ in range(M):
    a, b = map(int, input().split())
    pair[a].add(b)
    pair[b].add(a)

Q = []
visited = set()
max_group = 0
for i in range(N):
    if i not in visited:
        count = 1
        Q.append(i)
        visited.add(i)
        while Q:
            x = Q.pop()
            for friend in pair[x]:
                if friend not in visited:
                    Q.append(friend)
                    visited.add(friend)
                    count += 1
        if max_group < count:
            max_group = count
print(max_group)

Solution (Python):
from collections import deque

n,m = map(int,input().split())
ab = [[] for i in range(n)]

for i in range(m):
    a,b = map(int,input().split())
    ab[a-1].append(b-1)
    ab[b-1].append(a-1)
    
chk = [0]*n

cnt = 1

for i in range(n):
    if chk[i] == 0:
        chk[i] = cnt
        q = deque()
        q.append(i)
        while len(q) > 0:
            tmp = q.popleft()
            for j in range(len(ab[tmp])):
                if chk[ab[tmp][j]] == 0:
                    q.append(ab[tmp][j])
                    chk[ab[tmp][j]] = cnt
                    
    cnt += 1

li = [0]*(n+2)



for i in range(n):
    li[chk[i]] += 1
    
print(max(li))

Solution (Python):
class UnionFind():
    def __init__(self, n):
        self.n = 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

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    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())
uf = UnionFind(N)
for _ in range(M):
    a, b = map(int, input().split())
    uf.union(a-1, b-1)
    
ans = 0
for i in range(N):
    ans = max(uf.size(i), ans)
    
print(ans)

Solution (Python):
import sys



class UnionFind:
    def __init__(self, n: int):
        self.nodes = n
        self.parents = [-1] * n
        self.rank = [0] * 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 unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.parents[y] += self.parents[x]
            self.parents[x] = y
        else:
            self.parents[x] += self.parents[y]
            self.parents[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    
    def get_size(self, x):
        return -self.parents[self.find(x)]

    
    def same(self, x, y):
        return self.find(x) == self.find(y)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n, m = input_int_list()
    union = UnionFind(n + 1)
    for _ in range(m):
        a, b = input_int_list()
        union.unite(a, b)

    ans = min(union.parents) * (-1)
    print(ans)

    return


if __name__ == "__main__":
    main()

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

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

    def find(self, x):
        '''
        要素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が属するグループと要素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

    def size(self, x):
        '''
        要素xが属するグループのサイズ（要素数）を返す
        '''
        return -self.parents[self.find(x)]

    def same(self, x, y):
        '''
        要素x, yが同じグループに属するかどうかを返す
        '''
        return self.find(x) == self.find(y)

    def members(self, x):
        '''
        要素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())


G = UnionFind(N)

for i in range(M):
    A, B = map(int, input().split())
    A, B = A-1, B-1
    G.union(A, B)

ans = 0
for r in G.roots():
    ans = max(ans, G.size(r))
print(ans)