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


class UnionFindBySize():
    def __init__(self, n):
        self.parents = list(range(n))
        self.size = [1] * n

    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.size[x] < self.size[y]:
            self.size[y] += self.size[x]
            self.parents[x] = y
        else:
            self.size[x] += self.size[y]
            self.parents[y] = 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(self):
      return max([len(self.members(r)) for r in self.roots()])
    
uf = UnionFindBySize(N)


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

  
#print(uf.parents)
c = collections.Counter(uf.parents)
print(c.most_common()[0][1])

Submission status: Wrong Answer

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

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

G = {}
for i in range(0, M):
  ai, bi = map(int, readline().split())
  if ai in G:
    if type(G[ai]) is int:
      G[G[ai]].add(bi)
    else:
      G[ai].add(bi)
  elif bi in G:
    if type(G[bi]) is int:
      G[G[bi]].add(ai)
    else:
      G[bi].add(ai)
  else:
    G[ai] = {ai, bi}
    G[bi] = ai
ans = 0
for i in G:
  if type(G[i]) is not int:
    if len(G[i]) > ans:
      ans = len(G[i])
print(ans)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]
ab_set = []
for i in ab:
  ab_set.append([min(i), max(i)])
ab_set = list(map(tuple, ab_set))
ab_set = list(set(ab_set))
graph = [[] for _ in range(n + 1)]

for i in ab_set:
    graph[i[0]].append(i[1])
    graph[i[1]].append(i[0])

graph = graph[1:]
numlist = []

for i in graph:
  numlist.append(len(i))

print(max(numlist)+1)

Submission status: Wrong Answer

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

if M == 0 or M == 1:
  print(M * 2)
  exit()

r = [input().split()]

for i in range(1, M):
  a, b = input().split()
  for j in range(len(r)):
    if a in r[j] or b in r[j]:
      r[j].append(a)
      r[j].append(b)
    else:
      r.append([a, b])

ans = 0
for i in range(len(r)):
  ans = max(ans, len(set(r[i])))

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


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

    if p > q:
        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
        grpno[q-1]=grpno[p-1]
   
        # members[p-1] += members[q-1]

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

print(max(ngrp))

Submission status: Wrong Answer

Solution code:
from typing import Set, List, Dict

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

G: List[Set[int]] = []
lookup_tab: Dict[int, int] = {}
for _ in range(M):
    a, b = map(int, input().split())
    if a not in lookup_tab and b not in lookup_tab:
        G.append({a, b})
        lookup_tab[a] = len(G) - 1
        lookup_tab[b] = len(G) - 1
    elif a in lookup_tab:
        gid = lookup_tab[a]
        lookup_tab[b] = gid
        G[gid].add(b)
    elif b in lookup_tab:
        gid = lookup_tab[b]
        lookup_tab[a] = gid
        G[gid].add(a)

if G:
    print(max([len(g) for g in G]))
else:
    print(1)

Submission status: Wrong Answer

Solution code:
from copy import deepcopy


def main():
    n, m = map(int, input().split())
    groups = [None] * n
    groups_member = [None] * n
    num_groups = 0
    for i in range(m):
        a, b = map(int, input().split())
        if groups[a - 1] is None and groups[b - 1] is None:
            groups[a - 1] = num_groups
            groups[b - 1] = num_groups
            groups_member[num_groups] = set([a-1, b-1])
            num_groups += 1
        elif groups[a - 1] is None:
            groups[a - 1] = groups[b - 1]
            groups_member[groups[b - 1]].add(a - 1)
        elif groups[b - 1] is None:
            groups[b - 1] = groups[a - 1]
            groups_member[groups[a- 1]].add(b - 1)
        else:
            tmp = groups[b - 1]
            _tmp = deepcopy(groups_member[tmp])
            groups_member[tmp] |= set(groups_member[groups[a - 1]])
            groups_member[groups[a - 1]] |= set(_tmp)
        # print(groups)
        # print(groups_member)
    
    max_people = 0
    for m in groups_member:
        if m is None:
            break
        if max_people < len(m):
            max_people = len(m)
    if max_people == 0:
        max_people = 1

    print(max_people)
    # print(groups)
    # print(groups_member)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split(' '))
