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())
F = [list(map(int, input().split())) for i in range(m)]


friends=[{-1}]
num=1
people=[-1]*n
ans=0

F2=[]

for f in F:
    if f[0]<f[1]:
        F2.append(f)
    else:
        F2.append([f[1],f[0]])

F2.sort()

for f in F2:
    a=f[0]-1
    b=f[1]-1
    #print(a,b)

    if people[a]==-1 and people[b]==-1:
        friends.append({a,b})
        people[a]=num
        people[b]=num
        num+=1

    elif people[a]!=-1 and people[b]!=-1 and people[a]!=people[b]:
        A=people[a]
        B=people[b]
        friends[A] = friends[A].union(friends[B])
        #for i in friends[B]:
            #people[i]=A

    elif people[a]!=-1 and people[b]==-1:
        friends[people[a]].add(b)
        people[b]=people[a]

    elif people[a]==-1 and people[b]!=-1:
        friends[people[b]].add(a)
        people[a]=people[b]

    #print(people)
    #print(friends)

for i in friends:
    if len(i)>ans:
        ans=len(i)
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit, stdin

input = stdin.readline
import copy

from collections import deque, Counter

# import numpy as np


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

D = [-1] * N

tmp = 0
for i in range(M):
    a, b = list(map(int, input().split()))
    a -= 1
    b -= 1
    if D[a] == -1 and D[b] == -1:
        D[a] = tmp
        D[b] = tmp
        tmp += 1
    elif D[a] == -1 and D[b] != -1:
        D[a] = D[b]
    elif D[a] != -1 and D[b] == -1:
        D[b] = D[a]
    elif D[a] != D[b]:
        for k in range(N):
            if D[k] == D[b]:
                D[k] = a

R = Counter(D)
print(R.most_common()[0][1])

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
AB = []
for _ in range(m):
    a, b = map(int, input().split())
    AB.append([a, b])

class UnionFind: # with rank
    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
        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

uf = UnionFind(n)
for a, b in AB:
    uf.union(a, b)

m = {}
for g in uf.par:
    if not g in m:
        m[g] = 0
    m[g] += 1

ans = 0
for k,v in m.items():
    ans = max(ans, v)
print(ans)

Submission status: Wrong Answer

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

friends = {}
for n in range(N):
  friends[n+1] = []

for ab in AB:
  a,b = ab
  friends[a].append(b)
  friends[b].append(a)

#print(friends)

belong = {}
team_No = 0
for n in friends:
  if n not in belong:
    belong[n] = team_No
    team_No += 1
  for f in friends[n]:
    belong[f] = belong[n]

#print(belong)

teams = {}
#print(teams)
for n in belong:
  #print(n, belong[n])
  if belong[n] not in teams:
    teams[belong[n]] = []
  teams[belong[n]].append(n)

#print(teams)

max_member = max([len(teams[i]) for i in teams])
print(max_member)

Submission status: Wrong Answer

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

l = []
A = 0
B = 0
ans = N
if M > 0:
    A, B = map(int, input().split())
    l.append((A, B))
for i in range(M-1):
    A, B = map(int, input().split())
    hit = None
    for tup in l:
        if A in tup or B in tup:
            if hit != None:
                hit = tuple(set(hit+tup+(A, B)))
            else:
                hit = tuple(set(tup+(A, B)))
            l.remove(tup)
    if hit != None:
        l.append(hit)
    else:
        l.append((A, B))

if len(l) > 0:
    ans = 0
for item in (l):
    if ans < len(item):
        ans = len(item)
print(ans)

Submission status: Wrong Answer

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

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


for i in range(m):
    a,b = map(int,input().split())
    fa = f[a]
    f[a] = f[a] | f[b]      
    fb = f[b] | fa

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

Submission status: Wrong Answer

Solution code:
from collections import Counter, defaultdict

N, M = map(int, input().split())
# AB = [list(map(int, input().split())) for _ in range(M)]

