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: Wrong Answer

Solution code:
n, m = map(int, input().split())
A = []*m
B = []*m
for i in range(m):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
S = [set([i+1]) for i in range(n)]
#print(S)
for i in range(m):
    S[A[i]-1].add(B[i])
    S[B[i]-1].add(A[i])
    #print(S)

ans = 1
for i in range(n):
    ans = max(ans, len(S[i]))

print(str(ans))

Submission status: Wrong Answer

Solution code:
N, M=map(int,input().split())
groupnumber=[0 for i in range(N)]
grouplist=[]
counter=0
maxmembers=0
for i in range(M):

  a,b=map(int,input().split())
  a-=1
  b-=1
  if groupnumber[a]==0 and groupnumber[b]==0:
    counter+=1
    groupnumber[a]=counter
    groupnumber[b]=counter
    
    grouplist+=[[a,b]]
  elif groupnumber[a]==0 and groupnumber[b]!=0:
    groupnumber[a]=groupnumber[b]
    tmp=groupnumber[a]-1
    grouplist[tmp]+=[a]
    
  elif groupnumber[a]!=0 and groupnumber[b]==0:
    groupnumber[b]=groupnumber[a]
    tmp=groupnumber[b]-1
    grouplist[tmp]+=[b]
  else:
    if groupnumber[a]!=groupnumber[b]:
      tmp1=groupnumber[a]-1
      tmp2=groupnumber[b]-1
      
      for i in grouplist[tmp2]:
        groupnumber[i]=tmp1+1
      if grouplist[tmp2]!=[] and grouplist[tmp2]!=[]:
        grouplist[tmp1]+=grouplist[tmp2]
        grouplist[tmp2]=[]

for i in grouplist:
  tmp=len(i)
  if tmp>maxmembers:
    maxmembers=tmp
print(maxmembers)

Submission status: Wrong Answer

Solution code:
n, m = list(map(int, input().split()))
sets = []

if m == 0:
    print(n)
    exit()

a, b = list(map(int, input().split()))
s = set()
s.add(a)
s.add(b)
sets.append(s)
for i in range(1, m):
    a, b = list(map(int, input().split()))
    f = True
    for si in range(len(sets)):
        if a in sets[si] or b in sets[si]:
            sets[si].add(a)
            sets[si].add(b)
            f = False
            break
    if f:
        s = set()
        s.add(a)
        s.add(b)
        sets.append(s)

print(sets)
slen = sorted([len(s) for s in sets])
ans = 0
for l in slen:
    ans += (l - ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys,os
import math
from fractions import Fraction 
from collections import defaultdict
from random import randint

# sys.stderr=open('err.txt','w')
# sys.stdout=open('output.txt','w')
# sys.stdin=open('input.txt','r')

def linput():
    return list(minput())

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

###############################################################

def make_set(a):
    parent[a]=a

def find_set(a):
    if parent[a]==a:
        return a
    parent[a]=find_set(parent[a])
    return parent[a]

def union_set(a,b):
    x=find_set(a)
    y=find_set(b)
    if x==y:
        return
    elif x>y:
        parent[x]=y
    else:
        parent[y]=x

n,m=minput()
parent=[0]*n
for i in range(n):
    make_set(i)
ans=0
for i in range(m):
    x,y=minput()
    x=x-1
    y=y-1
    union_set(x,y)
# print(parent)
di = defaultdict(int)
for i in parent:
    di[i]+=1
# print(di)
for k,v in di.items():
    ans=max(ans,v)
print(ans)

Submission status: Wrong Answer

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

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

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

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

    #print(par)
    ans=0
    for i in range(n):
        tmp = par.count(i)
        if tmp > ans:
            ans = tmp
    print(ans)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
friends_set_list = [set() for i in range(N+1)]
ans = 1
for i in range(M):
    A, B = map(int, input().split())
    friends_set_list[A].add(B)
    friends_set_list[A] |= friends_set_list[B]
    friends_set_list[B] = friends_set_list[A]
    num = len(friends_set_list[A])
    if ans < num:
        ans = num

print(ans)

Submission status: Wrong Answer

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

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

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

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

    print(par)
    ans=0
    for i in range(n):
        tmp = par.count(i)
        if tmp > ans:
            ans = tmp
    print(ans)

Submission status: Wrong Answer

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 main():
    N,M = map(int,input().split())
    A = [0 for i in range(M)]
    B = [0 for i in range(M)]
    for i in range(M):
        A[i],B[i] = map(int,input().split())
        
    uf = UnionFind(max(N,M))
    for i in range(M):
        a = A[i]-1
        b = B[i]-1
        uf.union(a, b)
        
    ans = 0
    for i in range(M):
        ans = max(ans,uf.size(i))
        
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

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)