d = {}
seen = set()
for i in range(m):
  a, b = map(int, input().split(' '))
  if a not in seen and b not in seen:
    seen.add(a)
    seen.add(b)
    d[a] = [b]
  elif a in seen and b not in seen:
    seen.add(b)
    if a in d:
      d[a].append(b)
    else:
      for _ in d.values():
        if a in _:
          _.append(b)
  elif a not in seen and b in seen:
    seen.add(a)
    if b in d:
      d[b].append(a)
    else:
      for _ in d.values():
        if b in _:
          _.append(a)
  else:
    continue
    
ans = 0
for _ in d.values():
  ans = max(ans, len(_))
  
print(ans+1)

Submission status: Wrong Answer

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

g = [0]*N
n = [0]*N

num = 1

for i in range(len(c)):
    
    if g[c[i][0]-1] != 0 and g[c[i][1]-1] != 0:
        pass
    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

print(max(n) or 1)

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

n, m = map(int, readline().split())
ab = map(int, read().split())

graph = [[] for _ in range(n+1)]
for a, b in zip(ab, ab):
    if b in graph[a]:
        continue
    else:
        graph[a].append(b)
    if a in graph[b]:
        continue
    else:
        graph[b].append(a)

max_cnt = 0

for i in range(len(graph)):
    cnt = len(graph[i])
    if cnt > max_cnt:
        max_cnt = cnt

print(max_cnt + 1)

Submission status: Wrong Answer

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

info = set()
for i in range(m):
  a,b = map(int,input().split())
  a -= 1
  b -= 1
  
  if a > b:
    a,b = b,a
    
  info.add((a,b))
  
for v in info:
  a,b = v
  
  array[a].union(array[b])
  array[a].add(b)

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

print(ans)

Submission status: Wrong Answer

Solution code:

[N,M] = list(map(int,input().split()))

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

fri = [[] for i in range(N+1)]
for i in range(M):
    fri[AB[i][0]].append(AB[i][1])
    fri[AB[i][1]].append(AB[i][0])

for i in range(1,N+1):
    fri[i] = set(fri[i])
# print('fri:',fri)

out=[]
for i in range(1,N+1):
    out.append(len(fri[i]))
print(max(out)+1)

Submission status: Wrong Answer

Solution code:
from collections import deque

n,m=map(int,input().split())
f=[[] for i in range(n)]
check=[False for i in range(n)]
for i in range(m):
    a,b=map(int,input().split())
    a-=1
    b-=1
    f[a].append(b)
    f[b].append(a)
ans=1
for i in range(n):
    if not check[i]:
        d=deque()
        check[i]=True
        d.append(i)
        tmp=1
        while d:
            v=d.popleft()
            for j in f[i]:
                if check[j]:
                    continue
                check[j]=True
                tmp+=1
                d.append(j)
        ans=max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import copy

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

f=[]
for i in range(n+1):
    f.append([])


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

    fa=copy.copy(f[a])
    f[a].append(b)
    f[a].extend(f[b])   
    
    f[b].append(a)
    f[b].extend(fa)

    f[a] = list(set(f[a]))
    f[b] = list(set(f[b]))

cm=0
for i in range(n+1):
    if i in f[i]:
        f[i].remove(i)
    if cm < len(f[i])+1:
        cm = len(f[i])+1

print(cm)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, Counter
from itertools import product, permutations, combinations, accumulate
from bisect import bisect_left, bisect
from heapq import heappop, heappush
from fractions import gcd
from math import ceil, floor


def input_list():
    return list(map(int, input().split()))


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


