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:
import copy

n, m = map(int, input().split())
n_list = list(range(1, n + 1))
netdic = dict.fromkeys(n_list, set({}))
for i in range(m):
    a, b = map(int, input().split())
    trydic_a = copy.copy(netdic[a])
    trydic_a.add(b)
    netdic[a] = trydic_a
    trydic_b = copy.copy(netdic[b])
    trydic_b.add(a)
    netdic[b] = trydic_b

ansdic = dict.fromkeys(list(range(1, n + 1)), 0)
for i in range(n):
    ansdic[i + 1] = len(netdic[i + 1])


print(max(ansdic.values()) + 1)

Submission status: Wrong Answer

Solution code:
N , M = map(int,input().split())
g = [[] for i in range(N)]
for i in range(M):
  a , b = map(int,input().split())
  a -= 1
  b -= 1
  g[a].append(b)
  g[b].append(a)
m = 0
for i in range(N):
  l = len(set(g[i]))
  if m < l:
    m = l
print(m+1)

Submission status: Wrong Answer

Solution code:
from collections import deque
N, M = map(int, input().split())
G = [set() for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    G[a - 1].add(b - 1)
    G[b - 1].add(a - 1)
c = 0
for g in G:
    c = max(c, len(g) + 1)
print(c)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
t = [-1]*n
count = 0
for _ in range(m):
    a, b = map(lambda x: int(x) - 1, input().split())
    if t[a] == -1 and t[b] == -1:
        count += 1
        t[a] = count
        t[b] = count
        continue
    if t[a] != -1 and t[b] != -1 and t[a] != t[b]:
        t[a] = min(t[a], t[b])
        t[b] = min(t[a], t[b])
        for i in range(n):
            if t[i] == max(t[a], t[b]):
                t[i] = min(t[a], t[b])
        continue
    if t[a] != -1 and t[b] != -1 and t[a] == t[b]:
        continue
    if t[a] == -1:
        t[a] = t[b]
        continue
    if t[b] == -1:
        t[b] = t[a]
print(
    max([t.count(i) for i in set(t)])
)

Submission status: Wrong Answer

Solution code:
import sys

ininfo = input().split(" ")
num = int(ininfo[0])
m = int(ininfo[1])

if m==0:
  print(1)
  sys.exit()
  
if m==1:
  print(2)
  sys.exit()

list=[]
for i in range(m):
  list.append(input().split(" "))
  list[i].sort()

list.sort()
anslist=[]
s={list[0][0],list[0][1]}
anslist.append(s)
for i in list:
  match=0
  for j in anslist:
    if i[0] in j and i[1] in j:
      match=1
      break
    elif i[0] in j or i[1] in j:
      j.add(i[0])
      j.add(i[1])
      match=1
      break
  if match==0:
    s={i[0],i[1]}
    anslist.append(s)
      
ans=0
for i in anslist:
  if len(i)>ans:
    ans=len(i)

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque



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

D = [-1]*(N+1)
#S=[[0] * 3 for _ in range(2)] 2行3列の場合
S = [[]for i in range(N+1)]
deq=deque()

    


for i in range(M):
    A,B=map(int,input().split())
    
    S[A].append(B)
    S[B].append(A)
ans=0
for i in range(1,N+1):
    deq.append(i)
    count=0
    D = [-1]*(N+1)
    while len(deq)!=0:
        v= deq.popleft()
        
        for nv in S[v]:
            if D[nv] !=-1:
                continue
            D[nv] =D[v]+1
            deq.append(nv)
            count+=1
    if count>ans:
        ans=count



print(ans)

Submission status: Wrong Answer

Solution code:
nm = input('').split()
n = int(nm[0])
m = int(nm[1])

mx=0
friends=[]

for i in range(m):
    ab=input('').split()
    a = int(ab[0])
    b = int(ab[1])
    ingroup=False
    for f in friends:
        if a in f or b in f:
            f.add(a)
            f.add(b)
            ingroup=True
    if ingroup == False:
        friends.append(set([a,b]))
    
for f in friends:
    len_f=len(f)
    if len_f > mx:
        mx=len_f
#print(friends)
print(mx)

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

# if m == 0:
#     print(1)
#     exit()

# print(AB)

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)
# for v in range(1,n+1):
#     print('find(v) {}'.format(uf.find(v)))

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

