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: Accepted

Solution code:
from collections import Counter

class UnionFind:
    def __init__(self,num:int):
        self.r = [-1 for i in range(num)]
    def root(self,x:int):
        if(self.r[x] < 0):
            return x
        self.r[x] = self.root(self.r[x])
        return self.r[x]
    def unite(self,x:int,y:int):
        rx = self.root(x)
        ry = self.root(y)
        if(rx == ry):
            return False
        if(self.r[rx] > self.r[ry]):
            rx,ry = ry,rx
        self.r[rx] += self.r[ry]
        self.r[ry] = rx
        return True
    def size(self,x:int):
        return -self.r[self.root(x)]

N,M = map(int,input().split())
group = UnionFind(N)
for i in range(M):
    A,B = map(int,input().split())
    group.unite(A-1,B-1)
ans = 0
for i in range(N):
    ans = max(ans,group.size(i))
print(ans)

Submission status: Accepted

Solution code:
#Union Find

#xの根を求める
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
#        print(par)
        return par[x]
#xとyの属する集合の併合
def unite(x,y):
    x = find(x)
    y = find(y)
    
    if x == y:
        return False
    else:
        #sizeの大きいほうがx
        if par[x] > par[y]:
            x,y = y,x
        par[x] += par[y]
        par[y] = x
        return True

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

#xが属する集合の個数
def size(x):
    return -par[find(x)]


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

#初期化
#根なら-size,子なら親の頂点
par = [-1]*N

for i in range(M):
    a, b = map(int, input().split())
    unite(a-1, b-1)
#    print(par)

#print()
group = set([])

for i in range(N):
    group.add(find(i))

#print(group)
    
ans = 0
for g in group:
#    print(size(g))
    ans = max(ans, size(g))
print(ans)

Submission status: Accepted

Solution code:
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):
    x,y = map(int,input().split())
    uf.union(x-1,y-1)

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
groups = [x for x in range(n)]
count = [0]*n

def UnionFind(x):
    a = x
    while a != groups[a]:
        a = groups[a]
    groups[x] = a
    return a

for _ in range(m):
    x, y = map(int, input().split())
    x, y = UnionFind(x-1), UnionFind(y-1)
    if x != y:
        groups[y] = x
    
for i in range(n):
    count[UnionFind(i)] += 1
print(max(count))

Submission status: Accepted

Solution code:
from networkx import*
_,*n=map(str.split,open(0))
print(max(*map(len,[*connected_components(Graph(n))]),1,1))

Submission status: Accepted

Solution code:
from collections import Counter


class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n + 1)]
        self.rank = [0] * (n + 1)
        self.size = [1] * (n + 1)

    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:
            return

        if self.par[x] > self.par[y]:
            x, y = y, x

        self.par[y] = x
        self.size[x] += self.size[y]
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1

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


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

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

ans = max(uf.size)

print(ans)

Submission status: Accepted

Solution code:
n,m = list(map(int, input().split()))
parents = [-1] * (n + 1)
def find(x):
    if parents[x] < 0:
        return x
    else:
        parents[x] = find(parents[x])
        return parents[x]

def union(x, y):
    x = find(x)
    y = find(y)

    if x == y:
        return

    if parents[x] > parents[y]:
        x, y = y, x

    parents[x] += parents[y]
    parents[y] = x

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

for _ in range(m):
    a,b = list(map(int, input().split()))    
    union(a,b)

print(-min(parents[1:]))

Submission status: Accepted

Solution code:
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(ans,uf.size(i))
print(ans)

Submission status: Accepted

Solution code:
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 program():
    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)

    ans = max([uf.size(i) for i in range(N)])
    return ans


print(program())

Submission status: Accepted

Solution code:
#n = int(input())
import random
import sys
sys.setrecursionlimit(10 ** 7)
n, m = map(int, input().split())
#n=200000
#m=200000
#al = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
adjl = [[] for _ in range(n+1)]
NIL = -1
appear = {}
isalone = {}
for i in range(m):
    a, b = map(int, input().split())
    #a=random.randrange(1,n+1)
    #b=random.randrange(1,n+1)
    #while a==b:
    #    b=random.randrange(1,n+1)
    #print(a,b)
    if appear.get((a, b), False) or appear.get((a, b), False):
        continue
    appear[(a, b)] = True
    appear[(b, a)] = True
    isalone[a] = False
    isalone[b] = False
    adjl[a].append(b)
    adjl[b].append(a)

color = [NIL for i in range(n+1)]
color_id = 0


def dfs(u, color_id):
    global color
    color[u] = color_id
    for v in adjl[u]:
        if color[v] == NIL:  # 未到達なら再起呼び出し
            color[v] = color_id
            dfs(v, color_id)


for u in range(1, n+1):
    if color[u] == NIL:
        color_id += 1
        dfs(u, color_id)

group = {}
for i in range(1, n+1):
    if isalone.get(i, True):
        continue
    group[color[i]] = group.get(color[i], 0)+1

mx = 1
for k, v in group.items():
    mx = max(mx, v)