AB = []

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

    AB.append((a, b))

AB.sort(key=lambda x: (x[0], x[1]))

dic = defaultdict(list)
keys = []
for a, b in AB:
    if not keys or keys[-1] != a - 1:
        keys.append(a - 1)

    dic[a - 1].append(b - 1)

lst = [0] * N
c = 0
for a in keys:
    t = -1
    for b in dic[a]:
        if lst[b] != 0:
            t = lst[b]
            break

    if t == -1:
        c += 1
        for b in dic[a]:
            lst[b] = c

        lst[a] = c
    else:
        for b in dic[a]:
            lst[b] = t

        lst[a] = t

y = Counter(lst)

max_ = 0
for v in y.values():
    max_ = max(max_, v)

print(max_)

Submission status: Wrong Answer

Solution code:
N, M = (int(x) for x in input().split())
import collections
Parent = [i for i in range(N)]
def fp(i):
    global Parent
    if Parent[i] == i:
        return i
    else:
        Parent[i] = fp(Parent[i])
        return Parent[i]

for _ in range(M):
    a, b = (int(x) for x in input().split())
    Parent[max(a,b)-1] = fp(min(a,b)-1)

Groupsize = collections.defaultdict(lambda:0)
for i in range(N):
    i = fp(i)
    Groupsize[i] +=1
ans = max(gs for gs in Groupsize.values())
print(ans)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
con = []
ans = 0
for i in range(m):
    check = True
    b,c = map(int,input().split())
    if i == 0:
        con.append([b,c])
    else:
        for i in range(len(con)):
            if b in con[i]:
                check = False
                if c in con[i]:
                    break
                else:
                    con[i].append(c)
                    break
            elif c in con[i]:
                check = False
                con[i].append(b)
                break
        if check:
            con.append([b,c])
        ans = max(ans,len(con[i]))
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_group = 0
  for _ in range(M):
    A, B = map(int, input().split(" "))

    if num_of_friend[A] == 0 and num_of_friend[B] == 0:
      num_of_friend[A] = 1
      num_of_friend[B] = 1
      friends[A].add(A)
      friends[A].add(B)
      friends[B].add(A)
      friends[B].add(B)
    elif num_of_friend[A] == 0:
      friends[B].add(A)
      friends[A] = friends[B]
    elif num_of_friend[B] == 0:
      friends[A].add(B)
      friends[B] = friends[A]
    else:
      union = friends[A] | friends[B]
      friends[A] = union
      friends[B] = 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:
from collections import Counter

def solve():
    N, M = [int(i) for i in input().split()]
    F = [-1] * N
    for m in range(M):
        A, B = [int(i) for i in input().split()]
        if F[A - 1] == -1 and F[B - 1] == -1:
            F[A - 1] = m
            F[B - 1] = m
        elif F[A - 1] == -1 and F[B - 1] != -1:
            F[A - 1] = F[B - 1]
        elif F[A - 1] != -1 and F[B - 1] == -1:
            F[B - 1] = F[A - 1]
        else:
            for i in range(len(F)):
                if F[i] == F[A - 1] or F[i] == F[B - 1]:
                    F[i] = m
            F[A - 1] = m
            F[B - 1] = m
        if len(set(F)) == 1:
            print(len(F))
            exit()
    counter = Counter(F)
    print(max(counter.values()))

if __name__ == "__main__":
    solve()

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_group = 0
  for _ in range(M):
    A, B = map(int, input().split(" "))

    if num_of_friend[A] == 0 and num_of_friend[B] == 0:
      num_of_friend[A] = 1
      num_of_friend[B] = 1
      friends[A].add(A)
      friends[A].add(B)
      friends[B].add(A)
      friends[B].add(B)
    elif num_of_friend[A] == 0:
      friends[B].add(A)
      friends[A] = friends[B]
    elif num_of_friend[B] == 0:
      friends[A].add(B)
      friends[B] = friends[A]
    else:
      union = friends[A] | friends[B]
      friends[A] = union
      friends[B] = union

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

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