# print('uf {}'.format(uf.par))
# print(*m.items(),sep='\n')

ans = 0
for k,v in m.items():
    # if k == 0: continue
    # print(k,v)
    if ans < v:
        ans = v
if ans == 0: ans = n
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n,m = map(int,input().split())
group = {}
for i in range(1,n+1):
    group[i] = i
for _ in range(m):
    a,b = map(int,input().split())
    k = min(group[a],group[b])
    group[a] = k
    group[b] = k
p = Counter(group.values())
print(max(p.values()) + 1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

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

group_dict = defaultdict(lambda : None)
group_counter = 0
group_member = [0]*(n+1)

update_dict = defaultdict(lambda : None)

group = []
for i in range(m):
    a,b = friend_list[i]

    a_group = group_dict[a]
    b_group = group_dict[b]
    target_group = -1
    if a_group != None:
        target_group = a_group
    if b_group != None:
        target_group = b_group

    if target_group == -1:
        target_group = group_counter
        group_counter+=1

    if a_group != None and b_group != None:
        target_group = max(a_group,b_group)
        update_dict[min(a_group,b_group)] = max(a_group,b_group)
        for g in group_dict:
            if group_dict[g] == min(a_group,b_group):
                group_dict[g] = max(a_group,b_group)
    
    group_dict[a] = target_group
    group_dict[b] = target_group


for k in group_dict:

    new_k = update_dict[group_dict[k]]
    prev = None
    while prev != new_k and new_k != None:
        prev = new_k
        new_k = update_dict[new_k]
    if prev != None:
        change = prev
    else:
        change = group_dict[k]
    group_member[change] += 1

print(max(group_member))

Submission status: Wrong Answer

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

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

    if N == 0:
        print(0)
        return

    if M == 0 or N == 1:
        print(1)
        return
    ans = max(map(len, groups.values()))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import collections
n, m = map(int, input().split())
gp = [[] for i in range(n+1)]
seen = [0 for i in range(n+1)]
ans = []

for i in range(m):
  a, b = map(int, input().split())
  if b not in gp[a]:
    gp[a].append(b)
  if a not in gp[b]:
    gp[b].append(a)

for i in range(1, n+1):
  if seen[i] == 1:
    continue
  else:
    que = [i]
    cnt = 1
    while que:
      now = que.pop(0)
      for g in gp[now]:
        if seen[g] == 1:
          continue
        else:
          que.append(g)
          seen[g] = 1
          cnt += 1
    if cnt != 1:
      cnt -= 1
    ans.append(cnt)
          
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)
n,m = map(int,input().split())
graph = [[] for _ in range(n+1)]
for _ in range(m):
    a,b = map(int,input().split())
    graph[a].append(b)
    graph[b].append(a)
ans = 0
def dfs(node):
    todo = set()
    for nextnode in graph[node]:
        if tf[nextnode] == 0 :
            tf[nextnode] = 1
            seen.add(nextnode)
            todo.add(nextnode)
    for nextnode in todo:
        dfs(nextnode)
done = set()
while len(done) < n:
    tf = [0 for _ in range(n+1)]
    seen = set()
    for i in range(1,n+1):
        if i not in done:
            break
    done.add(i)
    dfs(i)
    ans = max(ans,sum(tf))
    done |= seen
print(ans)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
A=[1]*N
P=[]
for i in range(M):
    a,b=map(int,input().split())
    c=max(a,b)
    a=min(a,b)
    b=c
    P.append((a,b))
P=list(set(P))
for i in range(len(P)):
    A[P[i][0]-1]+=1
    A[P[i][1]-1]+=1
print(max(A))

Submission status: Wrong Answer