print(mx)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
par=[i for i in range(N+1)]
size=[1 for i in range(N+1)]
def find(x):
    if x!=par[x]:
        par[x]=find(par[x])
    return par[x]
def union(x,y):
    if find(x)!=find(y):
        x, y = par[x], par[y]
        par[y] = par[x]
        size[x] += size[y]
res=N
for i in range(M):
    s,e=map(int,input().split())
    union(s,e)
print(max(size))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
par=[-1]*n

def find(n):
    if par[n]<0:
        return n
    par[n]=find(par[n])
    return par[n]

def union(x,y):
    x,y=find(x),find(y)
    if x!=y:
        if x>y:
            x,y=y,x
        par[x]+=par[y]
        par[y]=x

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

print(-min(par))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-



class UnionFind():

    # https://note.nkmk.me/python-union-find/

    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)
# print(uf)
for i in range(m):
  # print("---")
  a, b = map(int, input().split())
  uf.union(a-1, b-1)
  # print(uf)

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

print(ans)

Submission status: Accepted

Solution code:
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 program():
  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)
  
  ans = max([uf.size(i) for i in range(N)])
  return ans

print(program())

Submission status: Accepted

Solution code:
'''
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineerin College

'''
import sys
from collections import deque,defaultdict as dd 
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]



def solve():
    
    
    
    # for _ in range(ii()):
    
    
    n,m = mi()
    
    par = [i for i in range(n+1)]
    sz = [1]*(n+1)
    
    def find(i):
        if i == par[i]:
            return par[i]
        par[i] = find(par[i])
        return par[i]
    
    def union(u,v):
        u = find(u)
        v = find(v)
        if u!= v:
            if sz[u]>sz[v]:
                par[v] = u
                sz[u]+=sz[v]
            else:
                par[u] = v
                sz[v] += sz[u]
    
    
    for i in range(m):
        x,y = mi()
        union(x,y)
    print(max(sz))
    
    
if __name__ =="__main__":
    # read()
    solve()

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def root(x):
        if par[x] == x:
            return x
        par[x] = root(par[x])
        return par[x]

    def unite(x,y):
        x = root(x)
        y = root(y)
        if rank[x] < rank[y]:
            par[x] = y
        else:
            par[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1
    n,m = LI()
    rank = [0]*n
    par = [i for i in range(n)]
    for _ in range(m):
        a,b = LI()
        a -= 1
        b -= 1
        if root(a) != root(b):
            unite(a,b)
    cnt = [0]*n
    for i in range(n):
        cnt[root(i)] += 1
    print(max(cnt))
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
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] #x(根)の要素にｙ(根)のグループの要素数をプラスする(要素の値は負の数で表される)
        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 relf.roots()}

    def __str__(self): #ルート要素: [そのグループに含まれる要素のリスト]を文字列で返す (print()での表示用)
        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)

ans=0
roots=uf.roots()

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

print(ans)

Submission status: Accepted

Solution code:
n , m = list(map(int, input().split()))
par=[i for i in range(n)]
rank=[0 for _ in range(n)]

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

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

for _ in range(m):
    x , y=list(map(int, input().split()))
    x -= 1 ; y-=1
    unit(x,y)
count=[0 for _ in range(n)]
for i in range(n):
    count[find(i)]+=1
print(max(count))

Submission status: Accepted

Solution code:
class UnionFind:
  def __init__(self,n):
    self.n=n
    self.parent=[i for i in range(n)] # 親
    self.rank=[1]*n # 木の高さ
    self.size=[1]*n # size[i]はiを根とするグループのサイズ
    
  def find(self,x): # xの根を返す
    if self.parent[x]==x:
      return x
    else:
      self.parent[x]=self.find(self.parent[x]) # 経路圧縮
      return self.parent[x]
  
  def unite(self,x,y): # x,yの属する集合を併合する
    x=self.find(x)
    y=self.find(y)
    if x != y:
      if self.rank[x]<self.rank[y]:
        self.parent[x]=y
        self.size[y]+=self.size[x]
      else:
        self.parent[y]=x
        self.size[x]+=self.size[y]
        if self.rank[x]==self.rank[y]:
          self.rank[x]+=1

  def group_size(self,x): # xが属する集合の大きさを返す
    return self.size[self.find(x)]

N, M =map(int,input().split())
A, B = [0]*M, [0]*M

direct = [[] for _ in range(N)]
uf = UnionFind(N)

for i in range(M):
    A[i], B[i] = map(int,input().split())
    A[i] -= 1
    B[i] -= 1
    uf.unite(A[i], B[i])

ans = 0
for i in range(N):
    ans = max(ans, uf.group_size(i))
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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


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

    def find(self, x):
        if self.parent[x] < 0:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

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

        if x == y:
            return
        elif self.rank[x] > self.rank[y]:
            self.parent[x] += self.parent[y]
            self.parent[y] = x
        else:
            self.parent[y] += self.parent[x]
            self.parent[x] = y
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

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

    def count(self, x):
        return -self.parent[self.find(x)]