uf1=UnionFind(n)
for i in range(m):
    a,b=map(int, input().split()) #a,b:つながっている辺
    uf1.union(a-1,b-1)
from collections import Counter
c=Counter(uf1.par)
print(max(c.values()))

Submission status: Wrong Answer

Solution code:
N,M,*A=map(int,open(0).read().split())
d={}
ss=[]
for i in range(0,len(A),2):
    a=A[i]
    b=A[i+1]
    if a in d:
        d[a].add(b)
        if b in d:
            d[a]|=d[b]
            d[b]=d[a]
        else:
            d[b]=d[a]
    elif b in d:
        d[b].add(a)
        d[a]=d[b]
    else:
        s=set([a,b])
        ss.append(s)
        d[a]=d[b]=s
print(max([1]+[len(s) for s in ss]))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[103]:


import collections


# In[137]:


N,M = map(int, input().split())
AB = []
for _ in range(M):
    A,B = map(int, input().split())
    A -= 1; B -= 1
    AB += [sorted([A,B])]


# In[138]:


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

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

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

def unite(x,y):
    if same(x,y):
        return 0
    par[y] = x


# In[139]:


for a,b in AB:
    unite(a,b)
ans = max(collections.Counter(par).values())
print(ans)


# In[ ]:

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
friends_set_list = [set() for i in range(N+1)]
ans = 1
for i in range(M):
    A, B = map(int, input().split())
    friends_set_list[A].add(B)
    friends_set_list[B].add(A)
    friends_set_list[A] |= friends_set_list[B]
    friends_set_list[B] = friends_set_list[A]
    num = len(friends_set_list[A])
    if ans < num:
        ans = num

print(ans)

Submission status: Wrong Answer

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

pairs = []
for m in range(M):
  pairs.append(list(map(int,input().split())))
  
Ns = [[i] for i in range(N)]

for i,pair in enumerate(pairs):
  if(pair[1]-1 not in Ns[pair[0]-1]):
  	Ns[pair[0]-1].append(pair[1]-1)
  if(pair[0]-1 not in Ns[pair[1]-1]):
  	Ns[pair[1]-1].append(pair[0]-1)
    
#print(Ns)
ans = 0
for i in range(len(Ns)):
  ans = max(ans,len(Ns[i]))
print(ans)

Submission status: Wrong Answer

Solution code:
def resolve():
  N, M = map(int, input().split(" "))
  friends = [None] * (N + 1)

  maximam_group = 0
  for _ in range(M):
    A, B = map(int, input().split(" "))

    if friends[A] == None and friends[B] == None:
      friends[A] = set([A, B])
      friends[B] = friends[A]
    elif friends[A] == None:
      friends[B].add(A)
      friends[A] = friends[B]
    elif friends[B] == None:
      friends[A].add(B)
      friends[B] = friends[A]
    else:
      union = friends[A].union(friends[B])
      for friend in union:
        friends[friend] = union

    if len(friends[A]) > maximam_group:
      maximam_group = len(friends[A])

  for friend in friends:
    print(friend)
  print(maximam_group)

if __name__ == "__main__":
  resolve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import collections