Solution code:
n, m = map(int,input().split())
friend = [[]]
for i in range(m):
  a, b = map(int,input().split())
  for j in range(len(friend)):
    if a in friend[j] and b in friend[j]:
      break
    elif a in friend[j] and not b in friend[j]:
      friend[j].append(b)
      break
    elif b in friend[j]:
      friend[j].append(a)
      break
    elif j == len(friend)-1:
      friend.append([a,b])
ans = 0
for i in range(len(friend)):
  ans = max(ans,len(friend[i]))
print(ans)

Submission status: Wrong Answer

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

from collections import defaultdict

# union find
class UnionFind:
    def __init__(self, n):
        self.table = [-1] * n
 
    def _root(self, x):
        stack = []
        tbl = self.table
        while tbl[x] >= 0:
            stack.append(x)
            x = tbl[x]
        for y in stack:
            tbl[y] = x
        return x
 
    def find(self, x, y):
        return self._root(x) == self._root(y)
 
    def union(self, x, y):
        r1 = self._root(x)
        r2 = self._root(y)
        if r1 == r2:
            return
        d1 = self.table[r1]
        d2 = self.table[r2]
        if d1 <= d2:
            self.table[r2] = r1
            self.table[r1] += d2
        else:
            self.table[r1] = r2
            self.table[r2] += d1

uf = UnionFind(N)

for _ in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if a > b:
        c = b
        b = a
        a = c

    uf.union(a, b)

# member = defaultdict(int)
r = min(uf.table)
if r != -1:
    mm = r
    ic = 0
    for n in uf.table:
        if n == -1:
            ic += 1

    if mm < ic:
        mm += 1
else:
    mm = 1

print(mm)

Submission status: Wrong Answer

Solution code:
def friends(arr):
    table = [{}]
    for a in arr:
        seted = False
        for t in table:
            if a[0] in t or a[1] in t:
                seted = True
                t.add(a[0])
                t.add(a[1])
        if not seted:
            table.append({a[0], a[1]})
    return max(table, key=len)
def start():
    n, m = [int(x) for x in input().split(" ")]
    arr = []
    while m:
        arr.append([int(x) for x in input().split(" ")])
        m -= 1
    print(len(friends(arr)))
start()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

def main():

    input = sys.stdin.readline

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

    A = [0] * M
    B = [0] * M

    search_set = set()
    friend_list = [ set() for _ in range(N)]
    friend_list_len = 0

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

        if (A[i] not in search_set) & (B[i] not in search_set):
            friend_list[friend_list_len].add(A[i])
            friend_list[friend_list_len].add(B[i])
            friend_list_len += 1
        else:
            for j in range(friend_list_len):
                flag = False
                if A[i] in friend_list[j]:
                    friend_list[j].add(B[i])
                    flag = True
                if B[i] in friend_list[j]:
                    friend_list[j].add(A[i])
                    flag = True
                if flag:
                    break

        search_set.add(A[i])
        search_set.add(B[i])

    ans = 1
    for i in range(friend_list_len):
        ans = max(ans,len(friend_list[i]))

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
friends = {}
if N <= 2:
    print(N)
else:
    for _ in range(M):
        f, s = map(int, input().split())
        if f in friends.keys():
            friends[f].append(s)
        else:
            friends[f] = [s]
        if s in friends.keys():
            friends[s].append(f)
        else:
            friends[s] = [f]
    biggest = 1
    for i, v in enumerate(friends):
        friends[v] = list(set(friends[v]))
        biggest = max(len(friends[v]), biggest)
    print(biggest + 1)

Submission status: Wrong Answer

Solution code:
from collections import deque
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 B-1 not in myd[A-1]: 
        myd[A-1] += [B-1]
    if A-1 not in myd[B-1]: 
        myd[B-1] += [A-1]
#print(myd)

myans = 0
for n in range(N):
    check = [0 for _ in range(N)]
    d = deque()
    mymax = 0
    for a in myd[n]:
        d.append(a)
    #print(d)
    while d:
        tgt = d.popleft()
        if check[tgt] == 0:
            mymax += 1
            #print(mymax)
            for b in myd[tgt]:
                d.append(b)
            check[tgt] = 1
            
    if myans < mymax:
        myans = mymax
print(myans)

Submission status: Wrong Answer

