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

    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())
UF=UnionFind(N)

for _ 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)

Submission status: Runtime Error

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

    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())
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(ans, UF.size(i));
 
 print(ans)

Submission status: Runtime Error

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

    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())
UF=UnionFind(N)

for _ 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)

Submission status: Runtime Error

Solution code:
def f(n):
    global myFriend
    if len(myFriend[n])==0:
        return []
    else:
        ans = []
        for i in list(set(myFriend[n])):
            ans += [i]
            ans += f(i)
        myFriend[n] = []
        return ans

n,m = map(int,input().split())
myFriend = [[] for i in range(n+1)]
for i in range(m):
    a,b = map(int,input().split())
    if a < b:
        myFriend[a].append(b)
    else:
        myFriend[b].append(a)
#print(myFriend)

ans = 0
for i in range(n+1):
    ans = max(ans,len(list(set(f(i)))))
print(ans+1)

Submission status: Runtime Error

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

ListM = [[0 for i in range(2)] for j in range(M)]

for m in range(M):
  a, b = map(int,input().split())
  if a < b:
    ListM[m][0] = a
    ListM[m][1] = b
  else:
    ListM[m][0] = b
    ListM[m][1] = a


ListM = list(map(list, set(map(tuple, ListM))))

ListT = {{ListM[0][0]},{ListM[0][1]}}
tmpI = ListM[0][0]
for i in range(M-1 ,1):
  if tmpI == ListM[i][0]:
    ListT.append({ListM[i][1]})
  else:
    tmpI = ListM[I][0]
    flg = true
    for j in range(ListT.len):
      if ListT[j].index(ListM[i][1])  != -1:
        flg = false
    
    if flg:
      ListT.append({ListM[i][1]})
          
print(len(ListT))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
A = [0] * M
B = [0] * M
for i in range(M):
    A[i], B[i] = 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)]
    
u = UnionFind(N)
for i in range(M):
    u.union(A[i], B[i])
    
ans = 1
for i in range(1, N+1):
    print("{}の属するグループは{}人です。".format(i, u.size(i)))
    ans = max(ans, u.size(i))
print(ans)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
A = [0] * M
B = [0] * M
for i in range(M):
    A[i], B[i] = map(int, input().split())

class UnionFind:
    def __init__(self, n):
        # 親要素のノード番号を格納。par[x] == xの時そのノードは根
        self.par = [-1 for i in range(n + 1)]
        
    # 検索
    # 根ならその番号を返す
    def find(self, x):
        if self.par[x] < 0:
            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.par[x] < self.par[y]:  # par<0の小さい方に合わせる
                self.par[y] += self.par[x]
                self.par[x] = y
            else:
                self.par[x] += self.par[y]
                self.par[y] = x

    def size(self, x):
        return -self.par[self.find(x)]
    
    
u = UnionFind(N)
for i in range(M):
    u.union(A[i], B[i])
    
ans = 1
for i in range(1, N+1):
    ans = max(ans, u.size(i))
print(ans)

Submission status: Runtime Error

Solution code:

n,m=map(int,input().split())
root=[-1]*n
def r(x):
    if root[x]<0:
        return x
    else:
        root[x]=r(root[x])
        return root[x]
def unite(x,y):
    x=r(x)
    y=r(y)
    if x==y:
        return
    root[x]+=root[y]
    root[y]=x
def size(x):
    x=r(x)
    return -root[x]
for i in range(m):
    x,y=map(int,input().split())
    x-=1
    y-=1
    unite(x,y)
ans=0
for i in range(n):
    ans=max(ans,size(i))
print(ans)

Submission status: Runtime Error

Solution code:
def main():
    n,m=map(int,input().split())
    uf=UnionFind(n)
    for a,b in (map(lambda x: int(x)-1, input().split()) for _ in range(m)):
        uf.union(a,b)
    print(max((uf.size(x) for x in uf.roots())))
    