N, M = input_list()
li = []
for _ in range(M):
    a, b = input_list()
    if li == []:
        li.append(set([a, b]))
    else:
        is_exists = False
        for s in li:
            if a in s:
                s.add(b)
                is_exists = True
                break
            elif b in s:
                s.add(a)
                is_exists = True
                break
        if not is_exists:
            li.append(set([a, b]))
ans = 0
for s in li:
    ans = max(ans, len(s))
print(ans)

Submission status: Wrong Answer

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

f = [[1] for i in range(N)]
for i in range(M):
    # print(f)
    A,B = list(map(int,input().split()))
    A -= 1
    B -= 1
    if id(f[A]) != id(f[B]):
        f[A][0] += f[B][0]
        f[B] = f[A]
ans = 0
for i in range(N):
    ans = max([f[i][0],ans])
print(ans)

Submission status: Wrong Answer

Solution code:
class UnionFind:
  def __init__(self, n):
    self.parent = [i for i in range(n)]
    self.height = [0 for i in range(n)]
        
  def get_root(self, i):
    if self.parent[i] == i:
      return i
    else:
      self.parent[i] = self.get_root(self.parent[i])
      return self.parent[i]
            
  def unite(self, i, j):
    root_i = self.get_root(i)
    root_j = self.get_root(j)
    if root_i != root_j:
      if self.height[root_i] < self.height[root_j]:
        self.parent[root_i] = root_j
      else:
        self.parent[root_j] = root_i
        if self.height[root_i] == self.height[root_j]:
          self.height[root_i] += 1
                
  def is_in_group(self, i, j):
    if self.get_root(i) == self.get_root(j):
      return True
    else:
      return False
          
N, M = map(int, input().split())
uf = UnionFind(N)
for i in range(M):
  a, b = map(int, input().split())
  uf.unite(a-1, b-1)
X = [0]*N
for i in range(N):
  X[uf.parent[i]] += 1
print(max(X))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
index = 1
group = [0] * (n + 1) # 何番目のグループにいるか
groups = [set()] # グループ
for i in range(m):
  a, b = map(int, input().split())
  _a = group[a]
  _b = group[b]
  if _a == 0 and _b == 0: # 新しいグループ
    group[a] = index
    group[b] = index
    groups.append({a,b})
    index += 1
  elif _a == 0: # aが新人
    group[a] = _b
    groups[_b].add(a)
  elif _b == 0: # bが新人
    group[b] = _a
    groups[_a].add(b)
  elif _a < _b: # a,bが別グループに所属:aのいるグループをbのいるグループに移行
    for g in groups[_b]:
      group[g] = _a
    groups[_a] = groups[_a] | groups[_b]
  elif _a > _b: # a,bが別グループに所属:bのいるグループをaのいるグループに移行
    for g in groups[_a]:
      group[g] = _b
    groups[_b] = groups[_b] | groups[_a]
ans = max(map(lambda g: len(g), groups))
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

def solve() :
    n, m = map(int, input().split())
    info = []
    member = list(range(n+1))
    member_flag = [True] * (n+1)
    for _ in range(m) :
        a, b = map(int, input().split())
        if a < b :
            if not [a, b] in info :
                if member_flag[b] :
                    member[b] = a
                    member_flag[b] = False
        else :
            if not [b, a] in info :
                if member_flag[a] :
                    member[a] = b
                    member_flag[a] = False

    member.remove(0)
    cnt = Counter(member).most_common()

    print(cnt[0][1])

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import collections
def solve():
  N,M = map(int,input().split())
  ABlist = [[0,0] for _ in range(M)]
  WA = [0]*N
  for i in range(N):
    WA[i] = i+1
  for i in range(M):
    A, B = map(int,input().split())
    if A>B:
      A,B =B,A
    ABlist[i][0] = A
    ABlist[i][1] = B
  ABlist.sort(key=lambda x: (x[0],x[1]))
  for i in range(M):
    A = ABlist[i][0]
    B = ABlist[i][1]
    WA[B-1] = WA[A-1]
  c = collections.Counter(WA)
  print(c.most_common()[0][1])