Submission status: Wrong Answer

Solution code:
from collections import deque

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

A = {}
for i in range(M):
    a, b = map(int, input().split())
    if a in A:
        A[a].append(b)
    else:
        A[a]=[b]
    if b in A:
        A[b].append(a)
    else:
        A[b]=[a]
queue=deque()
done=[]
max_num=1
for i in range(N):
    if i in done:
        break
    now_loop_len=0
    queue.append(i)
    while queue:
        now_node=queue.pop()
        if now_node in A:
            for j in A[now_node]:
                if j not in done and j not in queue:
                    queue.append(j)
        now_loop_len+=1
        done.append(now_node)
        print(queue)
    if max_num<now_loop_len:
        max_num=now_loop_len
print(max_num)

Submission status: Wrong Answer

Solution code:
# from collections import deque

n, m = map(int, input().split())
# # arr = [tuple(map(int, input().split())) for _ in range(m)]

# f = {a: set() for a in range(1, n+1)}
# for _ in range(m):
#     x, y = map(int, input().split())
#     f[x].add(y)
#     f[y].add(x)

# res = 0
# visited = set()
# stack = deque()
# for i in range(1, n+1):
#     if i in visited:
#         continue
#     visited = {i}
#     stack = deque([i])
#     while stack:
#         c = stack.pop()
#         for v in f[c]:
#             if v not in visited:
#                 visited.add(v)
#                 stack.append(v)
#     res = max(res, len(visited))

# print(res)

f = []
for _ in range(m):
    x, y = map(int, input().split())
    for f_ in f:
        if x in f_ or y in f_:
            f_.update((x, y))
            break
    else:
        f.append({x, y})

if m == 0:
    print(n)
else:
    print(max([len(f_) for f_ in f]))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
flist = []
ans = 1
for i in range(n):
  flist.append([i+1])
for j in range(m):
  a,b = map(int,input().split())
  MIN = min(a,b)
  MAX = max(a,b)
  flist[MIN-1].append(MAX)
for i in range(n):
  for j in flist[i]:
    flist[i] = flist[i] + flist[j-1]
  flist[i] = set(flist[i])
  score = len(flist[i])
  if ans < score:
    ans = score
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

print(len(max(rinsetu)))
# grp = 0
# color = ["white"] * (N + 1)
# color[0] = "black"
# for i in range(1, N + 1):
#     if color[i] == "white":
#         d = deque()
#         d.append(i)
#         a = set()
#         a.add(i)
#         while (d):
#             k = d.popleft()
#             color[k] = "black"
#             for j in rinsetu[k]:
#                 if color[j] == "white":
#                     d.append(j)
#                     a.add(j)
#         grp = max(grp, len(a))
# print(grp)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N, M = map(int, input().split(' '))
frend_dict = {}
info_list = []
index_list = []

flg = 0
for i in range(M):
    info = [int(n) for n in input().split(' ')]
    info.sort()
    
    if info in info_list:
        continue
    
    info_list.append(info)

    for n in range(2):
        if info[n] not in index_list:
            index_list.append(info[n])

        if info[n] not in frend_dict:
            frend_dict[info[n]] = [info[1-n]]
        else:
            if info[n-1] not in frend_dict[info[n]]:
                frend_dict[info[n]].append(info[n-1])

        if len(frend_dict[info[n]]) == N-1:
            flg = 1
            break
    
    if flg == 1:
        print(N)
        break

ans = 0
if flg == 0:
    for i in range(len(frend_dict)-1):
        for j in range(i+1, len(frend_dict)):
            tmp = set(frend_dict[index_list[i]] + frend_dict[index_list[j]])
            if ans < len(tmp):
                ans = len(tmp)

    print(ans)

