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())
arr = []
ans = 2
for i in range(M):
  if arr == []:
    arr.append(set(input().split( )))
    continue
  else:
    cnt = 0
    l = set(input().split( ))
    for a in arr:
      if l.isdisjoint(a):
        continue
      else:
        if cnt == 0:
          s = a.union(l)
          arr.remove(a)
          cnt += 1
        else:
          s = s.union(a)
          arr.remove(a)
    if cnt == 0:
      s = l
    arr.append(s)
    ans = max(ans, len(s))
#print(arr)
print(ans)

Submission status: Wrong Answer

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

relations = []


for _ in range(M):
    Ai, Bi = map(int, input().split())
    found = False
    for group in relations:
        if Ai in group or Bi in group:
            group.add(Ai)
            group.add(Bi)
            found = True
            break
    
    if not found:
        relations.append({Ai, Bi})

max_ = 0
for group in relations:
    max_ = max(len(group), max_)

print(max_)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
par = [-1]*(n+1)
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x]) #経路圧縮
        return par[x]
def same(x,y):
    return find(x) == find(y)
def unite(x,y):
    x = find(x)
    y = find(y)
    if x == y:
      return 0
    else:
      if par[x] > par[y]:
        x,y = y,x
      par[x] += par[y]
      par[y] = x
def size(x):
    return -par[find(x)]

max_size=0
for _ in range(m):
  a,b=map(int,input().split())
  unite(a,b)
  ss=max(size(a),size(b))
  max_size=max(ss,max_size)

print(max_size)

Submission status: Wrong Answer

Solution code:
import collections
n,m = map(int,input().split())
ls = []
for i in range(m):
    a,b = map(int,input().split())
    tmp = 0
    for i in range(len(ls)):
      if (a in ls[i]):
        ls[i].append(b)
        tmp = 1
        break
      elif (b in ls[i]):
        ls[i].append(a)
        tmp = 1
        break
    if tmp == 0:
    	ls.append([a,b])
#print(ls)
if len(ls) == 1:
  c = collections.Counter(ls[0])
  print(len(c))
else:
  ans = 0
  for i in ls:
    ans = max(ans,len(i))
  print(ans)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
a = []
con = []
for i in range(m):
    b = list(map(int,input().split()))
    b.sort()
    a.append(b)
a.sort(key=lambda x: x[0])
n = 0
for i in range(m):
    if i == 0:
        con.append(a[i])
    else:
        if a[i][0] in con[n]:
            if a[i][1] in con[n]:
                continue
            else:
                con[n].append(a[i][1])
        else:
            con.append(a[i])
            n+=1
ans = 0
for i in range(len(con)):
    ans = max(ans,len(con[i]))
print(ans)

Submission status: Wrong Answer

Solution code:
def resolve():
    n, m = map(int, input().split())
    s = [[j] for j in range(n)]
    for _ in range(m):
        a,b  = map(int, input().split())
        a-=1
        b-=1
        s[a]=list(set(s[b]+s[a]))
        s[b]=list(set(s[a]+s[b]))
    ans=0
    for i in range(len(s)):
        ans=max(len(s[i]),ans)
    print(ans)
resolve()

Submission status: Wrong Answer

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

d={}
dsu=[]
for i in range(n):
    dsu.append(i+1)

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

    if b in d:
        d[b].add(a)
    else:
        d[b]=set()
        d[b].add(a)
    


maxi=-1
# print(d)

for i in d:
    if len(d[i])>maxi:
        maxi=len(d[i])

print(maxi+1)

Submission status: Wrong Answer

Solution code:
import collections
from typing import Set, List, Dict

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