solve()

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
myd = {n:[] for n in range(N)}
for m in range(M):
    A, B = map(int, input().split())
    if myd[A-1] not in [B-1]: 
        myd[A-1] += [B-1]
    if myd[B-1] not in [A-1]: 
        myd[B-1] += [A-1]
#print(myd)
myans = 0
for n in range(N):
    myset = len(set(myd[n]))
    if myset > myans:
        myans =myset 
print(myans + 1)

Submission status: Wrong Answer

Solution code:
import collections

N,M = list(map(int, input().split()))
team_table = [0 for i in range(N)]
color = 0
for i in range(M):
    A,B = list(map(int, input().split()))
    if (team_table[A-1] == 0)&(team_table[B-1] == 0):
        color += 1
        team_table[A-1] = color 
        team_table[B-1] = color
    elif (team_table[A-1] == 0)&(team_table[B-1] != 0):
        team_table[A-1] = team_table[B-1]
    elif (team_table[A-1] != 0)&(team_table[B-1] == 0):
        team_table[B-1] = team_table[A-1]
        
ans = collections.Counter(team_table).most_common()[0][1]
        
print(ans)

Submission status: Wrong Answer

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

indexlist = [0 for i in range(n)]
indexnum = 1
for _ in range(m):
    a,b = map(int,input().split())
    x = indexlist[a-1]
    y = indexlist[b-1]
    if x == 0 and y == 0:
        indexlist[a-1] = indexnum
        indexlist[b-1] = indexnum
        indexnum += 1
    elif x == 0 and y != 0:
        indexlist[a-1] = y
    elif x != 0 and y == 0:
        indexlist[b-1] = x

c = collections.Counter(indexlist)
often = c.most_common()[0][1]
print(often)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
line = [[] for i in range(N)] # 
for i in range(M):
    x,y = map(int,input().split())
    line[x-1].append(y-1)
    line[y-1].append(x-1)
output = 0
for i in range(N):
    if len(set(line[i])) > output:
        output = len(set(line[i]))
print(output+1)

Submission status: Wrong Answer

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

for n in range(N+1):
  friends.append([])

for m in range(M):
  a, b = map(int, input().split())
  if a > b:
    tmp = b
    b = a
    a = tmp
  friends[a].extend([b])
ans = 0
for i in friends:
  ans = max(ans, len(list(set(i))))

print(ans+1)

Submission status: Wrong Answer

Solution code:
def abc177d():
    n, m = map(int, input().split())
    check = [-1] * n
    pair = []
    group_no = 0
    group = {}
    for i in range(m):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        if check[a] == check[b]:
            if check[a] == -1:
                group[group_no] = {a, b}
                check[a] = check[b] = group_no
                group_no += 1
        elif check[a] == -1:
            group[check[b]].add(a)
        elif check[b] == -1:
            group[check[a]].add(b)
        else:
            re = min(check[a], check[b])
            de = max(check[a], check[b])
            for v in group[de]:
                check[v] = re
            group[re] = group[re] | group[de]
            del group[de]
    max_val = 0
    for k in group:
        max_val = max(max_val,len(group[k]))
    print(max_val)
abc177d()

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self, n):
        self.parent = [i for i in range(n)]
        self.height = [0 for _ in range(n)]
        
    def get_root(self, i):
        if self.parent[i] == i:
            return i
        else:
            self.parent[i] = self.get_root(self.parent[i])
            return self.parent[i]
            
    def unite(self, i, j):
        root_i = self.get_root(i)
        root_j = self.get_root(j)
        if root_i != root_j:
            if self.height[root_i] < self.height[root_j]:
                self.parent[root_i] = root_j
            else:
                self.parent[root_j] = root_i
                if self.height[root_j] == self.height[root_j]:
                    self.height[root_i] += 1
                    
    def max_group_size(self):
        d = {}
        for i in self.parent:
            if i not in d:
                d[i] = 1
            else:
                d[i] += 1
        values = list(d.values())
        return max(values)
            
                    
    def is_in_group(self, i, j):
        if self.get_root(i) == self.get_root(j):
            return True
        else:
            return False
            

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