Submission status: Wrong Answer

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

g = [0] * N
n = 1
for i in range(M):
    if g[I[i][0]-1] == 0:
        if g[I[i][1]-1] == 0:
            g[I[i][0]-1] = n
            g[I[i][1]-1] = n
            n += 1
        else:
            g[I[i][0]-1] = g[I[i][1]-1]
    elif g[I[i][1]-1] == 0:
        g[I[i][1]-1] = g[I[i][0]-1]
ans = 0
for i in range(1, n):
    ans = max(ans, g.count(i))
print(ans)

Submission status: Wrong Answer

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

for i in range(M):
  a, b = input().split()
  if a > b: a, b = b, a
  r.append((a, b))

if M == 0:
  print(0)
  exit()

R = list(set(r))
M = len(R)
c = []

for i in range(M):
  for j in range(len(c)):
    if R[i][0] and R[i][1] in c[j]: break
    if R[i][0] in c[j] and not R[i][1] in c[j]:
      c[j].append(R[i][1])
      break
    if not R[i][0] in c[j] and R[i][1] in c[j]:
      c[j].append(R[i][0])
      break
  else: c.append([R[i][0], R[i][1]])

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

print(ans)

Submission status: Wrong Answer

Solution code:
import collections
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())
ans = 0
f = [-1]*N
for i in range(M):
  if f[A[i]-1] == -1 and f[B[i]-1] == -1:
    f[A[i]-1] = i
    f[B[i]-1] = i
  elif f[A[i]-1] != -1 and f[B[i]-1] == -1:
    f[B[i]-1] = f[A[i]-1]
  else:
    f[A[i]-1] = f[B[i]-1]
ff = [i for i in f if i != -1]
if ff != []:
  print(max(collections.Counter(ff).values()))
else:
  print(1)

Submission status: Wrong Answer

Solution code:
def solve():
    n, m = map(int, input().split())
    group = []
    done = [-1]*n
    num = 0
    for i in range(m):
        a, b = map(lambda x:int(x)-1, input().split())
        if done[a] != -1:
            if done[b] == -1:
                group[done[a]].append(b)
                done[b] = done[a]
        else:
            if done[b] == -1:
                group.append([a,b])
                done[a], done[b] = num, num
                num += 1
            else:
                group[done[b]].append(a)
                done[a] = done[b]
    people = [0]+[len(i) for i in group]
    ans = max(people)
    amari = done.count(-1)
    if amari >= sum(people):
        print(amari)
    else:
        print(max(people))
    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
freind = set()
freindlist = []
log = dict()
mea = 0
sig = 0
inputlist = []
for i in range(M):
    A,B = map(int,input().split())
    if A not in freind and B not in freind:
        freind.add(A)
        freind.add(B)
        freindlist.append({A,B})
        log[str(A)] = mea
        log[str(B)] = mea
        mea += 1
    if A in freind:
        if B in freind and log[str(A)] != log[str(B)]:
            sig1 = log[str(A)]
            sig2 = log[str(B)]
            freindlist[sig1] = freindlist[sig1] | freindlist[sig2]
            freindlist[sig2] = freindlist[sig1] | freindlist[sig2]
            log[str(A)] = min(log[str(A)], log[str(B)])
            log[str(B)]= min(log[str(A)], log[str(B)])
        else:
            freind.add(B)
            sig = log[str(A)] 
            freindlist[sig].add(B)
            log[str(B)] = sig
    if B in freind:
        if A in freind and log[str(A)] != log[str(B)]:
            sig1 = log[str(A)]
            sig2 = log[str(B)]
            freindlist[sig1] = freindlist[sig1] | freindlist[sig2]
            freindlist[sig2] = freindlist[sig1] | freindlist[sig2]
            log[str(A)] = min(log[str(A)], log[str(B)])
            log[str(B)]= min(log[str(A)], log[str(B)])
        else:
            freind.add(A)
            sig = log[str(B)] 
            freindlist[sig].add(A)
            log[str(A)] = sig