Solution code:
import copy

n,m = map(int,input().split())
f = []

for i in range(m):
    a,b=map(int,input().split())
    flg = True
    for j in f:
        if a in j:
            j.add(b)
            flg = False
        elif b in j:
            j.add(a)
            flg = False

    if flg:
        f.append({a,b})


nf = copy.deepcopy(f)

for i in range(len(f)):
    for j in f[i]:
        for k in range(i+1,len(f)):
            if j in f[k]:
                nf.append(f[i]|f[k])

ans = 0
for i in nf:
    ans = max(len(i),ans)

print(ans)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
x=[[i,i] for i in range(n+1)]
g=[[i,0] for i in range(n+1)]
x[0][1]=0
ab=[[] for i in range(m)]
for i in range(m):
    a,b=map(int,input().split())
    ab[i].append(min(a,b))
    ab[i].append(max(a,b))
ab=sorted(ab)
for i in range(m):
    a=ab[i][0]
    b=ab[i][1]
    if x[a][1]!=x[b][1]:
        x[a][1]=min(x[a][1],x[b][1])
        x[b][1]=min(x[a][1],x[b][1])
ans=0
for i in range(n):
    g[x[i+1][1]][1]+=1
for i in range(n):
    ans=max(ans,g[i+1][1])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys, io, os, re
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from pprint import pprint
from math import sin, cos, pi, radians, sqrt, floor, ceil
from copy import copy, deepcopy
from collections import deque, defaultdict
from fractions import gcd
from functools import reduce
from itertools import groupby, combinations
from heapq import heapify, heappush, heappop

# sys.setrecursionlimit(5000)

n1 = lambda: int(sys.stdin.readline().strip())
nn = lambda: list(map(int, sys.stdin.readline().strip().split()))
f1 = lambda: float(sys.stdin.readline().strip())
fn = lambda: list(map(float, sys.stdin.readline().strip().split()))
s1 = lambda: sys.stdin.readline().strip()
sn = lambda: list(sys.stdin.readline().strip().split())
nl = lambda n: [n1() for _ in range(n)]
fl = lambda n: [f1() for _ in range(n)]
sl = lambda n: [s1() for _ in range(n)]
nm = lambda n: [nn() for _ in range(n)]
fm = lambda n: [fn() for _ in range(n)]
sm = lambda n: [sn() for _ in range(n)]

def array1(n, d=0): return [d] * n
def array2(n, m, d=0): return [[d] * m for x in range(n)]
def array3(n, m, l, d=0): return [[[d] * l for y in xrange(m)] for x in xrange(n)]
def linc(A, d=1): return list(map(lambda x: x + d, A))
def ldec(A, d=1): return list(map(lambda x: x - d, A))

N, M = nn()
T = nm(M)

for i in range(M):
    a, b = T[i]
    if a > b: T[i] = [b, a]

T = sorted(T)
#print(T)

d = defaultdict(list)
team = defaultdict(list)
memo = array1(N+1, -1)

c = 0
for i in range(M):
    a, b = T[i]
    if memo[a] == -1 and memo[b] == -1:
        team[c].append(a)
        team[c].append(b)
        memo[a] = c
        memo[b] = c
        c += 1
    elif memo[a] == -1:
        team[memo[b]].append(a)
        memo[a] = memo[b]
    elif memo[b] == -1:
        team[memo[a]].append(b)
        memo[b] = memo[a]

#print(memo)
#pprint(team)

ans = 0
for i in range(len(team)):
    t = len(team[i])
    if t > ans:
        ans = t

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
import collections
class DJS:
    def __init__(self, n):
        self.p = defaultdict(int)
        self.rank = defaultdict(int)
        for i in range(n):
            self.makeSet(i)

    def makeSet(self,x):
        self.p[x] = x
        self.rank[x] = 0

    def findSet(self,x):
        if x != self.p[x]:
            self.p[x] = self.findSet(self.p[x])
        return self.p[x]
    
    def unite(self, x, y):
        self.link(self.findSet(x),self.findSet(y))
    
    def link(self, x, y):
        if self.rank[x] > self.rank[y]:
            self.p[y] = x
        else:
            self.p[x] = y
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1
        

    def same(self, x, y):
        return self.findSet(x) == self.findSet(y)