if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

    def find(self, x):
        if self.parents[x] < x:
            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 roots(self):
        return [i for i,x in enumerate(self.parents) if x < 0]

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

uf = UnionFind(n)

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

cnt = []
for i in uf.roots():
    cnt.append(i)
    
print(max(cnt))

Submission status: Runtime Error

Solution code:
n, m = map(int,input().split())
grp = []
grp1 = set()
if m == 0:
    print(1)
else:
    a, b = map(int, input().split())
    grp.append({a,b})
    grp1.add(a)
    grp1.add(b)
    for _ in range(m - 1):
        a, b = map(int, input().split())
        if a not in grp1 and b not in grp1:
                grp.append({a,b})
                grp1.add(a)
                grp1.add(b)
        else:
            for i in range(len(grp)):
                if a in grp[i]:
                    grp[i].add(b)
                    break
                elif b in grp[i]:
                    grp[i].add(a)
                    break
        ans = 0
    for i in grp:
        ans = max(ans, len(i))
    print(ans)

Submission status: Runtime Error

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

    def find(self, x):
        if self.parents[x] < x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        
        if x == y:
            return
        
        if self.parents[x] > self.parents[y]:
            x, y = y, x
            
        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

uf = UnionFind(n)

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

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

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
encountered = []
friends = []

for _ in range(M):
  a, b = map(int, input().split())
  a_enc = a in encountered
  b_enc = b in encountered
  
  if [a_enc, b_enc] == [False, False]:
    friends += [[a, b]]
    encountered += [a, b]
    
  elif [a_enc, b_enc] == [False, True]:
    for i in range(len(friends)):
      if b in friends[i]:
        friends[i] += [a]
        break
    encountered += [a]
    
  elif [a_enc, b_enc] == [True, False]:
    for i in range(len(friends)):
      if a in friends[i]:
        friends[i] += [b]
        break
    encountered += [b]
   
  else:
    a_loc, b_loc = 0, 0
    for i in range(len(friends)):
      if a in friends[i]:
        a_loc == i
      if b in friends[i]:
        b_loc == i
      if a_loc != 0 and b_loc != 0:
        break
    if a_loc != b_loc:
      friends[a_loc] += friends[b_loc]
      del friends[b_loc]

print(max(map(len, friends)))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
dic = {}  # 各人が何番グループに属しているか
num = {}  # 各グループに何人属しているか
count = 0  # グループの番号

for _ in range(M):
    a, b = map(int, input().split())
    if a in dic and b not in dic:
        dic[b] = dic[a]
        num[dic[a]] += 1
    elif a not in dic and b in dic:
        dic[a] = dic[b]
        num[dic[b]] += 1
    elif a not in dic and b not in dic:  # どちらもグループに属していない場合、新たなグループを作る
        dic[a] = count
        dic[b] = count
        num[count] = 2
        count += 1

print(max(num.values()))

Submission status: Runtime Error

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

for i in d:
  for j in f:
    if i[0] in j or i[1] in j:
      j.add(i[0])
      j.add(i[1])
      break
  else:
    f.append(set(i))

a = []
for i in f:a.append(len(i))
print(max(a))

Submission status: Runtime Error

Solution code:
N,M=input().split(' ')
n,m=int(N),int(M)

flist=[input().split(' ')for i in range(m)]
Alist=[int(0)for i in range(n)]
num=0
for i in range(m):
    a_i,b_i=int(flist[i][0]),int(flist[i][1])
    if Alist[a_i-1]==0 and Alist[b_i-1]==0:
        num+=1
        Alist[a_i-1]=num
        Alist[b_i-1]=num
    elif Alist[b_i-1]==Alist[a_i-1]:
      a=0
    elif Alist[a_i-1]!=0:
        Alist[b_i-1]=Alist[a_i-1]
    elif Alist[b_i-1]!=0:
        Alist[a_i-1]=Alist[b_i-1]