u = UnionFind(n)
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    u.unite(a, b)
ans = u.max_group_size()
print(ans)

Submission status: Wrong Answer

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

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

G = {}
for i in range(1, M):
  ai, bi = map(int, readline().split())
  if ai in G:
    if type(G[ai]) is int:
      G[G[ai]].add(bi)
    else:
      G[ai].add(bi)
  elif bi in G:
    if type(G[bi]) is int:
      G[G[bi]].add(ai)
    else:
      G[bi].add(ai)
  else:
    G[ai] = {ai, bi}
    G[bi] = ai
ans = 0
for i in G:
  if type(G[i]) is not int:
    if len(G[i]) > ans:
      ans = len(G[i])
print(ans)

Submission status: Wrong Answer

Solution code:
import collections

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))

    def find(self, 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 = self.find(x)
        y = self.find(y)
        if x == y:
            return
        else:
            self.parent[x] = y

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

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

count = collections.Counter(tree.parent).most_common(1)[0][1]
print(count)

Submission status: Wrong Answer

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

def main():
    n , m = map( int, input().split() )
    Sets = []

    Dicto = [ -1 for i in range(n + 1)]

    for i in range( m ):
        a , b = map( int , input().split() )
        if Dicto[a] == -1 and Dicto[b] == -1:
            Sets.append( set( [a,b] ) )
            x_i = len(Sets)
            Dicto[ a ] = x_i - 1
            Dicto[ b ] = x_i - 1
        elif Dicto[ a ] != -1:
            Sets[ Dicto[a] ].add( b )
            Dicto[ b ] = Dicto[a]
        elif Dicto[ b ] != -1:
            Sets[ Dicto[b] ].add( a )
            Dicto[ a ] = Dicto[b]

    
    max_len = 1

    for i in range( len(Sets) ):
        if max_len < len( Sets[i] ):
            max_len = len( Sets[i] )

    print(max_len)


main()

Submission status: Wrong Answer

Solution code:
(n,m),*e=[[*map(int,i.split())]for i in open(0)]
d=[[]for i in range(n+1)]
for i,j in e:
    d[i]+=[j]
    d[j]+=[i]
ans=0    
for i in range(n+1):
    a=set(d[i])
    for j in set(d[i]):
        a|=set(d[j])
    ans=max(ans,len(a))
print(ans)

Submission status: Wrong Answer

Solution code:
def resolve():
  N, M = map(int, input().split(" "))
  checked = [False] * (N + 1)
  # frend_labels = [0] * (N + 1)
  # frend_labels_counter = [0] * (N + 1)
  num_of_friend = [0] * (N + 1)
  friends = []
  for _ in range(N+1):
    friends.append(set())

  maximam_friends = 0
  for _ in range(M):
    A, B = map(int, input().split(" "))
    friends[A].add(B)
    friends[B].add(A)

    if num_of_friend[A] == 0 and num_of_friend[B] == 0:
      num_of_friend[A] += 1
      num_of_friend[B] += 1
    elif num_of_friend[A] == 0:
      num_of_friend[A] = num_of_friend[B]
      num_of_friend[B] += 1
      for friend in friends[B]:
        num_of_friend[friend] += 1
    elif num_of_friend[B] == 0:
      num_of_friend[B] = num_of_friend[A]
      num_of_friend[A] += 1
      for friend in friends[A]:
        num_of_friend[friend] += 1

    if num_of_friend[A] > maximam_friends:
      maximam_friends = num_of_friend[A]

  # for friend in friends:
  #   print(friend)
  print(maximam_friends+1)

if __name__ == "__main__":
  resolve()

Submission status: Wrong Answer

Solution code:
#coding: utf-8
from collections import defaultdict

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

uniq_grp = 0
grp = defaultdict(int)
ngrp = defaultdict(int)

ret = 0

for i in range(M):
    a, b = [int(x) for x in input().split()]
    
    if a in grp:
        if b in grp:
            continue
        else:
            grp[b] = grp[a]
            ngrp[grp[a]] += 1
            ret = max(ret, ngrp[grp[a]])
            continue
    elif b in grp:
        if a in grp:
            continue
        else:
            grp[a] = grp[b]
            ngrp[grp[a]] += 1
            ret = max(ret, ngrp[grp[a]])
            continue
    else:
        grp[a] = uniq_grp
        grp[b] = grp[a]
        ngrp[grp[a]] += 2
        ret = max(ret, ngrp[grp[a]])
        uniq_grp += 1
print(ret)

Submission status: Wrong Answer

Solution code:
# union find, bfs

import sys
from collections import deque
si = sys.stdin.readline


def main():
    [n, m] = [int(e) for e in si().split()]
    if not m:
        print(n)
        return

    visited = [False]*(n+1)
    fm = [set() for _ in range(n+1)]
    mx, comp = 0, 0

    while m:
        m -= 1
        [a, b] = [int(e) for e in si().split()]
        fm[a].add(b)
        fm[b].add(a)

    for i in range(1, n+1):
        if visited[i]:
            continue
        visited[i] = True
        comp += 1
        q = deque()
        q.append(i)
        cnt = 1
        while q:
            curr = q.popleft()
            for e in fm[curr]:
                if not visited[e]:
                    visited[e] = True
                    q.append(e)
                    cnt += 1
        mx = max(mx, cnt)
    if comp == 1:
        print(1)
    else:
        print(mx)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,M =map(int,input().split())
Numbers =list(range(N+1))
for _ in range(M):
    a, b = map(int, input().split())
    if a >b:
      A=b
      B=a
    else:
      A=a
      B=b
    Numbers[B]=Numbers[A]
C =Numbers.count(1)
for k in range(M):
  D =Numbers.count(k)
  if D>C:
    C = D
  else:
    C = C
  k = k +1
print(C)

Submission status: Wrong Answer

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

friends = []
for _ in range(n):
    friends.append(set([]))

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

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

Submission status: Wrong Answer

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

dic = dict()
dicSet = set()

for i in range(m):
    a,b = list(map(int,input().split()))
    if a in dicSet:
        dic[a].add(b)
    else:
        dic[a] = set()
        dic[a].add(b)
        dicSet.add(a)
    if b in dicSet:
        dic[b].add(a)
    else:
        dic[b] = set()
        dic[b].add(a)
        dicSet.add(b)

ans = 1

for i in dicSet:
    long = len(dic[i]) + 1
    if long > ans:
        ans = long


print(ans)

Submission status: Wrong Answer

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

dp=[0 for n in range(N)]
i=1
fusion=[]
for _ in range(M):
    A,B=map(int,input().strip().split())
    if dp[A-1]==0 and dp[B-1]==0:
        dp[A-1]=i
        dp[B-1]=i
        i+=1
    else:
        if dp[A-1]!=0 and dp[B-1]==0:
            dp[B-1]=dp[A-1]
        elif dp[B-1]!=0 and dp[A-1]==0:
            dp[A-1]=dp[B-1]
        else:
            fusion.append((dp[A-1],dp[B-1]))

l=[0 for n in range(i)]
for n in range(N):
    if dp[n]!=0:
        l[dp[n]-1]+=1
if not fusion:
    print(max(l))
else:
    MAX=0
    for e in fusion:
        if e[0]!=e[1]:
            MAX=max(MAX,l[e[0]-1]+l[e[1]-1])
        else:
            MAX=max(MAX,l[e[0]-1])
    print(MAX)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, M = list(map(int, input().split()))
groups = {i:-1 for i in range(N)}
cnt = 0
for _ in range(M):
    a, b = list(map(int, input().split()))
    a-=1
    b-=1
    if groups[a]==-1 and groups[b]==-1:
        groups[a] = cnt
        groups[b] = cnt
        cnt+=1
    elif groups[a]==-1:
        groups[a] = groups[b]
    elif groups[b]==-1:
        groups[b] = groups[a]
if cnt==0:
    print(1)
else:
    for n,m in Counter(groups.values()).most_common():
        if n==-1:
            continue
        print(m)
        break

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict

graph = defaultdict(list)

n, m = map(int, sys.stdin.readline().split())

for _ in range(m):
    a,b = map(int, sys.stdin.readline().split())

    if not b in graph[a]:
        graph[a].append(b)
    if not a in graph[b]:
        graph[b].append(a)

visited = set()
maxi = 0

def dfs(start):
    stk = [start]
    visited.add(start)
    temp = 1

    while stk:
        cur = stk.pop()

        for c in graph[cur]:
            if not c in visited:
                visited.add(c)
                stk.append(c)
                temp += 1

    return temp

for i in range(n):
    if i in graph and not i in visited:
        cnt = dfs(i)
        maxi = max(cnt, maxi)

print(maxi)

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10, gcd
from itertools import permutations, combinations, product, accumulate, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
#from fractions import gcd
from decimal import *
import heapq
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 1, 0, -1, 1, -1, -1, 1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
debugmode = True

def dfs(human, ignore):
    res = set([human])
    for n_human in r[human]:
        if n_human in ignore:
            continue
        n_ignore = set([i for i in ignore])
        n_ignore.add(n_human)
        for i in dfs(n_human, n_ignore):
            res.add(i)
    return res

n, m = MAP()
r = [set([]) for _ in range(n)]
for _ in range(m):
    a, b = MAP()
    a -= 1
    b -= 1
    if b < a:
        a, b = b, a
    r[a].add(b)
#print(r)
ans = 1
ans_set = set([])
for i in range(n):
    if i in ans_set:
        continue
    tmp = dfs(i, set([]))
    #print(i, tmp)
    ans = max(ans, len(tmp))
    for j in tmp:
        ans_set.add(j)
print(ans)

Submission status: Wrong Answer

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

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

    ans = max(map(len, groups))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
table = [set([x]) for x in range(n + 1)]

for _ in range(m):
    a, b = map(int, input().split())
    table[a] = table[b] = table[a] | table[b]

ans = 0
for j in table:
    ans = max(len(j), ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=[list(map(int,input().split())) for _ in range(m)]
d={k:set() for k in range(1,n+1)}
for a,b in l:
    for v in d[a]-{b}:
        d[v].add(b)
    d[a].add(b)
    for v in d[b]-{a}:
        d[v].add(a)
    d[b].add(a)
ans=0
for s in d.values():
    ans=max(ans,len(s)+1)
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
	N, M = [int(n) for n in input().split(" ")]
	friends = []
	for i in range(M):
		a, b = [int(x) for x in input().split(" ")]
		m = min([a, b])
		M = max([a, b])
		friends.append({
			"min" : m,
			"max" : M
		})
	friends.sort(key=lambda f: (f["min"], f["max"]))
	group = [-1] * N
	for f in friends:
		M = f["max"]
		m = f["min"]
		if group[m - 1] == group[M - 1] == -1:
			group[m - 1] = m - 1
			group[M - 1] = m - 1
		elif group[m - 1] == -1 and group[M - 1] != -1:
			group[m - 1] = group[M - 1]
		elif group[m - 1] != -1 and group[M - 1] == 1:
			group[M - 1] = group[m - 1]
		else:
			group[M - 1] = group[m - 1]

	group_member = [0] * N
	for i in range(len(group)):
		if group[i] == -1:
			group_member[i] += 1
		else:
			group_member[group[i]] += 1
	print(max(group_member))

main()