class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n + 1)]
        self.rank = [0] * (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 self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1


def main():
    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(collections.Counter(UF.par).values())
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

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

def unite(x, y):
  x = find(x)
  y = find(y)
  if x > y:
    par[x] = y
    find(x)
  else:
    par[y] = x
    find(y)
if M > 0:  
  for i in range(M):
    a, b = map(int, input().split())
    unite(a - 1, b - 1)
  
  ans = [0] * N
  Max = 0
  # for i in range(N):
  #   find(i)
  for i in par:
    ans[i] += 1
  for i in ans:
    if i > Max:
      Max = i
  
  print(Max)
else:
  print(1)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
A = []*m
B = []*m
for i in range(m):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
S = [set([i+1]) for i in range(n)]
#print(S)
for i in range(m):
    S[A[i]-1].add(B[i])
    S[B[i]-1].add(A[i])
    #print(S)

ans = 0
for i in range(n):
    ans = max(ans, len(S[i]))

print(str(ans))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
friends_set_list = [set() for i in range(N+1)]
ans = 0
for i in range(M):
    A, B = map(int, input().split())
    friends_set_list[A].add(B)
    friends_set_list[B].add(A)
    friends_set_list[A] |= friends_set_list[B]
    friends_set_list[B] = friends_set_list[A]
    num = len(friends_set_list[A])
    if ans < num:
        ans = num

print(ans)

Submission status: Wrong Answer

Solution code:
import random
import sys

# n, m = map(int, input().split())
n, m = 200000, 200000
group = {i: 0 for i in range(1, n+1)}
edge = {}
g_mem = {}
g_num = 0
g_con = set()

for i in range(m):
    # a, b = map(int, input().split())
    a, b = random.sample(range(1,n+1), 2)
    if group[a]==0 and group[b] == 0:
        g_num += 1
        group[a] = g_num
        group[b] = g_num
        g_mem[g_num] = 2
    elif group[a] == 0:
        group[a] = group[b]
        g_mem[group[b]] += 1
    elif group[b] == 0:
        group[b] = group[a]
        g_mem[group[a]] += 1
    elif group[a] != group[b]:
        if edge.get(group[a]):
            edge[group[a]].add(group[b])
        else:
            edge[group[a]] = {group[b]}
        if edge.get(group[b]):
            edge[group[b]].add(group[a])
        else:
            edge[group[b]] = {group[a]}

# print(g_mem)
# print(sum(g_mem.values()))
# print(edge)

g_reached = {i: 0 for i in range(1, g_num+1)}
ans = list(g_mem.values())
for now in range(1, g_num+1):
    if g_reached[now] == 0:
        g_reached[now] = 1
        if edge.get(now):
            count = g_mem[now]
            queue = set(edge[now])
            while len(queue) > 0:
                next_now = queue.pop()
                if g_reached[next_now] == 0:
                    g_reached[next_now] = 1
                    count += g_mem[next_now]
                    queue |= edge[next_now]
                queue -= {now}
                now = next_now
            ans.append(count)


# print(ans)
print(max(ans))

Submission status: Wrong Answer

Solution code:
def main(N, M, C):
    lst = [set() for n in range(N)]
    for c in C:
        lst[c[0]-1].add(c[1])
        lst[c[1]-1].add(c[0])

    for i in range(len(lst)):
        for s in lst[i]:
            lst[i] = lst[i] | lst[s-1]
    print(max({len(st) for st in lst}))


if __name__ == '__main__':
    N, M = list(map(int, input().split()))
    C = set()
    for _ in range(M):
        C.add(tuple(map(int, input().split())))
    main(N, M, C)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
n, m = map(int, input().split())
g = defaultdict(set)

if m == 0:
    print(n)

else:
    for _ in range(m):
        a, b = map(int, input().split())
        g[a - 1].add(b - 1)
        g[b - 1].add(a - 1)


    visited = [False]*n
    ans = 0
    for i in range(n):
        if not visited[i]:
            # cc = []
            count = 1
            q = deque([i])
            visited[i] = 1
            while q:
                node = q.pop()
                for nbr in g[node]:
                    if not visited[nbr]:
                        q.append(nbr)
                        visited[nbr] = True
                        count += 1
                
            # ans = max(ans, len(cc))
            ans = max(ans, count)

    print(ans)

Submission status: Wrong Answer

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

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

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

def unite(x, y):
  x = find(x)
  y = find(y)
  if x > y:
    par[x] = y
    # find(x)
  else:
    par[y] = x
    # find(y)
if M > 0:  
  for i in range(M):
    a, b = map(int, input().split())
    unite(a - 1, b - 1)
  
  ans = [0] * N
  Max = 0
  # for i in range(N):
  #   find(i)
  for i in par:
    ans[i] += 1
  for i in ans:
    if i > Max:
      Max = i
  
  print(Max)
else:
  print(1)

Submission status: Wrong Answer

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

dic = dict()
dicSet = set()
dicList = dict()
count = 0

ans = 0

for i in range(m):
    a,b = list(map(int,input().split()))
    if not(a in dicSet) and not(b in dicSet):
        dic[count] = set()
        dic[count].add(a)
        dic[count].add(b)
        dicList[a] = count
        dicList[b] = count
        count += 1
        dicSet.add(a)
        dicSet.add(b)

    elif a in dicSet and b in dicSet:
        dic[count] = dic[dicList[a]] | dic[dicList[b]]
        dicList[a] = count
        dicList[b] = count
        count += 1

    elif a in dicSet:
        dic[dicList[a]].add(b)
        dicList[b] = dicList[a]
        dicSet.add(b)
    else:
        dic[dicList[b]].add(a)
        dicList[a] = dicList[b]
        dicSet.add(a)


ans = 1

#print(dic)

for i in range(0,count):
    long = len(dic[i])
    if long > ans:
        ans = long

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
n, m = map(int, input().split())
g = defaultdict(set)

if m == 0:
    print(n)

else:
    for _ in range(m):
        a, b = map(int, input().split())
        g[a - 1].add(b - 1)
        g[b - 1].add(a - 1)


    visited = [False]*n
    ans = 0
    for i in range(n):
        if not visited[i] and i in g:
            # cc = []
            count = 1
            q = deque([i])
            visited[i] = 1
            while q:
                node = q.pop()
                for nbr in g[node]:
                    if not visited[nbr]:
                        q.append(nbr)
                        visited[nbr] = True
                        count += 1
                
            # ans = max(ans, len(cc))
            ans = max(ans, count)

    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, M = [int(x) for x in input().split()]

friends_map = {}

for m in range(M):
    a, b = [int(x) - 1 for x in input().split()]

    if a in friends_map:
        friends_map[a].add(b)
    else:
        friends_map[a] = {b}

    if b in friends_map:
        friends_map[b].add(a)
    else:
        friends_map[b] = {a}

stack = deque()
stack.appendleft((0, 1))

remain_user = {i for i in range(N)}

max_group = 0
while stack:
    now, group = stack.popleft()
    remain_user.discard(now)

    if now in friends_map:
        for next_node in friends_map[now]:
            add_num = len(friends_map[now])
            if next_node in remain_user:
                stack.appendleft((next_node, group+add_num))
                remain_user.discard(next_node)

    if not stack:
        if group > max_group:
            max_group = group

        if remain_user:
            stack.appendleft((remain_user.pop(), 1))

print(max_group)

Submission status: Wrong Answer

Solution code:
from collections import Counter

N,M = map(int,input().split())
num = [i for i in range(N+1)]
ab = [list(map(int,input().split())) for _ in range(M)]

ab.sort()

for a,b in ab:
    m = min(num[a],num[b])
    num[a] = num[b] = m

print(max(Counter(num).values()))

Submission status: Wrong Answer

Solution code:
strn,strm = input().split()
n = int(strn)
m = int(strm)
 
strab = []
 
for i in range(m):
  strab.append(input())
 
#strab.sort()
 
ab = []
for i in range(m):
  a , b = strab[i].split()
  if a < b:
    ab.append([int(a),int(b)])
  else :
    ab.append([int(b),int(a)])
 
ab.sort()
#print(ab)
 
num_friends = 0
friends = []
remain = list(range(m))
 
while True:  
  num_friends = max(len(friends),num_friends)
  
  if len(remain) == 0:
    break
 
  friends = []
  #print(remain)
  removelist = []
  for i in remain:
    # はじめの組み合わせなら、1番目を追加
    if friends == []:
      #print('から')
      friends.append(ab[i][0])
    if ab[i][0] in friends:
      #print('追加' + str(ab[i][1]))
      # 友達リストに組み合わせの２番目を追加する。
      if ab[i][1] in friends:
        pass
      else:
        friends.append(ab[i][1])
      # 残りリストからは消す。
      # remain.remove(i)
      removelist.append(i)
    else:
      pass
      #print('追加しない')
    #print('友達リスト' + str(friends))
  for i in removelist:
    remain.remove(i)
    
print(num_friends)
    
#print(strab)

Submission status: Wrong Answer

Solution code:
import sys,os
import math
from fractions import Fraction 
from collections import defaultdict
from random import randint

# sys.stderr=open('err.txt','w')
# sys.stdout=open('output.txt','w')
# sys.stdin=open('input.txt','r')

def linput():
    return list(minput())

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

###############################################################

def make_set(a):
    parent[a]=a

def find_set(a):
    if parent[a]==a:
        return a
    parent[a]=find_set(parent[a])
    return parent[a]

def union_set(a,b):
    x=find_set(a)
    y=find_set(b)
    if x==y:
        return
    elif x>y:
        parent[x]=y
    else:
        parent[y]=x

n,m=minput()
parent=[0]*n
for i in range(n):
    make_set(i)
ans=0
for i in range(m):
    x,y=minput()
    if x>y:
        x,y=y,x
    x=x-1
    y=y-1
    union_set(x,y)
# print(parent)
di = defaultdict(int)
for i in parent:
    di[i]+=1
# print(di)
for k,v in di.items():
    ans=max(ans,v)
print(ans)

Submission status: Wrong Answer

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

for i, ele in enumerate(ls):
    for j in range(i+1,len(ls)):
        ele = list(ele)
        zero = ele[0]
        one = ele[1]
        if zero in ls[j]:
            ls[j].add(ele[1])
            continue
        if one in ls[j]:
            ls[j].add(ele[0])
            continue
ans = 0
for ele in ls:
    if ans < len(ele):
        ans = len(ele)
print(ans)

Submission status: Wrong Answer

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

grpno=[i for i in range(N)]
ngrp=[1 for i in range(N)]
members=[[i] for i in range(N)]

ngrp_max=1

for i in range(M):
    p,q = list(map(int,input().split()))

    if grpno[p-1] > grpno[q-1]:
        r = p
        p = q
        q = r
    
    if grpno[p-1] != grpno[q-1]:
        ngrp[grpno[p-1]] += ngrp[grpno[q-1]]
        ngrp[grpno[q-1]] = 0

        for j in members[q-1]:
            grpno[j] = grpno[p-1]            

        members[p-1] += members[q-1]
        members[q-1]=[]                

        if ngrp_max < ngrp[grpno[p-1]]:
            ngrp_max = ngrp[grpno[p-1]]
   

# for i in range(N):
#     print("i=",i)
#     print(members[i])
#     print("group_no=",grpno[i])
#     print("#member=",ngrp[i])

print(ngrp_max)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
A = []*m
B = []*m
for i in range(m):
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)
S = [set() for i in range(n)]
#print(S)
for i in range(m):
    S[A[i]-1].add(B[i])
    S[B[i]-1].add(A[i])
    #print(S)