M=max(Alist)
Blist=[int(0)for i in range(M)]
for i in Alist:
  if i!=0:
	  Blist[i-1]+=1
print(max(Blist))

Submission status: Runtime Error

Solution code:
N,M=input().split(' ')
n,m=int(N),int(M)

flist=[input().split(' ')for i in range(m)]
Alist=[int(0)for i in range(n)]
num=0
for i in range(m):
    a_i,b_i=int(flist[i][0]),int(flist[i][1])
    if Alist[a_i-1]==0 and Alist[b_i-1]==0:
        num+=1
        Alist[a_i-1]=num
        Alist[b_i-1]=num
    elif Alist[a_i-1]!=0:
        Alist[b_i-1]=Alist[a_i-1]
    elif Alist[b_i-1]!=0:
        Alist[a_i-1]=Alist[b_i-1]
M=max(Alist)
Blist=[int(0)for i in range(M)]
for i in Alist:
  if i!=0:
	  Blist[i-1]+=1
print(max(Blist))

Submission status: Runtime Error

Solution code:
N,M=input().split(' ')
n,m=int(N),int(M)

flist=[input().split(' ')for i in range(m)]
Alist=[int(0)for i in range(n)]
num=0
for i in range(m):
    a_i,b_i=int(flist[i][0]),int(flist[i][1])
    if Alist[a_i-1]==0 and Alist[b_i-1]==0:
        num+=1
        Alist[a_i-1]=num
        Alist[b_i-1]=num
    elif Alist[a_i-1]!=0:
        Alist[b_i-1]=Alist[a_i-1]
    elif Alist[b_i-1]!=0:
        Alist[a_i-1]=Alist[b_i-1]
M=max(Alist)
print('Alist',Alist)
Blist=[int(0)for i in range(M)]
for i in Alist:
  if i!=0:
	  Blist[i-1]+=1
print('Blist',Blist)
print(max(Blist))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
list = []
A_prev = 0
B_prev = 0
for i in range(M):
    A, B = map(int, input().split())
    [A, B] = [min(A,B), max(A,B)]
    if [A, B] == [A_prev, B_prev]:
        continue
    else:
        list.append([A, B])
        A_prev = A
        B_prev = B

list.sort()
group = [list[0]]
for i in range(1,len(list)):
    point = 0
    for j in range(len(group)):
        if list[i][0] in group[j] and list[i][1] in group[j]:
            break
        elif list[i][0] in group[j]:
            group[j].append(list[i][1])
            break
        elif list[i][1] in group[j]:
            group[j].append(list[i][0])
            break
        else:
            point += 1
    if point == len(group):
        group.append(list[i])

num = []

for j in range(len(group)):
    num.append(len(group[j]))
print(max(num))

Submission status: Runtime Error

Solution code:

def main():
    from itertools import chain
    import sys
    input = sys.stdin.readline

    def gcd(x, y):
        if y == 0:
            return x
        return gcd(y, x % y)
    # 6n+1, 6n-1でも求めて、素数**2+素数*kはふるいおとす
    def prime_set(N):
        """
        Nまでの素数のsetを返す
        """
        if N < 4:
            return ({}, {}, {2}, {2, 3})[N]
        Nsq = int(N ** 0.5 + 0.5) + 1
        primes = {2, 3} | set(chain(range(5, N + 1, 6), range(7, N + 1, 6)))
        for i in range(5, Nsq, 2):
            if i in primes:
                primes -= set(range(i * i, N + 1, i * 2))
        return list(primes)

    def factorization(n, prime_set):
        l = []
        tmp = n
        nsq = int(n ** 0.5 + 0.5) + 1
        for i in prime_set:
            if  i > nsq: 
                break 
            if tmp%i == 0:
                while tmp%i == 0:
                    tmp //= i
                l.append(i)
        if tmp != 1:
            l.append(tmp)
        if l == []:
            if n != 1:
                l.append(n)
        return l
    n = int(input())
    A = [int(x) for x in input().split()]
    len_A = len(A)
    # setwise coprimeの条件(1ならOK)
    g = 0
    for a in A:
        g = gcd(g, a)
    # 素数列挙
    num = 10**6+1
    primes = prime_set(num)
    # 全部素因数分解
    prime_list = [0] * (num+1)
    pairwise_f = True
    ll = list()
    for a in A:
        l = []
        tmp = a
        # nsq = int(a ** 0.5 + 0.5) + 1
        for i in primes:
            if  i*i > a:
                break 
            if tmp%i == 0:
                while tmp%i == 0:
                    tmp //= i
                prime_list[i] += 1
                l.append(i)
        if tmp != 1:
            prime_list[tmp] += 1
            l.append(tmp)
        if l == []:
            if a != 1:
                prime_list[n] += 1
                l.append(a)
        set_prime_list = set(prime_list)
        if len_A > 10**5:
            if set_prime_list != set([0]) and set_prime_list != set([0, 1]):
                pairwise_f = False
                break
    # print(prime_list[:100])
    if set_prime_list != set([0]) and set_prime_list != set([0, 1]):
        pairwise_f = False
    if pairwise_f:
        print('pairwise coprime')
    elif g == 1:
        print('setwise coprime')
    else:
        print('not coprime')

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import Counter
def solve(n, m, a, b):
    parents = list(range(n+1))
    def root(x):
        return root(parents[x]) if parents[x] != x else x
    for u, v in zip(a, b):
        u = root(u)
        v = root(v)
        if u > v:
            u, v = v, u
        if u != v:
            parents[v] = u
    return max(Counter(map(root, parents)).values())

n, m = map(int, input().split())
if m > 0:
    a, b = zip(*[map(int, input().split()) for i in range(m)])
else:
    a, b = [], []
print(solve(n, m, a, b))

Submission status: Runtime Error

Solution code:
from collections import Counter
def solve(n, m, a, b):
    parents = list(range(n+1))
    def root(x):
        return root(parents[x]) if parents[x] != x else x
    for i in range(m):
        u = root(a[i])
        v = root(b[i])
        if u > v:
            u, v = v, u
        if u != v:
            parents[v] = u
    return max(Counter(parents).values())

n, m = map(int, input().split())
a, b = zip(*[map(int, input().split()) for i in range(m)])
print(solve(n, m, a, b))

Submission status: Runtime Error

Solution code:
from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
from functools import lru_cache
import math

#setrecursionlimit(10**6)
rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())
rlf = lambda: map(float, stdin.readline().split())

INF, NINF = float('inf'), float('-inf')
MOD = 10**9 + 7

def main():
	n, m = rli()
	seen = defaultdict(lambda: False)
	G = [[] for _ in range(n+100)]
	for _ in range(m):
		u, v = rli()
		u -= 1
		v -= 1
		u, v = min(u, v), max(u, v)
		if seen[(u, v)]: continue
		seen[(u, v)] = True
		G[u].append(v)
		G[v].append(u)
	sizes = []
	seen2 = defaultdict(lambda: False)
	loc_size = 0
	def dfs(node):
		nonlocal loc_size 
		seen2[node] = True
		loc_size += 1
		for nbhr in G[node]:
			if seen2[nbhr]: continue
			dfs(nbhr)

	for i in range(n):
		if seen[i]: continue
		loc_size = 0
		dfs(i)
		sizes.append(loc_size)
	print(max(sizes))
	stdout.close()

if __name__ == "__main__":
	main()

Submission status: Runtime Error

Solution code:
from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
from functools import lru_cache
import math

#setrecursionlimit(10**6)
rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())
rlf = lambda: map(float, stdin.readline().split())

INF, NINF = float('inf'), float('-inf')
MOD = 10**9 + 7