n,m = map(int,input().split())
djs = DJS(n)
for i in range(m):
    a,b = map(int,input().split())
    djs.unite(a-1,b-1)
tmp= collections.Counter(djs.p.values())
ret = max(tmp.values())
print(ret)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
m = [[i] for i in range(N)]
l = [0 for i in range(N)]
for i in range(M):
    A,B = map(int,input().split())
    for i in m[A-1]:
        if not B-1 in m[i]:
            m[i].append(B-1)
            l[i] += 1
    for i in m[B-1]:
        if not A-1 in m[i]:
            m[i].append(A-1)
            l[i] += 1
print(max(l)+1)

Submission status: Wrong Answer

Solution code:
a,b = list(map(int, input().split()))
friend_li = [[] for _ in range(a)]

result = 0

for i in range(b):
  c,d = list(map(int, input().split()))
  friend_li[min(c-1,d-1)].append(max(c-1,d-1))

for element in friend_li:
  len_ = len(list(set(element)))
  if len_ > result:
    result = len_
print(result+1)

Submission status: Wrong Answer

Solution code:
import copy

n, m = map(int, input().split())
n_list = list(range(1, n + 1))
netdic = dict.fromkeys(n_list, set({}))
for i in range(m):
    a, b = map(int, input().split())
    trydic_a = copy.copy(netdic[a])
    trydic_a.add(b)
    netdic[a] = trydic_a
    trydic_b = copy.copy(netdic[b])
    trydic_b.add(a)
    netdic[b] = trydic_b

ansdic = dict.fromkeys(list(range(1, n + 1)), 0)
for i in range(n):
    ansdic[i + 1] = len(netdic[i + 1])


print(max(ansdic.values()))

Submission status: Wrong Answer

Solution code:
from collections import Counter

N,M = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(M)]
check = [0 for x in range(N)]

# 変数
num1 = 0 # ABのi番目の0要素
num2 = 0 # ABのi番目の1要素
j = 1 # グループカウンタ
for i in range(M) : # M個の情報を格納
    num1 = AB[i][0]
    num2 = AB[i][1]
    #print("!",num1,num2)
    if (check[num1-1] == 0 and check[num2-1] ==0) : # 何も入っていなかったら
        #print("#",i,"zero")
        check[num1-1] = j # グループj
        check[num2-1] = j # グループj
        #print("#",check)
        j += 1
    elif((check[num1-1] != 0 and check[num2-1] ==0) or (check[num1-1] == 0 and check[num2-1] !=0)) : # もし片方に情報があったら
        if check[num1-1] == 0 :
            check[num1-1] = check[num2-1]
        else :
            check[num2-1] = check[num1-1] # 先に入っていたグループに合わせる
    else : # 両方に情報が入っていたら
        if check[num2-1] >= check[num1-1] :
            check[num2-1] = check[num1-1]
        else :
            check[num1-1] = check[num2-1] # 小さいグループに合わせる 

#print(check)
c = Counter(check)
print(c.most_common()[0][1])

Submission status: Wrong Answer

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

frends = []

for i in range(m):
  a,b = map(int, input().split())
  ainclude = set()
  binclude = set()
  flag = False
  for j in frends:
    if a in j:
      ainclude = j
      frends.remove(j)
      flag = True
  for j in frends:
    if b in j:
      binclude = j
      frends.remove(j)
      flag = True
  if flag == False:
    frends.append({a,b})
  else:
    c = {a,b}
    frends.append(ainclude|binclude|c)

cnt = 0

for i in frends:
  num = len(i)
  if cnt < num:
    cnt = num


print(cnt)

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]
print(max(collections.Counter(f).values()))

Submission status: Wrong Answer

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

friends = [0] * n
query = []

tmp = 1
for i in range(m):
  a,b = map(int, input().split())
  query.append((a,b))
  