Answer = 0
for i in range(len(freindlist)):
    j = len(freindlist[i])
    Answer = max(Answer, j)
print(Answer)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
if M == 0:
  print(0)
else:
  friends = [list(map(int, input().split())) for _ in range(M)]
  pairs = [[] for _ in range(N+1)]
  friendships = [1 for __ in range(N+1)]
  for x, y in friends:
    if x < y:
      pairs[x].append(y)
    else:
      pairs[y].append(x)
  for i, pair in enumerate(pairs):
    if len(pair) > 0:
      nums = set(pair)
      sets = [friendships[i]]
      for num in nums:
        if friendships[i] == friendships[num]:
          count = 1
          while(True):
            if count in sets:
              count +=1
            else:
              break
          friendships[num] = count
  print(max(friendships))

Submission status: Wrong Answer

Solution code:
input_n = input()
n = str(input_n)

#listS = list(n)
listN = n.split(' ')

menber = int(listN[0])
relationship = int(listN[1])

set_list = [set()]

for i in range(relationship):
    add_flag = 0
    input_n = input()
    n = str(input_n)
    listN = n.split(' ')
    tmp = []
    tmp.append(int(listN[0]))
    tmp.append(int(listN[1]))
    for j in range(2):
        for k in range(len(set_list)):
            if tmp[j] in set_list[k]:
                set_list[k].add(tmp[0])
                set_list[k].add(tmp[1])
                add_flag = 1
                break
    if add_flag == 0:
        set_list.append(set())
        set_list[len(set_list) - 1].add(tmp[0])
        set_list[len(set_list) - 1].add(tmp[1])

max = 0
for i in range(len(set_list)):
    tmp_max = len(set_list[i])
    if tmp_max > max:
        max = tmp_max

print(str(max))

Submission status: Wrong Answer

Solution code:
import collections
n,m = map(int,input().split())
if m==0:
  print(1)
  exit()
g = [-1]*n
gc = []
for i in range(m):
  a,b = map(int,input().split())
  if g[a-1]==-1 and g[b-1]==-1:
    g[a-1] = i
    g[b-1] = i
  elif g[a-1]!=-1 and g[b-1]==-1:
    g[b-1] = g[a-1]
  elif g[a-1]==-1 and g[b-1]!=-1:
    g[a-1] = g[b-1]
  elif g[a-1]!=g[b-1]:
    gc.append((g[a-1],g[b-1]))
c = collections.Counter(g)
if gc!=[]:
  for i in range(len(gc)):
    c[gc[i][0]] += c[gc[i][1]]
    c[gc[i][1]] =0
cnt = c.most_common()
if cnt[0][0]==-1:
  print (cnt[1][1])
else:
  print (cnt[0][1])

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
con = []
ans = 0
for i in range(m):
    check = True
    b,c = map(int,input().split())
    if i == 0:
        con.append([b,c])
    else:
        for i in range(len(con)):
            if b in con[i]:
                check = False
                if c in con[i]:
                    break
                else:
                    con[i].append(c)
                    break
            elif c in con[i]:
                check = False
                con[i].append(b)
                break
        if check:
            con.append([b,c])
            ans = max(ans,len(con[i]))
print(ans)

Submission status: Wrong Answer

Solution code:
n,m=[int(x) for x in input().split()]
s=[[] for i in range(n)]
for i in range(m):
  a,b=[int(x) for x in input().split()]
  if b-1 not in s[a-1]:
    s[a-1].append(b-1)
  if a-1 not in s[b-1]:
    s[b-1].append(a-1)
ans=0
for i in range(n):
  l=len(s[i])
  ans=max(ans,l)
print(ans+1)

Submission status: Wrong Answer