G: List[Set[int]] = []
lookup_tab: Dict[int, int] = {}
inv_tab: Dict[int, Set[int]] = collections.defaultdict(set)
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})
        gid = len(G) - 1
        lookup_tab[a] = gid
        lookup_tab[b] = gid
        inv_tab[gid].add(a)
        inv_tab[gid].add(b)
    elif a in lookup_tab and b not in lookup_tab:
        gid = lookup_tab[a]
        lookup_tab[b] = gid
        inv_tab[gid].add(b)
        G[gid].add(b)
    elif b in lookup_tab and a not in lookup_tab:
        gid = lookup_tab[b]
        lookup_tab[a] = gid
        inv_tab[gid].add(a)
        G[gid].add(a)
    else:
        a_gid = lookup_tab[a]
        b_gid = lookup_tab[b]
        if a_gid != b_gid:
            # merge two groups
            for k in inv_tab[b_gid]:
                lookup_tab[k] = a_gid
            G[a_gid] = G[a_gid].union(G[b_gid])
        else:
            continue

if G:
    print(max([len(g) for g in G]))
else:
    print(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())


def f(n, m, ab):
    graph = [[] for _ in range(n+1)]
    for a, b in zip(ab, ab):
        if b not in graph[a]:
            graph[a].append(b)
        if a not in graph[b]:
            graph[b].append(a)

    max_cnt = 0

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


print(f(n, m, ab))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
fst_line = input().split(" ")
p_num = int(fst_line[0])
joken_num = int(fst_line[1])

if joken_num == 0:
    print(1)

else:
    joken_list = []
    for i in range(joken_num):
        temp = input().split(" ")
        temp_list = [int(n) for n in temp]
        if(temp_list[1] < temp_list[0]):
            tmp = temp_list[1]
            temp_list[1] = temp_list[0]
            temp_list[0] = tmp
        
        temp_line = (str(temp_list[0])+" "+str(temp_list[1]))
        
        joken_list.append(temp_line)
    
    joken_list.sort()
    #print(joken_list)
    
    del_list=[]
    for i in range(joken_num - 1):
        if(joken_list[i]==joken_list[i+1]):
            del_list.append(i+1)
    del_list.sort(reverse = True)
    for i in range(len(del_list)):
        del joken_list[del_list[i]]
    
    #print(joken_list)
    
    group_list = []
    for i in range(p_num):
        group_list.append(i)
        
    num_joken_list = []
    for i in range(len(joken_list)):
        temp = joken_list[i].split(" ")
        temp_list = []
        temp_list.append(int(temp[0]))
        temp_list.append(int(temp[1]))
        num_joken_list.append(temp_list)
        
    #print(num_joken_list)
    
    #print(group_list)
    for i in range(len(num_joken_list)):
        a = num_joken_list[i][0]
        b = num_joken_list[i][1]
        #print(a,end=" ")
        #print(b)
        #print(group_list[b - 1],end=" ")
        group_list[b - 1] = group_list[a - 1]
        #print(group_list[b - 1])    
        
    group_list.sort()
    #print(group_list)
    
    max_num = 1
    temp = 1
    for i in range(p_num - 1):
        if group_list[i]==group_list[i+1]:
            temp +=1
        else:
            if temp > max_num:
                max_num = temp
            temp = 1
    
    if temp > max_num:
        max_num = temp
        
    print(max_num)

Submission status: Wrong Answer

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

n,m=map(int,input().split())
#p=[[-1]*2 for ii in range(m)]
p = [tuple(sorted(list(map(int,input().split())))) for ii in range(m)]
p=list(set(p))
p=[list(ii) for ii in p]

m=len(p)
#print(p)