ans = 0
for i in range(n):
    ans = max(ans, len(S[i]))

print(str(ans + 1))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
if M == 0:
    print(1)
else:
    D = {}
    count = 0
    friends = []
    while count < M:
        A, B = map(int,input().split())
        if A not in D and B not in D:
            D[A] = len(friends)
            D[B] = D[A]
            friends += [[A,B]]
        elif A in D and B not in D:
            friends[D[A]] += [B]
            D[B] = D[A]
        elif B in D and A not in D:
            friends[D[B]] += [A]
            D[A] = D[B]
        count += 1
    print(max(len(f) for f in friends))

Submission status: Wrong Answer

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

li = list(set() for _ in range(m))

all_set = set(range(1,n +1))

count = -1
for _ in range(m):
    flag = False
    one, two = map(int, input().split())
    for i in range(count):
        if one in li[i] or two in li[i]:
            li[i].add(one)
            li[i].add(two)
            try:
                all_set.remove(one)
            except:
                pass
            try:
                all_set.remove(two)
            except:
                pass
            flag = True
            break
    if flag is False:
        count += 1
        li[count].add(one)
        li[count].add(two)
        try:
             all_set.remove(one)
        except:
            pass
        try:
            all_set.remove(two)
        except:
            pass

max = 0
for j in li:
    if max < len(j):
        max = len(j)