Solution code:
def main():
    N,M = map(int,input().split())
    A = [[0,0] for i in range(M)]
    for i in range(M):
        A[i] = list(map(int,input().split()))
        list.sort(A[i])
    
    list.sort(A)
    
    List = [-1 for i in range(N)]
    List2 = [[] for i in range(N)]
    host = 0
    guest = 0
    for i in range(M):
        a = A[i][0]-1
        b = A[i][1]-1
        if List[a] == -1 and List[b] == -1:
            host = a
            guest = b
            List[host] = host
            List2[host].append(host)
            List[guest] = host
            List2[host].append(guest)
        elif List[a] != -1 and List[b] != -1 and List[a] != List[b]:
            for l in List2[List[b]]:
                List[l] = a
            List2[List[a]].extend(List2[List[b]])
            List2[List[b]] = []
        elif List[a] != -1:
            host = List[a]
            guest = b
            List[guest] = host
            List2[host].append(guest)
        elif List[b] != -1:
            host = List[b]
            guest = a
            List[guest] = host
            List2[host].append(guest)
        
    ans = [0 for i in range(N)]
    for i in range(N):
        L = List[i]
        if L != -1:
            ans[L] += 1
            
    print(max(ans))
        
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
 
n, m = map(int,input().split())

sub_total = [0] * n
joken = []
for i in range(m):
    a, b = map(int,input().split())
    mi = min(a,b)
    ma = max(a,b)
    j = str('{},{}'.format(mi,ma))
    if j not in joken:
        joken.append(j)
        sub_total[mi - 1] += 1
        sub_total[ma - 1] += 1

print(max(sub_total) + 1)

Submission status: Wrong Answer

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

for i in range(m):
	a,b=map(int,input().split())
	L[a].add(b)
	L[b].add(a)
	S.add(a)
	S.add(b)

for v in S:
	for i in range(n+1):
		if v in L[i]:
			L[i]=L[v]|L[i]
ans=0
for v in S:
	ans=max(ans,len(L[v]))
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N, M = map(int, input().split())

dic = defaultdict(list)
for _ in range(M):
    a, b = map(int, input().split())
    if b not in dic[a]:
        dic[a].append(b)

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

# 一番友達の多いグループをバラバラにすればいい
friends_count = []
for i in range(1, N + 1):
    friends_count.append(len(dic[i]) + 1)
print(max(friends_count))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
F = []
for _ in range(M):
    A, B = map(int, input().split())
    if len(F) == 0:
        F.append([A, B])
    isExist = False
    for f in F:
        if A in f or B in f:
            f.append(A)
            f.append(B)
            isExist = True
    if not isExist:
        F.append([A, B])
ans = 0
for f in F:
    ans = max(ans, len(list(set(f))))
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, M = map(int, input().split())
groups = [0] * (N + 1)
group_num_list = [[0]]
group_num = 1
for _ in range(M):
    A, B = map(int, input().split())
    if groups[A] != 0 and groups[B] != 0:
        if groups[A] != groups[B]:
            lower = min(groups[A], groups[B])
            higher = max(groups[A], groups[B])
            for i in group_num_list[higher]:
                groups[i] = lower
            tmp = group_num_list[higher]
            group_num_list[lower].extend(tmp)
            # del group_num_list[higher]
    elif groups[A] != 0:
        groups[B] = groups[A]
        group_num_list[groups[A]].append(int(B))
    elif groups[B] != 0:
        groups[A] = groups[B]
        group_num_list[groups[B]].append(int(A))
    else:
        groups[A] = group_num
        groups[B] = group_num
        group_num_list.append([int(A), int(B)])
        group_num += 1
counter = Counter(groups[1:])
# print(collection)
# print(group_num_list)
print(counter.most_common()[0][1])

Submission status: Wrong Answer

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

G = []
lookup_tab = {}

AB = [list(sorted((map(int, input().split())))) for _ in range(M)]
AB.sort()
for a, b in AB:
    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)
    else:
        continue

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

