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 sys
from io import StringIO
import unittest

def resolve():
    N, M = map(int, input().split())
    l = []
    for _ in range(M):
        i,j = map(int, input().split())
        gi = find_group(l,i)
        if not gi:
            gi = {i}
            l.append(gi)
        gj = find_group(l, j)
        if not gj:
            gj={j}
            l.append(gj)
        l.remove(gi)
        if gi!=gj:
            l.remove(gj)
        gi = gi|gj
        l.append(gi)
    max_size=0
    for s in l:
        max_size = max(max_size,len(s))
    print(max_size)


def find_group(l, i):
    for s in l:
        if i in s:
            return s
    return None

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """5 3
1 2
3 4
5 1"""
        output = """3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """4 10
1 2
2 1
1 2
2 1
1 2
1 3
1 4
2 3
2 4
3 4"""
        output = """4"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """10 4
3 1
4 1
5 9
2 6"""
        output = """3"""
        self.assertIO(input, output)


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
u=[i for i in range(n+1)]
for i in range(m):
	a,b=map(int,input().split())
	t=min(u[a],u[b])
	u[a]=t
	u[b]=t
s=set(u)
print(max([u.count(i) for i in s]))

Submission status: Wrong Answer

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

v = {i:i for i in range(1, n + 1)}

for _ in range(m):
    a, b = map(int, input().split())
    if v[a] > v[b]:
        v[a] = v[b]
    elif v[a] < v[b]:
        v[b] = v[a]

g = [0] * n

for i in v.values():
    g[i - 1] += 1

print(max(g))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub


import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()

def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


class UnionFind():
    def __init__(self):
        self.__table = {}
        self.__size = defaultdict(lambda: 1)
        self.__rank = defaultdict(lambda: 1)

    def __root(self, x):
        if x not in self.__table:
            self.__table[x] = x
        elif x != self.__table[x]:
            self.__table[x] = self.__root(self.__table[x])
        return self.__table[x]

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

    def union(self, x, y):
        x = self.__root(x)
        y = self.__root(y)
        if x == y:
            return False

        if self.__rank[x] < self.__rank[y]:
            self.__table[x] = y
            self.__size[y] += self.__size[x]
        else:
            self.__table[y] = x
            self.__size[x] += self.__size[y]
            if self.__rank[x] == self.__rank[y]:
                self.__rank[x] += 1
        return True

    def size(self, x):
        return self.__size[self.__root(x)]

    def num_of_group(self):
        g = 0
        for k, v in self.__table.items():
            if k == v:
                g += 1
        return g

@mt
def slv(N, M, AB):
    uf = UnionFind()
    p = set()
    for a, b in AB:
        uf.union(a, b)
        p.add(a)
        p.add(b)

    ans = 0
    for c in p:
        ans = max(ans, uf.size(c))
    return ans


def main():
    N, M = read_int_n()
    AB = [read_int_n() for _ in range(M)]
    print(slv(N, M, AB))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
F = [set() for j in range(N)]
for m in range(M):
  a, b = map(int, input().split())
  F[a - 1].add(b)
  F[b - 1].add(a)
  F[a - 1] = F[b - 1].union(F[a - 1])
  F[b - 1] = F[a - 1].union(F[b - 1])
ans = 0
for i in range(len(F)):
  ans = max(ans, len(F[i])) 
print(ans)

Submission status: Wrong Answer

Solution code:
from statistics import mode

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

rels = [i for i in range(n)]
inf = []
count = [0] * n

for i in range(m):
    rel = list(map(int, input().split()))
    rel = sorted(rel)
    inf.append(rel)

inf = sorted(inf)

for rel in inf:
    a = rel[0] - 1
    b = rel[1] - 1
    rels[b] = rels[a]

"""
for rel in rels:
    count[rel - 1] = count[rel - 1] + 1

res = rels.count(mode(rels))