if max < len(all_set):
    max = len(all_set)
print(max)

Submission status: Wrong Answer

Solution code:

from collections import defaultdict, deque
n, m = map(int, input().split())
g = defaultdict(list)

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


visited = [False]*n
ans = 0
for i in range(n):
    if not visited[i] and i in g:
        # cc = []
        count = 1
        q = deque([i])
        visited[i] = 1
        while q:
            node = q.pop()
            for nbr in g[node]:
                if not visited[nbr]:
                    q.append(nbr)
                    visited[nbr] = True
                    count += 1
            
        # ans = max(ans, len(cc))
        ans = max(ans, count)

print(ans)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
belong = [-1 for _ in range(n)]
graph = []
for _ in range(m):
    #print(graph)
    a, b = map(int, input().split())
    if belong[a-1] == -1 and belong[b-1] == -1:
        belong[a-1] = a-1
        belong[b-1] = a-1
        graph.append({a-1,b-1})
    else:
        if belong[a-1] != -1:
            belong[b-1] = a-1
            for g in graph:
                if a-1 in g:
                    g.add(a-1)
                    g.add(b-1)
        if belong[b-1] != -1:
            belong[a-1] = b-1
            for g in graph:
                if b-1 in g:
                    g.add(a-1)
                    g.add(b-1)


    