Submission status: Wrong Answer

Solution code:
import collections
N, M = map(int, input().split())
D = dict([(i,i) for i in range(1,N+1)])

L = []

for i in range(M):
    A, B = map(int, input().split())
    if A > B:
        A,B = B,A
    L += [(A,B)]
    
L = list(set(L))

L.sort()

for i in L:
    A,B = i
    m = min([D[A], D[B], A, B])
    D[A] = m
    D[B] = m
    
D = collections.Counter(D.values())
print(max(D.values()))

Submission status: Wrong Answer

Solution code:
def main():
	N, M = [int(n) for n in input().split(" ")]
	friends = []
	tmp = []
	for i in range(M):
		a, b = [int(x) for x in input().split(" ")]
		tmp.append(str(min([a, b])) + "-" + str(max([a, b])))
	tmp = list(set(tmp))
	for i in range(len(tmp)):
		a, b = [int(x) for x in tmp[i].split("-")]
		friends.append({
			"min" : a,
			"max" : b
		})
	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
		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:
			q = min([group[m - 1], group[M - 1]])
			while group[q] != -1:
				q = group[q]
			else:
				group[m - 1] = q
				group[M - 1] = q

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# def get_group_num(sum, nums):
#     if len(nums) == 1:
#         return sum + nums[0]
#     pass

def main():
    n, m = map(int, input().split())
    friends_list = []
    num_check = {}
    for i in range(m):
        check=True
        a,b = map(int, input().split())
        for i, friends_set in enumerate(friends_list):
            if a in friends_set:
                friends_list[i].add(b)
                check = False
                break
            if b in friends_set:
                friends_list[i].add(a)
                check = False
                break
        if check:
            friends_list.append({a, b})
    group_num = len(friends_list)
    max = 0
    for friends_set in friends_list:
        length = len(friends_set)
        if length > max:
            max = length
    print(max)
    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def solve():
    n, m = map(int, input().split())
    group = []
    done = [-1]*n
    num = 0
    for i in range(m):
        a, b = map(lambda x:int(x)-1, input().split())
        if done[a] != -1:
            if done[b] == -1:
                group[done[a]].append(b)
                done[b] = done[a]
        else:
            if done[b] == -1:
                group.append([a,b])
                done[a], done[b] = num, num
                num += 1
            else:
                group[done[b]].append(a)
                done[a] = done[b]
    people = [0]+[len(i) for i in group]
    ans = max(people)
    amari = done.count(-1)
    if amari / 2 >= n/2:
        print(amari)
    else:
        print(max(people))
    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
class UnionFind():
    #https://note.nkmk.me/python-union-find/ 
    #note.nkmk.me  2019-08-18 
    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())


# 初期入力
import sys
input = sys.stdin.readline  #文字列では使わない
N,M = map(int, input().split())
G =UnionFind(N)
ans =0
for i in range(M):
    a,b = map(int, input().split())
    #a,b =min(a,b),max(a,b)
    G.union(a-1,b-1)
    ans =max(ans,G.size(a-1))
if M ==0:
    print(N)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    n, m  = map(int, input().split())
    friend = set()

    for i in range(m):
        temp = list(map(int, input().split()))
        temp.sort()
        friend.add(tuple(temp))

    cnt = [0]*(n+1)

    for e in friend:
        cnt[e[0]] += 1
        cnt[e[1]] += 1

    print(max(cnt) + 1)        


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())
A = [0] * M
B = [0] * M
box = [[0 for i in range(N)] for j in range(N)]
for i in range(M):
    A[i], B[i] = map(int,input().split())
    if A[i] > B[i]:
        A[i], B[i] = B[i], A[i]
    box[A[i]-1][0] = 1
    box[A[i]-1][B[i]-1] = 1
res = [0] * N
for j in range(N):
    res[j] = sum(box[j])
print(max(res))