g=[-1]*(n+1)
r=0
rr=[2]
#print(g)
import numpy as np
for ii in range(m):
  if g[p[ii][0]]<0 and g[p[ii][1]]<0:
    g[p[ii][0]]=r
    g[p[ii][1]]=r
    if r>0:
      rr.append(2)
    r+=1
  elif g[p[ii][0]]>=0 and g[p[ii][1]]>=0:
    g0=g[p[ii][0]]
    g1=g[p[ii][1]]
    if g0!=g1:
      #print(g0,g1)
      if rr[g0]>rr[g1]:
        g0,g1=g1,g0
      rr[g1]+=rr[g0]
      rr[g0]=0
      for jj in np.arange(1,n+1):
        if g[jj]==g0:
          g[jj]=g1
  elif g[p[ii][0]]>=0:
    g[p[ii][1]]=g[p[ii][0]]
    rr[g[p[ii][0]]]+=1
  elif g[p[ii][1]]>=0:
    g[p[ii][0]]=g[p[ii][1]]
    rr[g[p[ii][1]]]+=1
  #print(g)
  #print(rr)
#print(g)
#print(rr)
print(max(rr))

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split())
friends = set()
for i in range(m):
  friends.add(tuple(map(int,input().split())))

friendd = {}
for i,j in friends:
  if i in friendd:
    friendd[i].add(j)
  else:
    friendd[i] = {j}
  if j in friendd:
    friendd[j].add(i)
  else:
    friendd[j] = {i}
#print(friendd)

ans = 0
for i, s in friendd.items():
  ans = max(ans, len(s)+1) 
  
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

N, M = map(int, input().split())
friend = defaultdict(set)
leader = []
for i in range(M):
    Ai, Bi = map(int, input().split())
    friend[Ai].add(Bi)
    friend[Bi].add(Ai)

already = {}
answer = 0
for i in range(1, N + 1):
    if i in already:
        continue
    if i not in friend:
        size = 1
        if size > answer:
            answer = size
        continue

    queue = []
    for f in friend[i]:
        already[f] = True
        queue.append(f)
    already[i] = True
    size = 0
    while len(queue) > size:
        item = queue[size]
        size += 1
        if item in friend:
            for f in friend[item]:
                if f not in already:
                    already[f] = True
                    queue.append(f)
    if size > answer:
        answer = size
print(answer + 1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
num=[[_+1] for _ in range(n)]
for i in range(m):
    a,b=map(int,input().split())
    num[a-1].append(b)
    num[b-1].append(a)
ans=0
for i in range(n):
    cnt=set()
    for j in range(len(num[i])):
        cnt.add(num[i][j])
    ans=max(ans,len(cnt))
print(ans)

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]
    print(max(max(people), done.count(-1)))
    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
d={}
for i in range(n):
    d[i]=0
mem=[set() for i in range(n)]
for i in range(m):
    n1,n2=map(int,input().split())
    if n1<=n2:
        a,b=n1-1,n2-1
    else:
        a,b=n2-1,n1-1
    if not b in mem[a]:
        d[a]+=1
        mem[a].add(b)
m=0
for i in range(n):
    m=max(m,d[i])
print(m+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)

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:
#!/usr/bin/env python3
import sys
from collections import defaultdict
import copy

input = sys.stdin.readline


def I():
    return int(input())


def MI():
    return map(int, input().split())


N, M = MI()

d = defaultdict(set)

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

for _ in range(M):
    a, b = MI()
    d[a].add(b)
    d[b].add(a)

ans = 0
for value in d.values():
    tmp = copy.copy(value)
    for v in value:
        tmp |= d[v]
    ans = max(ans, len(tmp))

print(ans)

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
track=(n+1)*[0]
l=[]
for i in range(q):
    j, k = map(int, input().split())
    j, k = min(j, k), max(j, k)
    l.append([j,k])
l.sort()

for i in l:
    j=i[0]
    k=i[1]
    if track[j]==0:
        track[j]={k:1}
        track[k]=-j
    elif type(track[j]) is int and track[j]<0:
        track[-track[j]][k]=1
        track[k]=track[j]
    else:
        track[j][k]=1
ans=1
for i in track:
    if type(i) is dict:
        ans=max(ans,1+len(i))
print(ans)

Submission status: Wrong Answer

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

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

groups = [[]]
_max = []

