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 bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

def main():
    from collections import deque
    N, M = MI()
    fri_list = [[] for _ in range(N)]
    for i in range(M):
        a, b = MI()
        fri_list[a - 1].append(b - 1)
        fri_list[b - 1].append(a - 1)


    group_list = []
    num_list = [0] * N
    for i in range(0, N):
        if num_list[i] == 0:
            cnt = 0
            queue = deque([i])
            while queue:
                v = queue.popleft()
                for j in fri_list[v]:
                    if num_list[j] == 0:
                        num_list[j] = 1
                        queue.append(j)
                        cnt += 1
            group_list.append(cnt)

    ans = max(group_list)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

def main():
    from collections import deque
    N, M = MI()
    fri_list = [[] for _ in range(N)]
    for i in range(M):
        a, b = MI()
        fri_list[a - 1].append(b - 1)
        fri_list[b - 1].append(a - 1)


    group_list = []
    num_list = [0] * N
    for i in range(0, N):
        if num_list[i] == 0:
            cnt = 0
            queue = deque([i])
            while queue:
                v = queue.popleft()
                for i in fri_list[v]:
                    if num_list[i] == 0:
                        num_list[i] = 1
                        queue.append(i)
                        cnt += 1
            group_list.append(cnt)

    ans = max(group_list)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
"""
自分の解答
"""

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

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

R = [set(map(int, input().split(" "))) for i in range(M)]

groups = []
while len(R) > 0:
    r = R.pop()
    for r_ in R:
        if len(r & r_) > 0:
            r |= r_
    groups.append(r)

print(max(map(len, groups)))

Submission status: Wrong Answer