for a,b in query:
  if friends[a - 1] == 0 and  friends[b - 1] == 0:
    friends[a - 1] = tmp
    friends[b - 1] = tmp
    tmp += 1
  elif friends[a - 1] == 0:
    friends[a - 1] = friends[b - 1]
  elif friends[b - 1] == 0:
    friends[b - 1] = friends[a - 1]
  else:
    index_num = [n for n, v in enumerate(friends) if v == friends[a - 1]]
    duplicated = friends[b - 1]
    for j in index_num:
      friends[j] = duplicated
      
counted = collections.Counter(friends)
ans = counted.most_common()[0][1]
if m == 0:
  print(1)
elif ans != 0:
  print(ans)
else:
  print(counted.most_common()[0][2])

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python

def main():
    n, m = map(int, input().split())
    ab_arr = []
    for _ in range(m):
        ab = list(map(int, input().split()))
        ab.sort()
        ab_arr.append(ab)

    ab_arr.sort(key=lambda ab: (ab[0], ab[1]))
    # print(ab_arr)
    f_set = [{i} for i in range(n+1)]

    ans = 0
    for ab in ab_arr:
        a, b = ab
        f_set[a] = f_set[a] | f_set[b]
        f_set[b] = f_set[a]
        ans = max(ans,len(f_set[a]))

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#D


INF=float('inf')
n,m=map(int,input().split())
R=[-1 for i in range(n)]
def f(u,v):
  ms=min(u,v)
  mb=max(u,v)
  if R[ms]==-1 and R[mb]==-1:
    R[ms]=ms
    R[mb]=ms
  elif R[ms]!=-1 and R[mb]!=-1:
    stop=True
    while stop:
      x=R[ms]
      if R[x]==x:
        stop=False
    R[ms]=x
    R[mb]=x
  else:
    if R[ms]==-1:
      R[ms]=mb
    else:
      R[mb]=ms

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

NO=R.count(-1)
rset=set()
root=[0 for i in range(n)]
for i in range(n):
  if R[i]==-1:
    root[R[i]]=-INF
  else:
    root[R[i]]+=1

print(max(root))

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

# if m == 0:
#     print(1)
#     exit()

# print(AB)

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)
# for v in range(1,n+1):
#     print('find(v) {}'.format(uf.find(v)))

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

# print('uf {}'.format(uf.par))
# print(*m.items(),sep='\n')

ans = 0
for k,v in m.items():
    # if k == 0: continue
    # print(k,v)
    if ans < v:
        ans = v
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)]
dic={}
for i in range(M):
    a,b=map(int,(input().split()))
    if a not in dic:
        c=set()
        c.add(a)
        c.add(b)
        dic[a]=c
    else:dic[a].add(b)
    if b not in dic:
        c = set()
        c.add(a)
        c.add(b)
        dic[b] = c
    else:dic[b].add(a)

    # rinsetu[a].add(b)
    # rinsetu[b].add(a)
ans=0
for k,v in dic.items():
    ans=max(ans,len(v))
print(ans)
# 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:
n, m = map(int,input().split())
A = []
chk = [0]*n

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

A = list(set(A))

for i in range(len(A)):
    chk[A[i][0]-1] += 1

print(max(chk) + 1)

Submission status: Wrong Answer

Solution code:
import sys, io, os, re
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from pprint import pprint
from math import sin, cos, pi, radians, sqrt, floor, ceil
from copy import copy, deepcopy
from collections import deque, defaultdict
from fractions import gcd
from functools import reduce
from itertools import groupby, combinations
from heapq import heapify, heappush, heappop

# sys.setrecursionlimit(5000)

n1 = lambda: int(sys.stdin.readline().strip())
nn = lambda: list(map(int, sys.stdin.readline().strip().split()))
f1 = lambda: float(sys.stdin.readline().strip())
fn = lambda: list(map(float, sys.stdin.readline().strip().split()))
s1 = lambda: sys.stdin.readline().strip()
sn = lambda: list(sys.stdin.readline().strip().split())
nl = lambda n: [n1() for _ in range(n)]
fl = lambda n: [f1() for _ in range(n)]
sl = lambda n: [s1() for _ in range(n)]
nm = lambda n: [nn() for _ in range(n)]
fm = lambda n: [fn() for _ in range(n)]
sm = lambda n: [sn() for _ in range(n)]