for m in range(M):
    A, B = map(int, input().split())
    for i,group in enumerate(groups):
        if (A in group)and(B in group):
            break
        elif (A in group):
            groups[i].append(B)
        elif (B in group):
            groups[i].append(A)
        else:
            groups.append([A,B])

flag = 0

for n in range(1,N+1,1):
    for group in groups:
        if n in group:
            flag += 1
    if flag==0:
        groups.append([n])

for group in groups:
    _max.append(len(group))

print(max(_max))

Submission status: Wrong Answer

Solution code:
def main():

    n, m = map(int, input().split())
    relations = {}
    for i in range(m):
        nums = list(map(int, input().split()))
        nums.sort()
        if nums[0] not in relations:
            relations[nums[0]] = [nums[1]]
        elif nums[1] not in relations[nums[0]]:
            relations[nums[0]].append(nums[1])
        nums.reverse()
        if nums[0] not in relations:
            relations[nums[0]] = [nums[1]]
        elif nums[1] not in relations[nums[0]]:
            relations[nums[0]].append(nums[1])
    ans = 0
    for key in relations:
        ans = max(len(relations[key]), ans)
    print(ans + 1)

if __name__ == '__main__':
    main()

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)
    union = friends[A] | friends[B]
    friends[A] = union
    friends[B] = union

    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[B] += 1
      for friend in friends[B]:
        num_of_friend[friend] = num_of_friend[B]
    elif num_of_friend[B] == 0:
      num_of_friend[A] += 1
      for friend in friends[A]:
        num_of_friend[friend] = num_of_friend[A]
    else:
      count_union = len(friends[A]) - 1
      for friend in friends[A]:
        num_of_friend[friend] = count_union

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

n, m = map(int, input().split())
if m == 0:
    print(1)
    exit()
elif m == 1:
    print(2)
    exit()

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("parent: ", u.parent)
print(ans)

Submission status: Wrong Answer

Solution code:
N, M = [ int(v) for v in input().split(" ") ]
r = [0]*N

cache = []
for i in range(M):
  A, B = [ int(v) for v in input().split(" ") ]
  if set([A,B]) in cache:
    continue
  r[A-1] += 1
  r[B-1] += 1
  cache.append(set([A,B]))

#print(cache)
#print(r)
print(max(r)+1)

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())
    n = min(group[a],group[b])
    group[a] = n
    group[b] = n
p = Counter(group.values())
print(max(p.values()))

Submission status: Wrong Answer

Solution code:
def tuika(a,b):
    if a in dicSet:
        dic[a].add(b)
    else:
        dic[a] = set()
        dic[a].add(b)
        dicSet.add(a)



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)
    for j in dic[a]:
        #print(dic[a],b)
        tuika(j,b)

    if b in dicSet:
        dic[b].add(a)
    else:
        dic[b] = set()
        dic[b].add(a)
        dicSet.add(b)
    for j in dic[b]:
       tuika(j,a)

ans = 1

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

#print(dic)

print(ans)

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)])
for i in range(M):
    A, B = map(int, input().split())    
    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:
from collections import Counter
persons=list(map(int, input().rstrip().split()))
n=persons[0]
m=persons[1]
ar1=[]
ar2=[]
temp=list(range(n))
for i in range(m):
    arr=list(map(int, input().rstrip().split()))
    ar1.append(arr[0])
    ar2.append(arr[1])
for j in range(m):
    if temp[ar1[j]-1] != temp[ar2[j]-1]:
        if ar1[j]<ar2[j]:
            temp[ar2[j]-1]=temp[ar1[j]-1]
        else:
            temp[ar1[j]-1]=temp[ar2[j]-1]
z = Counter(temp)
res=z.most_common(1)[0][0]
print(temp.count(res))

Submission status: Wrong Answer

Solution code:
import copy
n, m = map(int, input().split())
data = [[i] for i in range(n)]
ans = 0
for i in range(m):
    a, b = map(int, input().split())
    if b - 1 not in data[a - 1]:
        data[a - 1].append(b - 1)
    if a - 1 not in data[b - 1]:
        data[b - 1].append(a - 1)