def main():
	n, m = rli()
	seen = defaultdict(lambda: False)
	G = [[] for _ in range(n)]
	for _ in range(m):
		u, v = rli()
		u -= 1
		v -= 1
		u, v = min(u, v), max(u, v)
		if seen[(u, v)]: continue
		seen[(u, v)] = True
		G[u].append(v)
		G[v].append(u)
	sizes = []
	seen2 = defaultdict(lambda: False)
	loc_size = 0
	def dfs(node):
		nonlocal loc_size 
		seen2[node] = True
		loc_size += 1
		for nbhr in G[node]:
			if seen2[nbhr]: continue
			dfs(nbhr)

	for i in range(n):
		if seen[i]: continue
		loc_size = 0
		dfs(i)
		sizes.append(loc_size)
	print(max(sizes))
	stdout.close()

if __name__ == "__main__":
	main()

Submission status: Runtime Error

Solution code:
class UnionFind:

    def __init__(self):
        self.parents = {}
        self.sz = {}
        self.size = 0


    def add(self, a):
        if a in self.parents:
            return
        self.size += 1
        self.sz[a] = 1
        self.parents[a] = a 

    def find(self, a):
        if self.parents[a] != a:
            self.parents[a] = self.find(self.parents[a])

        return self.parents[a]

    def join(self, a, b):
        pa, pb = self.find(a), self.find(b)
        if pa == pb:
            return
        if self.sz[pa] > self.sz[pb]:
            self.parents[pb] = pa
            self.sz[pa] += self.sz[pb]
        else:
            self.parents[pa] = pb 
            self.sz[pb] += self.sz[pa]

        self.size -= 1


n,m = list(map(int,input().split(" ")))
uf = UnionFind()
ans = 0
for _ in range(m):
    x,y = list(map(int,input().split(" ")))
    uf.add(x)
    uf.add(y)
    uf.join(x,y)
    ans = max(ans, uf.sz[x])


print(max(uf.sz.values()))

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
d = dict()
flag = [[True] * n for _ in range(n)]
for i in range(m):
    a, b = map(int, input().split())
    if flag[a - 1][b - 1] == True:
        if flag[b - 1][a - 1] == True:
            if a not in d:
                d[a] = 1
            else:
                d[a] += 1
            if b not in d:
                d[b] = 1
            elif b in d:
                d[b] += 1
    flag[a - 1][b - 1] = False
    flag[b - 1][a - 1] = False
print(max(d.values()) + 1)

Submission status: Runtime Error

Solution code:
N, M = [int(i) for i in input().split()]
L = []
for i in range(M):
  a, b = [int(j) for j in input().split()]
  for j in range(len(L)):
    ifa = a in L[j]
    ifb = b in L[j]
    if (not ifb) and ifa:
      L[j].append(b)
      break
    elif (not ifa) and ifb:
      L[j].append(a)
      break
    elif ifa and ifb:
      break
  else:
    L.append([a, b])

f = True
while f:
  f = False
  i = 0
  while i < len(L)-1:
    flag = False
    j = i + 1
    while j < len(L):
      if len(set(L[i]+L[j])) < len(L[i]+L[j]):
        L[i] = L[i] + L[j]
        pop(L[j])
        f = True
        flag = True
        j -= 1
      j += 1
    if flag:
      i -= 1
    i += 1

lenmax = 0
for i in range(len(L)):
  if lenmax < len(L[i]):
    lenmax = len(L[i])
print(lenmax)

Submission status: Runtime Error

Solution code:
N, M = [int(i) for i in input().split()]
L = []
for i in range(M):
  a, b = [int(j) for j in input().split()]
  for j in range(len(L)):
    ifa = a in L[j]
    ifb = b in L[j]
    if (not ifb) and ifa:
      L[j].append(b)
      break
    elif (not ifa) and ifb:
      L[j].append(a)
      break
    elif ifa and ifb:
      break
  else:
    L.append([a, b])

i = 0
while i < len(L)-1:
  flag = False
  j = i + 1
  while j < len(L):
    if len(set(L[i]+L[j])) < len(L[i]+L[j]):
      L[i] = L[i] + L[j]
      pop(L[j])
      flag = True
      j -= 1
    j += 1
  if flag:
    i -= 1
  i += 1