while res < sum(count) - res:
    res = res + 1
    """

print(rels.count(mode(rels)))

Submission status: Wrong Answer

Solution code:
from statistics import mode

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

rels = [i for i in range(n + 1)]
inf = []
count = [0] * n

for i in range(m):
    rel = list(map(int, input().split()))
    rel = sorted(rel)
    inf.append(rel)

inf = sorted(inf)

for rel in inf:
    a = rel[0] - 1
    b = rel[1] - 1
    rels[b] = rels[a]

for rel in rels:
    count[rel - 1] = count[rel - 1] + 1

res = rels.count(mode(rels))

while res < sum(count) - res:
    res = res + 1

print(res)

Submission status: Wrong Answer

Solution code:
from statistics import mode

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

rels = [i for i in range(n + 1)]
inf = []

for i in range(m):
    rel = list(map(int, input().split()))
    rel = sorted(rel)
    inf.append(rel)

inf = sorted(inf)

for rel in inf:
    a = rel[0]
    b = rel[1]
    rels[b] = rels[a]

#print(rels)
    
print(rels.count(mode(rels)))

Submission status: Wrong Answer

Solution code:
from statistics import mode

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

rels = [i for i in range(n + 1)]
inf = []

for i in range(m):
    rel = list(map(int, input().split()))
    rel = sorted(rel)
    inf.append(rel)

inf = sorted(inf)

for rel in inf:
    a = rel[0]
    b = rel[1]
    rels[b] = rels[a]

print(rels)
    
print(rels.count(mode(rels)))

Submission status: Wrong Answer

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

for i in range(m):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    
    f[a].append(b)
    f[b].append(a)
    
import collections
q = collections.deque()

for i in range(n):
    for j in f[i]:
        q.append(j)
        
    while q:
        p = q.popleft()
        
        if p != i:
            f[i] += f[p]

            for k in f[p]:
                q.append(k)

            f[p] = []
        
    f[i] = sorted(set(f[i]))
            
ans = 0

for i in f:
    res = len(i)
    ans = max(ans,res)
    
print(ans)

Submission status: Wrong Answer

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

for i in range(m):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    
    if max(a,b) not in set(f[min(a,b)]):
        f[min(a,b)].append(max(a,b))
    
import collections
q = collections.deque()

for i in range(n):
    for j in f[i]:
        q.append(j)
        
    while q:
        p = q.popleft()
        
        f[i] += f[p]
            
        for k in f[p]:
            q.append(k)
            
        f[p] = []
        
    f[i] = sorted(set(f[i]))
            
ans = 0

for i in f:
    res = len(i) + 1
    ans = max(ans,res)
    
print(ans)

Submission status: Wrong Answer

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

for i in range(m):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    
    if max(a,b) not in f[min(a,b)]:
        f[min(a,b)].append(max(a,b))
    
import collections
q = collections.deque()

for i in range(n):
    for j in f[i]:
        q.append(j)
        
    while q:
        p = q.popleft()
        
        f[i] += f[p]
            
        for k in f[p]:
            q.append(k)
            
        f[p] = []
        
    f[i] = sorted(set(f[i]))
            
ans = 0

for i in f:
    res = len(i) + 1
    ans = max(ans,res)
    
print(ans)

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

from collections import Counter
N,M=map(int,input().split())
ufbs=UnionFind(N)
for _ in range(M):
    A,B=map(int,input().split())
    ufbs.union(A-1,B-1)
ans=0
for x in ufbs.roots():
    ans=max(ans,x)
print(ans)

Submission status: Wrong Answer

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

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

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

        if x == y:
            return

        if self.size[x] < self.size[y]:
            self.size[y] += self.size[x]
            self.parents[x] = y
        else:
            self.size[x] += self.size[y]
            self.parents[y] = x

from collections import Counter
N,M=map(int,input().split())
ufbs=UnionFindBySize(N)
for _ in range(M):
    A,B=map(int,input().split())
    ufbs.union(A-1,B-1)
ans=Counter(ufbs.parents)
print(max(ans.values()))

Submission status: Wrong Answer

Solution code:
from statistics import mode

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

rels = [i for i in range(n)]
#count = [0] * n
#print(count)

for i in range(m):
    rel = list(map(int, input().split()))
    rel = sorted(rel)
    a = rel[0] - 1
    b = rel[1] - 1
    rels[b] = rels[a]
    #count[a] = count[a] + 1
    #print(count)
    
print(rels.count(mode(rels)))

Submission status: Wrong Answer

Solution code:
from collections import deque
from numba import jit
 
N, M = map(int, input().split())
S = [set() for i in range(N)]
visited = [-1] * N
colorcount = [-1] * N
 
for i in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    S[a].add(b)
    S[b].add(a)

@jit
def solve():
    q = deque()
    for i in range(N):
        q.append(i)
        color = i
        cnt = 1
        while len(q):
            nt = q.popleft()
            if visited[nt] != -1:
                continue
            visited[nt] = color
            q.extend(S[nt])
            cnt += 1
        colorcount[color] = cnt - 1
 
print(max(colorcount))

Submission status: Wrong Answer

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

rels = [i for i in range(n)]

for i in range(m):
    rel = list(map(int, input().split()))
    rel = sorted(rel)
    a = rel[0] - 1
    b = rel[1] - 1
    rels[b] = rels[a]

els = set(rels)

enm = 0

for e in els:
    en = rels.count(e)
    if enm < en:
        enm = en

print(enm)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,m = map(int,input().split())
graph = [[] for i in range(n)]
for i in range(m):
  a,b = map(lambda z:int(z)-1,input().split())
  graph[a].append(b)
  graph[b].append(a)

size = [0 for i in range(n)]
for i in range(n):
  if not size[i]:
    tmp = 1
    stack = deque([i])
    while stack:
      now = stack.popleft()
      size[now] = tmp
      tmp+=1
      for to in graph[now]:
        if not size[to]:
          stack.append(to)

print(max(size))

Submission status: Wrong Answer

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

size = [0 for i in range(n)]
check = [True for i in range(n)]
for i in range(n):
  if check[i]:
    tmp = 1
    stack = [i]
    while stack:
      now = stack.pop()
      size[now] = tmp
      tmp+=1
      check[now] = False
      for to in graph[now]:
        if check[to]:
          stack.append(to)

print(max(size))

Submission status: Wrong Answer

Solution code:
import sys,os
import math
from fractions import Fraction 
from collections import defaultdict
from random import randint

# sys.stderr=open('err.txt','w')
# sys.stdout=open('output.txt','w')
# sys.stdin=open('input.txt','r')

def linput():
    return list(minput())

def minput():
    return map(int, sys.stdin.readline().strip().split())

###############################################################

def make_set(a):
    parent[a]=a
    sizen[a]=1

def find_set(a):
    if parent[a]==a:
        return a
    parent[a]=find_set(parent[a])
    return parent[a]

def union_set(a,b):
    x=find_set(a)
    y=find_set(b)
    if x==y:
        return
    else:
        if sizen[x]>sizen[y]:
            parent[y]=x
            sizen[x]+=1
        else:
            parent[x]=y
            sizen[y]+=1

n,m=minput()
parent=[0]*n
sizen=[0]*n
for i in range(n):
    make_set(i)
ans=0
for i in range(m):
    x,y=minput()
    if x>y:
        x,y=y,x
    x=x-1
    y=y-1
    union_set(x,y)
# print(parent)
di = defaultdict(int)
# for i in parent:
#     di[i]+=1
# print(di)

for v in sizen:
    ans=max(ans,v)
print(ans)

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)

print(max(len(set(i)) for i in graph)+1)

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
f = [[] for i in range(N)]
for i in range(M):
    A,B = map(int,input().split())
    f[A-1].append(B-1)
    f[B-1].append(A-1)
from collections import deque
d = deque()
x = [-1]*N
for i in range(N):
    d.append(f[i])
    while len(d) > 0:
        z = d.popleft()
        for j in z:
            if x[j] == -1:
                x[j] = i
                d.append(f[j])
import collections
ans = collections.Counter(x)
print(ans.most_common()[0][1])

Submission status: Wrong Answer

Solution code:
from collections import Counter

class UnionFind:
    def __init__(self,num:int):
        self.r = [-1 for i in range(num)]
    def root(self,x:int):
        if(self.r[x] < 0):
            return x
        self.r[x] = self.root(self.r[x])
        return self.r[x]
    def unite(self,x:int,y:int):
        rx = self.root(x)
        ry = self.root(y)
        if(rx == ry):
            return False
        if(self.r[rx] > self.r[ry]):
            rx,ry = ry,rx
        self.r[rx] += self.r[ry]
        self.r[ry] = rx
        return True
    def size(self,x:int):
        return -self.r[self.root(x)]

N,M = map(int,input().split())
group = UnionFind(N)
for i in range(M):
    A,B = map(int,input().split())
    group.unite(A-1,B-1)
ans = 0
for i in range(N):
    ans = max(ans,group.size(i))
print(group.r)
print(ans)

Submission status: Wrong Answer

Solution code:
'''
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineerin College

'''
import sys
from collections import deque,defaultdict as dd 
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]



def solve():
    
    
    
    # for _ in range(ii()):
    
    
    n,m = mi()
    
    par = [i for i in range(n+1)]
    sz = [1]*(n+1)
    
    def find(i):
        if i == par[i]:
            return par[i]
        par[i] = find(par[i])
        return par[i]
    
    def union(u,v):
        u = find(u)
        v = find(v)
        if u!= v:
            if sz[u]>sz[v]:
                par[v] = u
                sz[u]+=1
            else:
                par[u] = v
                sz[v] += 1
    
    
    for i in range(m):
        x,y = mi()
        union(x,y)
    print(max(sz))
    
    
if __name__ =="__main__":
    # read()
    solve()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

# スペース区切りの整数の入力
n, m = map(int, input().split())

group_list = []

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

  a_group_index = -1
  b_group_index = -1

  index = 0
  for group in group_list:

    if a in group:
      a_group_index = index

    if b in group:
      b_group_index = index

    index += 1

    if a_group_index != -1 and b_group_index != -1:
      break

  if a_group_index == -1 and b_group_index == -1:
    # print("a")
    group_list.append([a, b])
    # group_list.append({b})
  elif a_group_index != -1 and b_group_index == -1:
    # print("b")
    group_list[a_group_index].append(b)
  elif a_group_index == -1 and b_group_index != -1:
    # print("c")
    group_list[b_group_index].append(a)
  else:
    # print("d")
    if(a_group_index != b_group_index):
      group_list[a_group_index].append(group_list[b_group_index])
      del group_list[b_group_index]

  # print(group_list)

size = 0
for ggg in group_list:
  if size < len(ggg):
    size = len(ggg)

print(size)

Submission status: Wrong Answer

Solution code:
# D
from collections import Counter
n, m = map(int, input().split())
ab = [list(map(int, input().split()))for _ in range(m)]
for i in ab:
    if i[0] > i[1]:
        i[0], i[1] = i[1], i[0]
ab.sort(key=lambda x: x[1])
ab.sort(key=lambda x: x[0])
print(ab)
people = [0]*n
for i in ab:
    if people[i[0]-1] == 0:
        people[i[0]-1] = i[0]
    people[i[1]-1] = people[i[0]-1]
print(people)
# Counter.(list).most_common()
people = Counter(people).most_common()
print(people[0][1])

Submission status: Wrong Answer

Solution code:
# D
from collections import Counter
n, m = map(int, input().split())
ab = [list(map(int, input().split()))for _ in range(m)]
for i in ab:
    if i[0]>i[1]:
        i[0],i[1]=i[1],i[0]
ab.sort(key=lambda x:x[1])
ab.sort(key=lambda x:x[0])
# print(ab)
people=[0]*n
for i in ab:
    if people[i[0]-1]==0:
        people[i[0]-1]=i[0]
    if people[i[1]-1]==0:
        people[i[1]-1]=people[i[0]-1]
# print(people)
people=Counter(people).most_common()
print(people[0][1])

Submission status: Wrong Answer

Solution code:
# D
from collections import Counter
n, m = map(int, input().split())
ab = [list(map(int, input().split()))for _ in range(m)]
for i in ab:
    if i[0]>i[1]:
        i[0],i[1]=i[1],i[0]
ab.sort(key=lambda x:x[1])
ab.sort(key=lambda x:x[0])
print(ab)
people=[0]*n
for i in ab:
    if people[i[0]-1]==0:
        people[i[0]-1]=i[0]
    if people[i[1]-1]==0:
        people[i[1]-1]=people[i[0]-1]
print(people)
people=Counter(people).most_common()
print(people[0][1])

Submission status: Wrong Answer

Solution code:
# D
from collections import Counter
n, m = map(int, input().split())
ab = [list(map(int, input().split()))for _ in range(m)]
ab.sort(key=lambda x:x[1])
ab.sort(key=lambda x:x[0])
# print(ab)
people=[0]*n
for i in ab:
    if people[i[0]-1]==0:
        people[i[0]-1]=i[0]
    if people[i[1]-1]==0:
        people[i[1]-1]=people[i[0]-1]
# print(people)
people=Counter(people).most_common()
print(people[0][1])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
N, M = map(int, input().split())
# N, M < 2*10^5
AB = []
for i in range(M):
    A, B = map(int, input().split())
    if B < A:
        A, B = B, A
    AB.append((A-1, B-1))

def solve(N, M, AB):
    parent = []
    for i in range(N):
        parent.append([i, 1])

    def root(n):
        if parent[n][0] == n:
            return parent[n]
        parent[n] = root(parent[n][0])
        return parent[n]

    for a, b in AB:
        Ra, Ca = root(a)
        Rb, Cb = root(b)
        if Ra != Rb:
            parent[a] = (Ra, Ca+Cb)
            parent[b] = (Ra, Ca+Cb)

    C = 0
    for i in range(N):
        r, c = parent[i]
        C = max(C, c)
    print(C)
solve(N, M, AB)

Submission status: Wrong Answer

Solution code:
import sys

# D - Friends
def append_(A, B):
  global people

  people.setdefault(A, [])
  people[A].append(B)


def create_new_group(p):
  global groups

  group = set()
  group.add(p)

  groups.append(group)


def exists_friend(i, group):
  global friends

  for p in group:
    if i in friends[p]:
      return True


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

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

friends = dict()

for i in range(1, N + 1):
  friends[i] = set()

for k in people:
  for v in people[k]:
    friends[k].add(v)

    for f in people[v]:
      friends[k].add(f)

ans = 0

for v in friends.values():
  ans = max(ans, len(v))

print(ans)

Submission status: Wrong Answer

Solution code:
import collections

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

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

def find(x):
  if par[x] == x:
    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 same(x,y) == False:
    par[x] = y

for i in range(m):
  a,b = map(int, input().split())
  unite(a-1, b-1)
  
c = collections.Counter(par)  
print(c.most_common()[0][1])

Submission status: Wrong Answer

Solution code:
class UnionFindTree():
    def __init__(self, num_leaves):
        #self.num_leaves = num_leaves
        self.root = [i for i in range(num_leaves+1)]
        
    def find(self, leaf):
        if self.root[leaf] == leaf:
            return self.root[leaf]
        else:
            self.root[leaf] = self.find(self.root[leaf])
            return self.root[leaf]
        
    def union(self, leaf_a, leaf_b):
      	# find roots
        root_a = self.find(leaf_a)
        root_b = self.find(leaf_b)
        if root_a != root_b:
            # then union roots
            self.root[leaf_b] = root_a
            
        
N, M = map(int, input().split())
uft = UnionFindTree(N)
for m in range(M):
  a, b = map(int, input().split())
  uft.union(a, b)
  
max_size = max([[uft.find(n) for n in range(N)].count(n) for n in range(N+1)])
print(max_size)

Submission status: Wrong Answer

Solution code:
class UnionFindTree():
    def __init__(self, num_leaves):
        #self.num_leaves = num_leaves
        self.root = [i for i in range(num_leaves+1)]
        
    def find(self, leaf):
        if self.root[leaf] == leaf:
            return self.root[leaf]
        else:
            self.root[leaf] = self.find(self.root[leaf])
            return self.root[leaf]
        
    def union(self, leaf_a, leaf_b):
      	# find roots
        root_a = self.find(leaf_a)
        root_b = self.find(leaf_b)
        if root_a != root_b:
            # then union roots
            self.root[leaf_b] = root_a
            
        
N, M = map(int, input().split())
uft = UnionFindTree(N)
for m in range(M):
  a, b = map(int, input().split())
  uft.union(a, b)
  
# 経路圧縮
for n in range(N):
  uft.find(n)
  
max_size = max([uft.root.count(n) for n in range(N+1)])
print(max_size)

Submission status: Wrong Answer

Solution code:
from collections import deque

def bfs(fd, n):
    visited = set()
    ans = 0
    for i in range(1, n + 1):
        if i in visited: continue
        volume = 1
        queue = deque(list(fd[i]))
        visited.add(i)
        while queue:
            x = queue.popleft()
            if x in visited: continue
            visited.add(x)
            volume += 1
            queue.append(x)
        ans = max(ans, volume)
    return ans

if __name__ == "__main__":
    n, m = map(int, input().split())
    fd = {i+1:set() for i in range(n)}
    for _ in range(m):
        a, b = map(int, input().split())
        fd[a].add(b)
        fd[b].add(a)
    print(bfs(fd, n))

Submission status: Wrong Answer

Solution code:
class UnionFindTree():
    def __init__(self, num_leaves):
        #self.num_leaves = num_leaves
        self.root = [i for i in range(num_leaves+1)]
        
    def find(self, leaf):
        if self.root[leaf] == leaf:
            return self.root[leaf]
        else:
            self.root[leaf] = self.find(self.root[leaf])
            return self.root[leaf]
        
    def union(self, leaf_a, leaf_b):
      	# find roots
        root_a = self.find(leaf_a)
        root_b = self.find(leaf_b)
        if root_a != root_b:
            # then union roots
            self.root[leaf_b] = root_a
            
        
N, M = map(int, input().split())
uft = UnionFindTree(N)
for m in range(M):
  a, b = map(int, input().split())
  uft.union(a, b)
  
# 経路圧縮
for n in range(N):
  uft.find(n)
  
num_group = len(set(uft.root))-1
print(num_group)

Submission status: Wrong Answer

Solution code:
import collections

n, m = map(int, input().split())
a = []
b = []
for _ in range(m):
    A, B = map(int, input().split())
    a.append(A)
    b.append(B)
friend = [0] * n
num = 0
for i in range(m):
    if friend[a[i]-1] == 0 and friend[b[i]-1] == 0:
        num +=1
        friend[a[i]-1] = num
        friend[b[i]-1] = num
    elif friend[b[i]-1] == 0:
        friend[b[i]-1] = friend[a[i]-1]
    elif friend[a[i]-1] == 0:
        friend[a[i]-1] = friend[b[i]-1]
counted = collections.Counter(friend)

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

Submission status: Wrong Answer

Solution code:
import collections

n, m = map(int, input().split())
a = []
b = []
for _ in range(m):
    A, B = map(int, input().split())
    a.append(A)
    b.append(B)
friend = [0] * n
num = 0
for i in range(m):
    if friend[a[i]-1] == 0 and friend[b[i]-1] == 0:
        num +=1
        friend[a[i]-1] = num
        friend[b[i]-1] = num
    elif friend[b[i]-1] == 0:
        friend[b[i]-1] = friend[a[i]-1]
    else:
        friend[a[i]-1] = friend[b[i]-1]
counted = collections.Counter(friend)
#最頻値が同値である場合全て出力したい
print(counted.most_common()[0][1])
#頻出数ではなく、文字だけ出力したい

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        self.sum = [1] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            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 x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.sum[y] += self.sum[x]
        else:
            self.par[y] = x
            self.sum[x] += self.sum[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)


N, M = map(int,input().split())
A = [0] * M
B = [0] * M

uni = UnionFind(N)

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

print(uni.par)
print(uni.rank)
print(uni.sum)

print(max(uni.sum))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
group = []
for _ in range(M):
	AB = list(map(int, input().split()))
	next = []
	temp = set()
	for g in group:
		f = True
		for ab in AB[:]:
			if ab in g:
				temp = temp | g
				AB.remove(ab)
				f = False
		if f:
			next.append(g)
	temp = temp | set(AB)
	next.append(temp)
	group = next
max = 0
for g in group:
	if len(g) > max:
		max = len(g)
print(max)