res = copy.deepcopy(data)
for s in range(n):
    for t in range(len(data[s])):
        if s != t:
            res[data[s][t]].extend(data[s])

for k in range(n):
    ans = max(len(set(res[k])), ans)
print(ans)

Submission status: Wrong Answer

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

AB=[0]*n
Mindex=[0]*n
Minfo=[0]*(m+1)

MinfoIndex=1

for A in Alist:
  a=A[0]-1
  b=A[1]-1
  
  if AB[a]+AB[b]==0:
    AB[a]+=1
    AB[b]+=1
    Mindex[a]=MinfoIndex
    Mindex[b]=MinfoIndex
    Minfo[MinfoIndex]+=2
    MinfoIndex+=1
  else:
    if AB[a]>0 and AB[b]>0:
      pass
    elif AB[a]>0:
      AB[b]+=1
      Mindex[b]=Mindex[a]
      Minfo[Mindex[b]]+=1
    elif AB[b]>0:
      AB[a]+=1
      Mindex[a]=Mindex[b]
      Minfo[Mindex[b]]+=1   
    
    
print(max(Minfo))

Submission status: Wrong Answer

Solution code:
import numpy as np
N, M = map(int, input().split())
A = np.zeros((M,2), dtype=int)

for i in range(M):
    Ai = input().split()
    
    A[i,0] = min(int(Ai[0]), int(Ai[1]))
    A[i,1] = max(int(Ai[0]), int(Ai[1]))

Asort = np.sort(A, axis=0)

Grp_lst = np.arange(1, N+1)
for i in range(M):
    Grp_lst[Asort[i,1] - 1] = Grp_lst[Asort[i,0] - 1]

count = np.zeros(N,dtype=int)

for i in range(N):
    count[Grp_lst[i]-1] += 1

print(max(count))

Submission status: Wrong Answer

Solution code:
n,q=map(int,input().split())
track=(n+1)*[0]
l=[]
for i in range(q):
    j, k = map(int, input().split())
    j, k = min(j, k), max(j, k)
    l.append([j,k])
l.sort()

for i in l:
    j=i[0]
    k=i[1]
    if track[j]==0:
        track[j]={k:1}
        track[k]=-j
    elif type(track[j]) is int and track[j]<0:
        track[-track[j]][k]=1
        track[k]=track[j]
    else:
        track[j][k]=1
ans=1
for i in track:
    if type(i) is dict:
        ans=max(ans,1+len(i))
print(track)
print(ans)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
freind = set()
freindlist = []
log = dict()
mea = 0
sig = 0
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
    elif A in freind:
        freind.add(B)
        sig = log[str(A)] 
        freindlist[sig].add(B)
        log[str(B)] = sig
    elif B in freind:
        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:
from collections import deque

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

friends_map = {}

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

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

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

stack = deque()
stack.appendleft(0)

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

group = 0
while stack:
    member = stack.popleft()
    group += 1
    remain_user.discard(member)

    if member in friends_map:
        ng_set = friends_map[member]
    else:
        ng_set = set()

    if not remain_user:
        print(group)
        exit()

    for target in list(remain_user):
        if not target in ng_set:
            # グループに参加させる
            remain_user.discard(target)

            if target in friends_map:
                ng_set |= friends_map[target]
        else:
            # 新しいグループを作る
            stack.appendleft(target)

print(group)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m = map(int,input().split())
l = [set([]) for i in range(n+1)]
for i in range(m):
  a,b = map(int,input().split())
  l[a].add(b)
  l[b].add(a)
Q = deque()
for i in range(n):
  Q.append(i+1)
vis = [-1]*(n+1)
vis[1] = 1
while Q:
  temp = Q.popleft()
  for v in l[temp]:
    if vis[v]!=-1:
      continue
    vis[temp] += 1
    vis[v] = vis[temp]
    Q.appendleft(v)