uf = UnionFind(n)

for a, b in ab:
    uf.union(a, b)

ans = 0
for i in range(1, n + 1):
    ans = max(ans, uf.count(i))

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)

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

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

Submission status: Accepted

Solution code:
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)

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

Submission status: Accepted

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

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())
      
uni = UnionFind(N)
for i in range(M):
  uni.union(List[i][0]-1,List[i][1]-1)
res = 0
for i in range(N):
  res = max(res,uni.size(i))
print(res)

Submission status: Accepted

Solution code:
#BFS
from collections import deque
from sys import stdin
input = stdin.readline

N,M = map(int, input().split())
neighbor = [[] for _ in range(N)]
for i in range(M):
    a,b = map(lambda x: int(x) - 1, input().split())
    neighbor[a].append(b)
    neighbor[b].append(a)

checked = [0]*N
queue = deque()
group = [0]*N
groupnum = 0
for i in range(N):
    if group[i] == 0:
        groupnum += 1
        queue.append(i)
        group[i] = groupnum
        while queue:
            x = queue.popleft()
            for p in neighbor[x]:
                if group[p] == 0:
                    queue.append(p)
                    group[p] = groupnum

group_membercnt = [0]*(N+1)
for num in group:
    group_membercnt[num] += 1
print(max(group_membercnt[1:]))

Submission status: Accepted

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

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
class UnionFindTree():
    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
        
        # 常にxの要素数＞yの要素数とする
        if self.parents[x] > self.parents[y]:
            x, y = y, x
            
        # self.parents[x] に　yの要素数を追加してから併合
        self.parents[x] += self.parents[y]
        self.parents[y] = x

def main():
  N, M = map(int, input().split())
  uft = UnionFindTree(N)
  
  for m in range(M):
    a, b = map(lambda x: int(x)-1, input().split())
    uft.union(a, b)
    
  print(-min(uft.parents))
  
  
  
if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
class UnionFindTree():
    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
        
        # 常にxの要素数＞yの要素数とする
        if self.parents[x] > self.parents[y]:
            x, y = y, x
            
        # self.parents[x] に　yの要素数を追加してから併合
        self.parents[x] += self.parents[y]
        self.parents[y] = x
  
N, M = map(int, input().split())
uft = UnionFindTree(N)
for m in range(M):
  a, b = map(lambda x: int(x)-1, input().split())
  uft.union(a, b)
  
print(-min(uft.parents))

Submission status: Accepted

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

V = [set() for _ in range(n)] # 隣接リスト
for a, b in [[*map(lambda x: int(x)-1, input().split())] for _ in range(m)]:
    V[a].add(b)
    V[b].add(a)

from collections import deque, Counter
G = [-1] * n # グループ色分け
for s in range(n): # BFS
    dq = deque([s])
    while dq:
        p = dq.popleft()
        if G[p] >= 0: continue
        G[p] = s # 色付け
        for c in V[p]: dq.append(c)

print(max(Counter(G).values())) # 最大メンバ数

Submission status: Accepted

Solution code:
from collections import deque

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


t = dict()

for i in range(1, n + 1):
    t[i] = set()


for a, b in ab:
    t[a].add(b)
    t[b].add(a)


def dfs(graph, start):
    visited = set()
    stack = [start]

    while stack:
        vertex = stack.pop()
        if vertex not in visited:
            visited.add(vertex)

            stack.extend(graph[vertex] - visited)
    return visited

seen = set()
length = 0
for i in range(1, n+1):
    if i not in seen:
        ans = dfs(t, i)
        for num in ans:
            seen.add(num)

        if len(ans) > length:
            length = len(ans)

print(length)

Submission status: Accepted

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


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

for _ in range(M):
    A, B = map(lambda x:int(x) - 1, input().split())
    to[A].append(B)
    to[B].append(A)


def dfs(v, to, seen):
    global count
    seen[v] = True
    count += 1
    for nv in to[v]:
        if seen[nv]:
            continue
        dfs(nv, to, seen)


def main():
    global count
    seen = [False] * N
    cnts = []
    for i in range(N):
        if not seen[i]:
            dfs(i, to, seen)
            cnts.append(count)
            count = 0
    
    print(max(cnts))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

V = [set() for _ in range(n)] # 隣接リスト
for a, b in [[*map(lambda x: int(x)-1, input().split())] for _ in range(m)]:
    V[a].add(b)
    V[b].add(a)

from collections import deque
G = [-1] * n # グループ色分け
for s in range(n): # BFS
    dq = deque([s])
    while dq:
        p = dq.popleft()
        if G[p] >= 0: continue
        G[p] = s # 色付け
        for c in V[p]: dq.append(c)

C = dict() # グループメンバ数集計
for g in G:
    if not g in C: C[g] = 0
    C[g] += 1

print(max(C.values())) # 最大メンバ数

Submission status: Accepted

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

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

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


N, M = map(int, input().split())
par = [-1 for i in range(N)]

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

print(-min(par))