max_g = 0
for g in graph:
    if len(g)>max_g:
        max_g = len(g)
print(max_g)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
friends_set_list = [set() for i in range(N+1)]
ans = 0
for i in range(M):
    A, B = map(int, input().split())
    friends_set_list[A].add(B)
    friends_set_list[B].add(A)
    friends_set_list[A] |= friends_set_list[B]
    friends_set_list[B] |= friends_set_list[A]
    num = len(friends_set_list[A])
    if ans < num:
        ans = num

print(ans)

Submission status: Wrong Answer

Solution code:
N,M,*A=map(int,open(0).read().split())
d={}
ss=[]
for i in range(0,len(A),2):
    a=A[i]
    b=A[i+1]
    if a in d:
        d[a].add(b)
        if b in d:
            d[b].add(a)
        else:
            d[b]=d[a]
    elif b in d:
        d[b].add(a)
        d[a]=d[b]
    else:
        s=set([a,b])
        ss.append(s)
        d[a]=d[b]=s
print(max([1]+[len(s) for s in ss]))

Submission status: Wrong Answer

Solution code:
N, M=map(int, input().split())
s = set([])
for _ in range(M):
  a=list(map(int, input().split()))
  a.sort()
  if tuple(a) not in s:
    s.add(tuple(a))
ans=0
while s:
  tmp=set(s.pop())
  flag=True
  after = list(s)
  while flag:
    for i in list(s):
      if not set(tmp).isdisjoint(set(i)):
        tmp = set(tmp) | set(i)
        s.discard(i)
    if after==list(s):
      flag=False
    else:
      after=list(s)
  ans=max(ans, len(tmp))
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque
n, m = map(int, input().split())
g = defaultdict(set)