Solution code:
def main():
    n, m, *ab, = map(int, open(0).read().split())
    p = [-1] * (n + 1)
    ans = -1

    def find(x):
        while p[x] > 0:
            x = p[x]
        return x

    def union(i, j):
        pi, pj = find(i), find(j)
        if pi == pj:
            return
        if p[pi] < p[pj]:
            pi, pj = pj, pi
            i, j = j, i
        p[pi] += p[pj]
        p[pj] = pi
        reconnect(j, pi)

    def reconnect(i, j):
        while p[i] > 0:
            k = p[i]
            p[i] = j
            i = k

    for i, j in zip(ab[::2], ab[1::2]):
        union(i, j)

    print(-ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

def main():
    from collections import deque
    N, M = MI()
    fri_list = [[] for _ in range(N)]
    for i in range(M):
        a, b = MI()
        fri_list[a - 1].append(b - 1)
        fri_list[b - 1].append(a - 1)


    group_list = []
    num_list = [0] * (N + 1)
    for i in range(0, N):
        if num_list[i] == 0:
            cnt = 0
            queue = deque([i])
            while queue:
                v = queue.popleft()
                for i in fri_list[v]:
                    if num_list[i] == 0:
                        num_list[i] = 1
                        queue.append(i)
                        cnt += 1
            group_list.append(cnt)

    ans = max(group_list)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

def main():
    from collections import deque
    N, M = MI()
    fri_list = [[-1] for _ in range(N)]
    for i in range(M):
        a, b = MI()
        fri_list[a - 1].append(b)
        fri_list[b - 1].append(a)


    group_list = []
    num_list = [0] * (N + 1)
    for i in range(1, N + 1):
        if num_list[i] == 0:
            cnt = 0
            queue = deque([i])
            while queue:
                v = queue.popleft()
                for i in fri_list[v - 1]:
                    if i == -1:
                        continue
                    if num_list[i] == 0:
                        num_list[i] = 1
                        queue.append(i)
                        cnt += 1
            group_list.append(cnt)

    ans = max(group_list)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

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

graph = [[] for _ in range(n+1)]

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

dist = [-1] * (n+1)
dist[0] = 0
dist[1] = 0

d = deque()
d.append(1)

while d:
 v = d.popleft()
 for i in graph[v]:
   if dist[i] != -1:
     continue
   dist[i] = dist[v] + 1
   d.append(i)

ans = dist[1:]
print(ans.count(1) +1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
l=[]
for i in range(m):
    temp=list(map(int,input().split()))
    l.append(temp)
group=[{i} for i in range(n+1)]

for a,b in l:
    group[a].add(b)
    group[b].add(a)
ans=max(list(map(len,group)))
print(ans)

Submission status: Wrong Answer

Solution code:
import collections

n, m = map(int, input().split())
if m == 0:
    print(1)
    exit()
if m == 1:
    print(2)
    exit()
boss = {i: 0 for i in range(1, n + 1)}

def look_boss(num):
    while num != boss[num]:
        num = boss[num]
    return num

for _ in range(m):
    a, b = map(int, input().split())
    if boss[a] == 0 and boss[b] != 0:
        boss[a] = look_boss(b)
    elif boss[a] != 0 and boss[b] == 0:
        boss[b] = look_boss(a)
    elif boss[a] == 0 and boss[b] == 0:
        boss[a] = a
        boss[b] = a
    elif boss[a] != boss[b]:
        boss[boss[b]] = look_boss(a)

tmp = collections.Counter(boss.values())
if 0 in tmp:
    tmp.pop(0)
print(max(tmp.values()))

Submission status: Wrong Answer

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

def main():
    from collections import deque
    N, M = MI()
    fri_list = [[-1] for _ in range(N)]
    for i in range(M):
        a, b = MI()
        fri_list[a - 1].append(b)
        fri_list[b - 1].append(a)


    group_list = []
    num_list = [0] * (N + 1)
    for i in range(1, N + 1):
        if num_list[i] == 0:
            G = []
            queue = deque([i])
            G.append(i)
            while queue:
                v = queue.popleft()
                for i in fri_list[v - 1]:
                    if i == -1:
                        continue
                    if num_list[i] == 0:
                        num_list[i] = 1
                        queue.append(i)
                        G.append(i)
            group_list.append(G)

    len_list = []
    for l in group_list:
        x = len(l)
        len_list.append(x)
    ans = max(len_list)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import math
import queue

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

d = {}

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

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

ans = 1
for k, v in d.items():
    if not visited[k]:
        cnt = 0
        q = queue.Queue()
        q.put(k)
        while q.qsize() != 0:
            ss = q.get()
            visited[ss] = True
            cnt += 1
            if cnt >= n:
                break
            for dd in list(d[ss]):
                if not visited[dd]:
                    q.put(dd)
        ans = max(ans, cnt)

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import queue

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

d = {}

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

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

ans = 1
for k, v in d.items():
    if not visited[k]:
        cnt = 0
        q = queue.Queue()
        q.put(k)
        while q.qsize() != 0:
            ss = q.get()
            cnt += 1
            if cnt >= n:
                break
            for dd in list(d[ss]):
                if not visited[dd]:
                    q.put(dd)
        visited[k] = True
        ans = max(ans, cnt)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

def main():
    from collections import deque
    N, M = MI()
    fri_list = [[-1] for _ in range(N)]
    for i in range(M):
        a, b = MI()
        fri_list[a - 1].append(b)
        fri_list[b - 1].append(a)

    def bfs(u, n, g, A, B):
        queue = deque([u])
        A.append(u)
        while queue:
            v = queue.popleft()
            for i in g[v - 1]:
                if i == -1:
                    continue
                if B[i] == 0:
                    B[i] = 1
                    queue.append(i)
                    A.append(i)

    group_list = []
    num_list = [0] * (N + 1)
    for i in range(1, N + 1):
        if num_list[i] == 0:
            G = []
            bfs(i, N, fri_list, G, num_list)
            group_list.append(G)

    len_list = []
    for l in group_list:
        x = len(l)
        len_list.append(x)
    ans = max(len_list)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

def main():
    from collections import deque
    N, M = MI()
    fri_list = [[-1] for _ in range(N)]
    for i in range(M):
        a, b = MI()
        fri_list[a - 1].append(b)
        fri_list[b - 1].append(a)

    def bfs(u, n, g, A, B):
        queue = deque([u])
        A.append(u)
        while queue:
            v = queue.popleft()
            for i in g[v - 1]:
                if i == -1:
                    continue
                if B[i] is 0:
                    B[i] = 1
                    queue.append(i)
                    A.append(i)

    group_list = []
    num_list = [0] * (N + 1)
    for i in range(1, N + 1):
        if num_list[i] == 0:
            G = []
            bfs(i, N, fri_list, G, num_list)
            group_list.append(G)

    len_list = []
    for l in group_list:
        x = len(l)
        len_list.append(x)
    ans = max(len_list)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

A = [[]]
c = 0

for i in range(m):
    a, b = map(int, input().split())
    c = 0
    for j in range(len(A)):
        a_in = a in A[j]
        b_in = b in A[j]
        if a_in == True and b_in == False:
            A[j].append(b)
            c = 1
            break
        elif b_in == True and a_in == False:
            A[j].append(a)
            c = 1
            break
        elif a_in == True and b_in == True:
            c = 1
            break
    if c == 0:
        A.append([a, b])

c = 0
for i in A:
    c = max(c, len(i))
print(c)

Submission status: Wrong Answer

Solution code:
import collections

n, m = map(int, input().split())
if m == 0:
    print(1)
    exit()
if m == 1:
    print(2)
    exit()
ids = {i: 0 for i in range(1, n+1)}
for _ in range(m):
    a, b = map(int, input().split())
    if ids[a] == 0 and ids[b] != 0:
        ids[a] = ids[b]
    elif ids[a] != 0 and ids[b] == 0:
        ids[b] = ids[a]
    elif ids[a] == 0 and ids[b] == 0:
        ids[a] = a
        ids[b] = a
    elif ids[a] != ids[b]:
        ids[ids[b]] = ids[a]
real = []
for i in ids:
    if ids[i] == 0:
        continue
    t = i
    while t != ids[t]:
        t = ids[t]
    ids[i] = t
tmp = collections.Counter(ids.values())
if 0 in tmp:
    tmp.pop(0)
print(max(tmp.values()))

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
branch = list(tuple(map(int,input().split())) for i in range(m))
que = range(n)
flag = -1
friend = [{1}]

for a,b in branch:
    for i in range(len(friend)):
        if (a in friend[i]) or (b in friend[i]):
            friend[i].add(a)
            friend[i].add(b)
            if flag >= 0:
                friend[flag] = friend[flag]|friend[i]
                friend.pop(i)
                break
            flag = i
        else:
            if flag == -1:
                friend.append({a,b})
                flag += 1
    flag = -1

len_friend = list(len(i) for i in friend)
print(max(len_friend))

Submission status: Wrong Answer

Solution code:
class UnionFind():
  def __init__(self, n):
    self.n = n
    self.parents = [-1] * n

  def find(self, x):
    if self.parents[x] < 0:
      return x
    else:
      self.parents[x] = self.find(self.parents[x])
      return self.parents[x]

  def union(self, x, y):
    x = self.find(x)
    y = self.find(y)

    if x == y:
      return

    if self.parents[x] > self.parents[y]:
      x, y = y, x

    self.parents[x] += self.parents[y]
    self.parents[y] = x

  def size(self, x):
    return -self.parents[self.find(x)]

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

  def members(self, x):
    root = self.find(x)
    return [i for i in range(self.n) if self.find(i) == root]

  def roots(self):
    return [i for i, x in enumerate(self.parents) if x < 0]

  def group_count(self):
    return len(self.roots())

  def all_group_members(self):
    return {r: self.members(r) for r in self.roots()}

  def __str__(self):
    return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

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

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

  print(min(friends.parents))

if __name__ == "__main__":
  resolve()

Submission status: Wrong Answer

Solution code:
import math
import queue

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

d = {}

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

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

ans = 1
for k, v in d.items():
    if not visited[k]:
        cnt = 0
        q = queue.Queue()
        q.put(k)
        while q.qsize() != 0:
            ss = q.get()
            cnt += 1
            if cnt >= n:
                break
            for dd in list(d[ss]):
                if not visited[dd]:
                    q.put(dd)
                    visited[ss] = True
        ans = max(ans, cnt)

print(ans)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
if M==0:
    print(N)
else:
    A = [list(map(int,input().split())) for _ in range(M)]
    frends=[{*A[0]}]
    for (a,b) in A:
        flg = True
        for ifre in range(len(frends)):
            if (a in frends[ifre]) or (b in frends[ifre]):
                frends[ifre].add(a)
                frends[ifre].add(b)
                flg=False
                break
        if flg:
            frends.append({a,b})
    print(max([len(f) for f in frends]))

Submission status: Wrong Answer

Solution code:
N, M = map(int,input().split())
communities = [0 for i in range(N)]
same = []
num = 1
for i in range(M):
  A, B = map(int,input().split())
  if communities[A-1] == 0 and communities[B-1] == 0:
    communities[A-1] = num
    communities[B-1] = num
    num += 1
  elif communities[A-1] == 0:
    communities[A-1] = communities[B-1]
  elif communities[B-1] == 0:
    communities[B-1] = communities[A-1]
  elif communities[A-1] != communities[B-1]:
    same.append(min(communities[A-1], communities[B-1]))
    same.append(max(communities[A-1], communities[B-1]))
  else:
    continue

if len(communities) > 0:
  communities.sort()
members = [0 for i in range(num)]
for i in range(N):
  if communities[i] == 0:
    continue
  else:
    members[communities[i]-1] += 1
for i in range(len(same)//2):
  members[same[2*i]-1] += members[same[2*i+1]-1]
print(max(members))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict as dd
from collections import deque
d=dd(list)
q=deque([])

N,M=map(int, input().split())
for _ in range(M):
    f=0
    i,j=map(int, input().split())
    if i>j:
        i,j=j,i
    if j in d[i]:
        continue
    else:
        d[i].append(j)

chk=[0]*N
chk[0]=1
q.append(0)
a=0
while 0 not in chk:
    temp=q.pop()
    for i in range(d[temp]):
        chk[d[temp][i]-1]=1
        q.append(d[temp][i])

    if q==0:
        while a<N:
            a+=1
            if chk[a]==0:
                q.append(a+1)

ans=0
for i in  range(len(d)):
    ans=max(ans,len(d[i])+1)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
import collections
from collections import deque
from collections import defaultdict

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD2 = 998244353
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

def main():
 
    N, M = NMI()
    AB = [NLI() for _ in range(M)]
    
    #各頂点の遡った親が同じなら、同じグループ
    parents = [i for i in range(N+1)] # 最初は自分自身が親。
 
    
    def find_parent(x):#ある子の親を探す
        if parents[x] == x:#自分自身が親ならそれでいい
            return parents[x]
        else:
            parents[x] = find_parent(parents[x])#親の値を親の親の…親に書き換える
            return parents[x]
    
    
    def unite_children(x,y):#親を同じにする
        parent_x = find_parent(x)
        parent_y = find_parent(y)
        if parent_x == parent_y:#すでに親が同じなら何もしない
            return
        parents[x] = min(parent_x,parent_y)
        parents[y] = min(parent_x,parent_y)

 
        
    for m in range(M):
        a = AB[m][0]
        b = AB[m][1]
        
        if a > b:
            a,b = b,a
        
        unite_children(a,b)

    
    for n in range(N+1):
        parents[n] = find_parent(n)

    del parents[0]
    pa = collections.Counter(parents)

    print(pa.most_common()[0][1])
 

 
 
 
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
if M==0:
    print(N)
else:
    A = [list(map(int,input().split())) for _ in range(M)]
    frends=[{*A[0]}]
    for (a,b) in A:
        flg = True
        for ifre in range(len(frends)):
            if (a in frends[ifre])|(b in frends[ifre]):
                frends[ifre].add(a)
                frends[ifre].add(b)
                flg=False
                break
        if flg:
            frends.append({a,b})
    print(max([len(f) for f in frends]))

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
if M==0:
    print(N)
else:
    A = [list(map(int,input().split())) for _ in range(M)]
    frends=[{*A[0]}]
    for (a,b) in A:
        flg = True
        for ifre in range(len(frends)):
            if (a in frends[ifre])|(b in frends[ifre]):
                frends[ifre].add(a)
                frends[ifre].add(b)
                flg=False
                break
        if flg:
            print(a,b)
            frends.append({a,b})
    print(max([len(f) for f in frends]))

Submission status: Wrong Answer

Solution code:
def search(x, num):
    nodes.remove(x)
    for y in edge[x]:
        edge[y].discard(x)
#     for y in list(edge[x]):
#         if y in nodes:
#             num = search(y, num)
    return num + 1

(n,m),*d  = [list(map(int, s.split())) for s in open(0)]

edge = [set() for _ in range(n+1)]
for x in d:
    edge[x[0]].add(x[1])
    edge[x[1]].add(x[0])

nodes = set(range(1,n+1))
ans = 1
for x in list(nodes):
    if x in nodes:
        ans = max(ans, search(x, 0))
print(ans)

Submission status: Wrong Answer

Solution code:
def search(x, num):
    nodes.remove(x)
#     for y in edge[x]:
#         edge[y].discard(x)
#     for y in list(edge[x]):
#         if y in nodes:
#             num = search(y, num)
    return num + 1

(n,m),*d  = [list(map(int, s.split())) for s in open(0)]

edge = [set() for _ in range(n+1)]
for x in d:
    edge[x[0]].add(x[1])
    edge[x[1]].add(x[0])

nodes = set(range(1,n+1))
ans = 1
for x in list(nodes):
    if x in nodes:
        ans = max(ans, search(x, 0))
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin

# from pprint import pprint
# import math
# import collections

# n = stdin.readline().rstrip()
# n = int(n)

n, m = [int(x) for x in stdin.readline().rstrip().split()]
# a = [int(x) for x in l]

d = [stdin.readline().rstrip().split() for _ in range(m)]
d = [tuple(map(int, x)) for x in d]

# most friends
p_rel = [[_] for _ in range(n)]

for rel in d:
    x = p_rel[rel[0] - 1]
    x.append(rel[1] - 1)

    y = p_rel[rel[1] - 1]
    y.append(rel[0] - 1)

p_rel = [set(_) for _ in p_rel]

# print(p_rel)

max_friends = max([len(_) for _ in p_rel])

friends = [len(_) for _ in p_rel]
max_friends_idx = friends.index(max_friends)

x = set(p_rel[max_friends_idx])
#
ans = len(x)
#
#
if m == 0:
    print(1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

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 = []
        a.append(i)
        while (d):
            k = d.popleft()
            color[k] = "black"
            for j in rinsetu[k]:
                if color[j] == "white":
                    d.append(j)
                    a.append(j)
        grp=max(grp,len(a))
print(grp)

Submission status: Wrong Answer

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

group = [[] for i in range(n)]
group_num = [-1 for i in range(n+1)]
group_count = -1

for i in range(m):
    a, b = map(int, input().split())
    if group_num[a] != -1 and group_num[b] != -1:
        for j in group[group_num[b]]:
            group_num[j] = group_num[a]
        group[group_num[a]] += group[group_num[b]]
    if group_num[a] != -1:
        group[group_num[a]].append(b)
        group_num[b] = group_num[a]
    elif group_num[b] != -1:
        group[group_num[b]].append(a)
        group_num[a] = group_num[b]
    else:
        group_count += 1
        group[group_count].append(a)
        group[group_count].append(b)
        group_num[a] = group_count
        group_num[b] = group_count

len_max = -1
for i in range(len(group)):
    len_max = max(len_max, len(set(group[i])))

print(len_max)

Submission status: Wrong Answer

Solution code:
from sys import stdin

# from pprint import pprint
# import math
# import collections

# n = stdin.readline().rstrip()
# n = int(n)

n, m = [int(x) for x in stdin.readline().rstrip().split()]
# a = [int(x) for x in l]

d = [stdin.readline().rstrip().split() for _ in range(m)]
d = [tuple(map(int, x)) for x in d]

# most friends
p_rel = [[_] for _ in range(n)]

for rel in d:
    x = p_rel[rel[0] - 1]
    x.append(rel[1] - 1)

    y = p_rel[rel[1] - 1]
    y.append(rel[0] - 1)

# print(p_rel)

max_friends = max([len(_) for _ in p_rel])

friends = [len(_) for _ in p_rel]
max_friends_idx = friends.index(max_friends)

#
# # print(p.index(max_friends))
#
x = set(p_rel[max_friends_idx])
#
ans = len(x)
#
#
print(ans)

Submission status: Wrong Answer

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

pg = [-1] * (N + 1)  # person's group
gpc = []  # group person count
gc = 0  # group count

for _ in range(M):
	Ai, Bj = map(int, input().strip().split())

	if pg[Ai] > -1 and pg[Bj] > -1:
		continue

	if pg[Ai] < 0 and pg[Bj] < 0:
		pg[Ai] = gc
		pg[Bj] = gc
		gpc.append(2)
		gc += 1

	if pg[Ai] < 0:
		pg[Ai] = pg[Bj]
		gpc[pg[Ai]] += 1

	if pg[Bj] < 0:
		pg[Bj] = pg[Ai]
		gpc[pg[Bj]] += 1

if not gpc:
	print(1)
else:
	print(max(gpc))

Submission status: Wrong Answer

Solution code:
import math
import queue

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

d = {}

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

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

ans = 1
for k, v in d.items():
    if not visited[k]:
        cnt = 0
        q = queue.Queue()
        q.put(k)
        while q.qsize() != 0:
            ss = q.get()
            cnt += 1
            if cnt >= n:
                break
            visited[ss] = True
            for dd in list(d[ss]):
                if not visited[dd]:
                    q.put(dd)
        ans = max(ans, cnt)

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import queue

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

d = {}

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

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

ans = 0
for k, v in d.items():
    if not visited[k]:
        cnt = 0
        q = queue.Queue()
        q.put(k)
        while q.qsize() != 0:
            ss = q.get()
            cnt += 1
            if cnt >= n:
                break
            visited[ss] = True
            for dd in list(d[ss]):
                if not visited[dd]:
                    q.put(dd)
        ans = max(ans, cnt)

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
from collections import deque
from collections import defaultdict as dd
import sys
sys.setrecursionlimit(10**8)

def DFS():
    temp=q.pop()

    for i in range(len(d[temp])):
        if chk[d[temp][i]-1]==0:
            chk[d[temp][i]-1]=1
            q.append(d[temp][i])
        else:break

        if d[temp][i] not in d[temp]:
            d[temp].append(d[temp][i])

    if len(q)>0:
        DFS()
    else:
        f=0
        for i in range(N):
            if chk[i]==0:
                f=1
                chk[i]=1
                q.append(i+1)
                break

        if f==0:
            ans()
        else:
            DFS()
    return

def ans():
    a=0
    for i in range(len(d)):
       a=max(a,len(d[i])+1)
    print(a) 
    exit(0)
    
    return


q=[]
d=dd(list)
N,M=map(int, input().split())
chk=np.zeros(N,dtype=np.int)

for _ in range(M):
    i,j=map(int, input().split())
    if i>j:
        i,j=j,i

    if j not in d[i]:
        d[i].append(j)
    if i not in d[j]:
        d[j].append(i)

q.append(1)
chk[0]=1

DFS()

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m=map(int,input().split())
seen=[False]*n
num=[[] for _ in range(n)]
for i in range(m):
    a,b=map(int,input().split())
    num[min(a,b)-1].append(max(a,b))
ans=0
for i in range(n):
    if seen[i]==False:
        queue=deque()
        queue.append(i)
        cnt=1
        yn=0
        while yn!=1:
            if len(queue)!=0:
                num01=queue.popleft()
            else:
                yn=1
            seen[num01]=True
            for j in range(len(num[num01])):
                num10=num[num01][j]-1
                if seen[num10]==False:
                    seen[num10]=True
                    cnt+=1
                    queue.append(num10)
        ans=max(ans,cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
import collections

n, m = map(int, input().split())
if m == 0:
    print(1)
    exit()
if m == 1:
    print(2)
    exit()
ids = {i: 0 for i in range(1, n+1)}
id = 1
for _ in range(m):
    a, b = map(int, input().split())
    if ids[a] == 0 and ids[b] != 0:
        ids[a] = ids[b]
    elif ids[a] != 0 and ids[b] == 0:
        ids[b] = ids[a]
    elif ids[a] == 0 and ids[b] == 0:
        ids[a] = id
        ids[b] = id
        id += 1
tmp = collections.Counter(ids.values())
if 0 in tmp:
    tmp.pop(0)
print(max(tmp.values()))