def array1(n, d=0): return [d] * n
def array2(n, m, d=0): return [[d] * m for x in range(n)]
def array3(n, m, l, d=0): return [[[d] * l for y in xrange(m)] for x in xrange(n)]
def linc(A, d=1): return list(map(lambda x: x + d, A))
def ldec(A, d=1): return list(map(lambda x: x - d, A))

N, M = nn()
T = nm(M)

T = sorted(T)

d = defaultdict(list)
team = defaultdict(list)
memo = array1(N+1, -1)

c = 0
for i in range(M):
    a, b = T[i]
    if memo[a] == -1 and memo[b] == -1:
        team[c].append(a)
        team[c].append(b)
        memo[a] = c
        memo[b] = c
        c += 1
    elif memo[a] == -1:
        team[memo[b]].append(a)
        memo[a] = memo[b]
    elif memo[b] == -1:
        team[memo[a]].append(b)
        memo[b] = memo[a]

#print(memo)
#pprint(team)

ans = 0
for i in range(len(team)):
    t = len(team[i])
    if t > ans:
        ans = t

print(ans)

Submission status: Wrong Answer

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

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

    if M == 0:
        print(1)
        return
    ans = max(map(len, groups.values()))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m=map(int,input().split())
d={}
ans=0
for i in range(m):
  a,b=map(int,input().split())
  if a in d:
    d[a].append(b)
    if b in d:
      d[a].extend(d[b])
  else:
    d[a]=deque([b])
  if b in d:
    d[b].append(a)
    if a in d:
      d[b].extend(d[a])
  else:
    d[b]=deque([a])
for i in d.values():
  ans=max(len(list(set(i))),ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n,m = map(int, input().split())
edges = [[] for i in range(n)]
for i in range(m):
    a,b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges[b].append(a)
#print(edges)

le = len(edges)
cnt = 0
ll=[]
for i in range(le):
  tmp = len(set(edges[i]))
  ll.append(tmp)
  cnt = max(cnt,tmp)
#print(ll)
max_index = ll.index(max(ll))


from collections import deque
for i in range(max_index,max_index+1):
    s = set()
    d = [-1] * n
    d[i] = 0
    q = deque([])
    q.append(i)
    while len(q) > 0:
        v = q.popleft()
        # print(v,d[v])
        for nx in edges[v]:
            if d[nx] >= 0:continue
            d[nx] = d[v] + 1
            q.append(nx)
#    print(d,q)
#    print(d.count(3))
print(cnt+1 - d.count(3))

Submission status: Wrong Answer

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

group_list = []
inverted_dict = {}

for i in range(M):
    a, b = input().split(" ")
    flag = False

    if a in inverted_dict and not b in inverted_dict:
        idx = inverted_dict[a]
        group_list[idx].add(b)
        inverted_dict[b] = idx
        flag = True
    if b in inverted_dict and not a in inverted_dict:
        idx = inverted_dict[b]
        group_list[idx].add(a)
        inverted_dict[a] = idx
        flag = True
    if a in inverted_dict and b in inverted_dict:
        idx_a = inverted_dict[a]
        idx_b = inverted_dict[b]
        group_list[idx_a] = group_list[idx_a].union(group_list[idx_b])
        group_list[idx_b] = group_list[idx_a]
        flag = True
    if not flag:
        idx = len(group_list)
        inverted_dict[a] = idx
        inverted_dict[b] = idx
        group_list.append(set([a, b]))
max_group = 1
for g in group_list:
    max_group = max(max_group, len(g))
print(max_group)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
friends_set=set()
for i in range(M):
    a,b=map(int,input().split())
    if a>b:
        a,b=b,a
    friends_set.add((a,b)) 
    
friends_list=[[] for _ in range(N)]
for f in friends_set:
    friends_list[f[0]].append(f[1])

max_len=0
for i in friends_list:
    max_len=max(max_len,len(i)+1)
print(max_len)