print(max(vis))

Submission status: Wrong Answer

Solution code:
import collections

# https://www.ics.uci.edu/~eppstein/PADS/UnionFind.py


class UnionFind:
    """Union-find data structure.

    Each unionFind instance X maintains a family of disjoint sets of
    hashable objects, supporting the following two methods:

    - X[item] returns a name for the set containing the given item.
      Each set is named by an arbitrarily-chosen one of its members; as
      long as the set remains unchanged it will keep the same name. If
      the item is not yet part of a set in X, a new singleton set is
      created for it.

    - X.union(item1, item2, ...) merges the sets containing each item
      into a single larger set.  If any item is not yet part of a set
      in X, it is added to X as one of the members of the merged set.
    """

    def __init__(self):
        """Create a new empty union-find structure."""
        self.weights = {}
        self.parents = {}

    def __getitem__(self, object):
        """Find and return the name of the set containing the object."""

        # check for previously unknown object
        if object not in self.parents:
            self.parents[object] = object
            self.weights[object] = 1
            return object

        # find path of objects leading to the root
        path = [object]
        root = self.parents[object]
        while root != path[-1]:
            path.append(root)
            root = self.parents[root]

        # compress the path and return
        for ancestor in path:
            self.parents[ancestor] = root
        return root

    def __iter__(self):
        """Iterate through all items ever found or unioned by this structure."""
        return iter(self.parents)

    def union(self, *objects):
        """Find the sets containing the objects and merge them all."""
        roots = [self[x] for x in objects]
        heaviest = max([(self.weights[r], r) for r in roots])[1]
        for r in roots:
            if r != heaviest:
                self.weights[heaviest] += self.weights[r]
                self.parents[r] = heaviest


n, m = [int(x) for x in input().split()]
uf = UnionFind()
for i in range(1, n + 1):
    uf[i]

for _ in range(m):
    a, b = [int(x) for x in input().split()]
    uf.union(a, b)
print(collections.Counter(uf.parents.values()).most_common(1)[0][1])

Submission status: Wrong Answer

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

for x in range(N+1):
    F += [[x+1]]

for p in range(M):
    F[AB[p][0]] += [AB[p][1]]
    F[AB[p][1]] += F[AB[p][0]]

for q in range(N+1):
    F[q] = list(set(F[q]))

for r in range(N+1):
    L += [len(F[r])]

print(max(L))

Submission status: Wrong Answer

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

friend_list = [set()]

for i in range(m):
    a, b = map(int, input().split())
    found = False
    for friend_set in friend_list:
        if a in friend_set:
            friend_set.add(b)
            found = True
            break
        elif b in friend_set:
            friend_set.add(a)
            found = True
            break
    if not found:
        friend_set = set()
        friend_set.add(a)
        friend_set.add(b)
        friend_list.append(friend_set)

ans = 0
for friend_set in friend_list:
    ans = max(len(friend_set), ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import math

n, m = map(int, input().split(" "))
abL = [list(map(int, input().split(" "))) for _ in range(m)]

d = {}

for a, b in abL:
    if a in d:
        d[a].add(b)
    else:
        sa = set()
        sa.add(b)
        d[a] = sa

    if b in d:
        d[b].add(a)
    else:
        sb = set()
        sb.add(a)
        d[b] = sb

ans = -1

for k, v in d.items():
    ans = max(len(v) + 1, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
INF = 10**18
sys.setrecursionlimit(10**6)

def li():
    return [int(x) for x in input().split()]

N, M = li()
L = []
for i in range(M):
    a, b = sorted(li())
    L.append((a, b))

L = set(L)

cnts = [0] * (N+1)

for l in L:
    a, b = l
    cnts[a] += 1

ans = max(cnts) + 1
print(ans)

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

    for i in range(M):
        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()