for _ in range(m):
    a, b = map(int, input().split())
    g[a - 1].add(b - 1)
    g[b - 1].add(a - 1)


visited = [False]*n
ans = 0
for i in range(n):
    if not visited[i] and i in g:
        # cc = []
        count = 1
        q = deque([i])
        visited[i] = 1
        while q:
            node = q.pop()
            for nbr in g[node]:
                if not visited[nbr]:
                    q.append(nbr)
                    visited[nbr] = True
                    count += 1
            
        # ans = max(ans, len(cc))
        ans = max(ans, count)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

    m = min(num[a],num[b])
    num[a] = num[b] = m

print(max(Counter(num).values()))

Submission status: Wrong Answer

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

dic = dict()
dicSet = set()
dicList = dict()
count = 0

ans = 0

for i in range(m):
    a,b = list(map(int,input().split()))
    if not(a in dicSet) and not(b in dicSet):
        dic[count] = set()
        dic[count].add(a)
        dic[count].add(b)
        dicList[a] = count
        dicList[b] = count
        count += 1
        dicSet.add(a)
        dicSet.add(b)

    elif a in dicSet and b in dicSet:
        dic[count] = dic[dicList[a]] | dic[dicList[b]]
        dicList[a] = count
        dicList[b] = count
        count += 1

    elif a in dicSet:
        dic[dicList[a]].add(b)
        dicList[b] = dicList[a]
    else:
        dic[dicList[b]].add(a)
        dicList[a] = dicList[b]


ans = 1


for i in range(0,count):
    long = len(dic[i])
    if long > ans:
        ans = long

print(ans)

Submission status: Wrong Answer

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

I = [-1] * (n+1)
x = [[]]
for _ in range(m):
    a,b = map(int, input().split())
    if (I[a]!=-1 and I[b]!=-1) or a == b: continue

    if x == [[]]:
        x[0] += [a,b]
        I[a]=I[b]=0
    else:
        if I[a]==-1 and I[b]==-1:
            x.append([a,b])
            I[a]=I[b] = len(x)-1
        elif I[a]!=-1:
            x[I[a]].append(b)
            I[b] = I[a]
        elif I[b]!=-1:
            x[I[b]].append(a)
            I[a] = I[b]

ans = 0
for i in x:
    ans = max(ans, len(i))
print(ans if m != 0 else 1)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

c = [list(map(int, input().split())) for i in range(M)]

g = np.zeros(N, dtype = int)
n = np.zeros(N, dtype = int)

mylist = []

num = 1

for i in range(len(c)):
    
    if g[c[i][0]-1] != 0 and g[c[i][1]-1] != 0:
        if g[c[i][0]-1] == g[c[i][1]-1]:
            pass
        else:
            mylist.append((g[c[i][0]-1]-1, g[c[i][1]-1]-1))
    elif g[c[i][0]-1] != 0:
        g[c[i][1]-1] = g[c[i][0]-1]
        n[g[c[i][0]-1]-1] += 1
    elif g[c[i][1]-1] != 0:
        g[c[i][0]-1] = g[c[i][1]-1]
        n[g[c[i][1]-1]-1] += 1
    else:
        g[c[i][0]-1] = num
        g[c[i][1]-1] = num
        n[num-1] += 2
        num += 1
        
for i in range(len(mylist)):
    a = n[mylist[i][0]]
    b = n[mylist[i][1]]
    n[mylist[i][0]] += b
    n[mylist[i][1]] += a

print(max(n) or 1)