lenmax = 0
for i in range(len(L)):
  if lenmax < len(L[i]):
    lenmax = len(L[i])
print(lenmax)

Submission status: Runtime Error

Solution code:
import sys

ans=0
g=[]
n=0
input()
for s in sys.stdin:
    x=set()
    d=[]
    for a in s.split():
        x.add(a)
        for i in range(len(g)):
            if a in g[i]:
                d+=[i]
                x|=g[i]
    for i in d:
        del g[i]
    g.append(x)

if g:
    print(max(len(t) for t in g))
else:
    print(0)

Submission status: Runtime Error

Solution code:
import sys

ans=0
g=set()
input()
for s in sys.stdin:
    for a in s.split():
        for u in [*g]:
            if a in u:
                g.discard(u)
                s+=u
    g.add(s)

print(max(len(set(t.split())) for t in g))

Submission status: Runtime Error

Solution code:
import sys

ans=0
g=[]
input()
for s in sys.stdin:
    for a in s.split():
        for u in g:
            if a in u:
                s+=u
    g+=[s]


print(max(len(set(t.split())) for t in g))

Submission status: Runtime Error

Solution code:
from collections import deque

n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
t = []
s = set()
q = deque()

a, b = ab[0]
q.append(a)
lt = set

while len(q) != 0:
    x = q.popleft()
    s.add(x)
    tt = {x}
    tmp = []
    #print(x)
    #print(ab)
    for a, b in ab:
        if a in tt and b not in s:
            tt.add(b)
            s.add(b)
        elif b in tt and a not in s:
            tt.add(a)
            s.add(a)
        elif a not in tt and b not in tt:
            if a not in q:
                q.append(a)
                tmp.append([a, b])
    ab = tmp
    #print(q)
    t.append(tt)
#print(t)
ans = 0
for tt in t:
    ans = max(ans, len(tt))
print(ans)

Submission status: Runtime Error

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

    def root(self, x):
        if (self.r[x]) < 0:
            return x

        return self.r[x] - self.root(self.r[x])

    def unite(self, x, y):
        x = self.root(x)
        y = self.root(y)

        if x == y:
            return False
        r = self.r

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

        r[x] += r[y]
        r[y] = x

        return True

    def size(self, x):
        return -self.r[self.root(x)]


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


relateions = UnionFind(N)
for _ in range(M):
    A, B = map(int, input().split())
    relateions.unite(A-1, B-1)

max_ = -1
for i in range(N):
    max_ = max(relateions.size(i), max_)

print(max_)

Submission status: Runtime Error

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

    def root(self, x):
        if (self.r[x]) < 0:
            return x

        return self.r[x] - self.root(self.r[x])

    def unite(self, x, y):
        x = self.root(x)
        y = self.root(y)

        if x == y:
            return False
        r = self.r

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

        r[x] += r[y]
        r[y] = x

        return True

    def size(self, x):
        return -self.r[self.root(x)]


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


relateions = UnionFind(N)
for _ in range(M):
    A, B = map(int, input().split())
    relateions.unite(A, B)

max_ = -1
for i in range(N):
    max_ = max(relateions.size(i), max_)

print(max_)

Submission status: Runtime Error

Solution code:
from collections import deque

n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
t = []
s = set()
q = deque()

a, b = ab[0]
q.append(a)
lt = set

while len(q) != 0:
    x = q.popleft()
    s.add(x)
    tt = [x]
    for a, b in ab:
        if a in tt and b not in s:
            tt.append(b)
            s.add(b)
        elif b in tt and a not in s:
            tt.append(a)
            s.add(a)
        elif a not in s:
            q.append(a)
            s.add(a)
    t.append(tt)

ans = 0
for tt in t:
    ans = max(ans, len(tt))
print(ans)

Submission status: Runtime Error

Solution code:
class Node():
    def __init__(self,index):
        self.index = index
        self.parent = self 
        self.size = 1

    @staticmethod
    def find_set(node):
        if node.parent == node:
            return node 
        root = Node.find_set(node.parent)
        node.parent = root
        root.size += 1
        return root

    @staticmethod
    def union(node_a,node_b):
        root_a = Node.find_set(node_a)
        root_b = Node.find_set(node_b)
        if root_a != root_b:
            if root_a.size < root_b.size:
                root_a.parent = root_b
            else:
                root_b.parent = root_a



if __name__ == "__main__":
    n,m = [int(val) for val in input().split()]
    
    persons = []
    count = []
    for i in range(n):
        persons.append(Node(i))
        count.append(0)
    
    for i in range(m):
        a,b = [int(val) for val in input().split()]
        if a > b:
            Node.union(persons[a-1],persons[b-1])
        else:
            Node.union(persons[b-1],persons[a-1])


    for idx,person in enumerate(persons):
        count[Node.find_set(person).index] += 1
    
    # print(count)
    result = max(count)
    print(result)

Submission status: Runtime Error

Solution code:
import numpy

class UnionFind:
    def __init__(self, N):
        self.N = N

        # the parent of all node is itself
        self.parent = list(range(N))

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

    def unite(self, i, j):
        rooti = self.root(i)
        rootj = self.root(j)

        if rooti == rootj:
            return 
        else:
            self.parent[self.root(i)] = rootj

    def same(self, i, j):
        return self.root[i] == self.root[j]

N, M = map(int, input().split())
R = numpy.array([[int(r) for r in input().split()] for _ in range(M)]) - 1

forest = UnionFind(N)
for p1, p2 in R:
    forest.unite(p1, p2)

count = {}
for node in range(N):
    r = forest.root(node)
    if r in count:
        count[r] += 1
    else:
        count[r] = 1

m = max([count[key] for key in count])
print(m)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
x = []
for i in range(m):
    t = list(map(int,input().split()))
    if len(x)>0:
        for j in range(len(x)):
            r = 0
            while r <len(x[j]):
                if x[j][r] == t[0] or x[j][r] == t[1]:
                    x[j].extend(t)
                    break
                r +=1
        if r >= len(x[j]):
            x.append(t)
    elif len(x) == 0:
        x.append(t)
z = []
for i in range(len(x)):
    z.append(len(set(x[i])))
print(max(z))

Submission status: Runtime Error

Solution code:
l=lambda:map(int, input().split())
n,m=l()
p=[-1]*n
def r(x):
  if p[x]<0:return x
  p[x]=r(p[x]);return r(p[x])
def u(x,y):
  x,y=r(x),(y)
  if x==y: return  
  if x>y: x,y=y,x
  p[x]+=p[y]
  p[y]=x
def e(x,y):
  return r(x)==r(y)
def s(x):
  return -p[r(x)]
for _ in range(m):
  a,b=l()
  u(a-1,b-1)
print(-min(p))

Submission status: Runtime Error

Solution code:
class UnionTree:
    def __init__(self,n):
        self.parent = [i for i in range(n)]
    def get_root(self,m):
        if self.parent[m] == m:
            return m
        else:
            self.parent[m] = self.get_root(self.parent[m])
            return self.parent[m]
    def have_same_root(self,n,m):
        if self.get_root[n] == self.get_root[m]:
            return True
        else:
            return False
    def unite(self,n,m):
        root_n = self.get_root(n)
        root_m = self.get_root(m)
        if root_n != root_m:
            self.parent[root_n] = root_m
N, M = map(int, input().split())
if M == 0:
  print(0)
else:
  ut = UnionTree(N)
  for _ in range(M):
      a, b = map(int, input().split())
      ut.unite(a-1, b-1)
  child_num = [0]*N
  for num in range(N):
      temp = ut.get_root(num)
      child_num[temp] += 1
  print(max(child_num))