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

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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):
        """
        yをxの根に繋ぐ
        """
        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 same(self, x, y):
        """
        xとyが同じ連結成分か判別する
        """
        return self.find(x) == self.find(y)

    def size(self, x):
        """
        xの連結成分の大きさを返す
        """
        return -self.parents[self.find(x)]

    def kruskal(self, edge):
        """
        :param edge: edge = [(コスト, 頂点1, 頂点2),...]の形で重み付き隣接リストを渡して下さい
        :return: 最小全域木のコストの和
        """
        edge.sort()
        cost_sum = 0
        for cost, node1, node2 in edge:
            if not self.same(node1, node2):
                cost_sum += cost
                self.union(node1, node2)
        return cost_sum


def resolve():
    n, m = map(int, input().split())
    uf = UnionFind(n)
    res = 0
    for _ in range(m):
        a, b = map(int, input().split())
        if not uf.same(a - 1, b - 1):
            uf.union(a - 1, b - 1)
            res = max(res, uf.size(a - 1))
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
lis = [i+1 for i in range(n)]
li = [sorted(list(map(int,input().split()))) for i in range(m)]
li.sort()
for i in range(m):
    lis[li[i][1]-1] = lis[li[i][0]-1]
lis.sort()
ans = 0
cnt = 1
for i in range(n-1):
    if lis[i] != lis[i+1]:
        ans = max(ans,cnt)
        cnt = 1
    else:
        cnt += 1
print(max(ans,cnt))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
p=[-1 for i in range(n)]
l=[]
for i in range(m):
    a,b=map(int,input().split())
    if a>b:
        a,b=b,a
    if p[a-1]==-1 and p[b-1]==-1:
        l+=[[a,b]]
        p[a-1],p[b-1]=len(l)-1,len(l)-1
    elif p[b-1]==-1:
        l[p[a-1]]+=[b]
        p[b-1]=p[a-1]
    elif p[a-1]==-1:
        l[p[b-1]]+=[a]
        p[a-1]=p[b-1]
q=0
for i in l:
    k=len(i)
    if k>q:
        q=k
print(q)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
from collections import deque
from collections import Counter
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n,m = readInts()
ab = {}
for i in range(m):
	a,b = readInts()
	if a in ab:
		ab[a].append(b)
	else:
		ab[a] = [b]
	if b in ab:
		ab[b].append(a)
	else:
		ab[b] = [a]

ans = -1
for i in ab:
	ab[i] = set(ab[i])
	ans = max(ans, len(ab[i]))

print(ans+1)

Submission status: Wrong Answer

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

if __name__ == "__main__":
    N, M = map(int, input().split())
    adj = [set() for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        adj[a - 1].add(b - 1)
        adj[b - 1].add(a - 1)
    print(max(len(adj[i]) for i in range(N)) + 1)

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
sys.setrecursionlimit(200000)
class uf:
    def __init__(self,n):
        self.n=n
        self.l=[-1]*n
    def ro(self,n):         #root
        if self.l[n]<0:
            return n
        r=self.ro(self.l[n])
        self.l[n]=r
        return r
    def me(self,a,b):       #merge
        ra=self.ro(a)
        rb=self.ro(b)
        if self.l[ra]>self.l[rb]:
            ra,rb=rb,ra
        if ra!=rb:
            self.l[ra]+=self.l[rb]
            self.l[rb]=ra
    def size(self,n):
        return -self.l[self.ro(n)]
    def sa(self,a,b):       #same
        return self.ro(a)==self.ro(b)
    def rl(self):           #roots list
        return [i for i,v in enumerate(self.l) if v<0]
    
    def len(self):          #len(roots)
        return len(self.rl())
    def ul(self):
        d={n:i for i,n in enumerate(self.rl())}
        m=[[]for i in range(self.len())]
        for i in range(self.n):
            m[d[self.ro(i)]].append(i)
        return m
    def __str__(self):
        return f"{self.ul()}"
    def __setitem__(self,i,v):
        self.l[i]=v
N,M=map(int,input().split())
print(N)
u=uf(N)
for i in range(M):
    A,B=map(lambda x: int(x)-1,input().split())
    u.me(A,B)
a=0
for i in u.ul():
    t=len(i)
    if t>a:
        a=t
print(a)

Submission status: Wrong Answer

Solution code:
class UF:
  def __init__(self,n=0):
    self.vertices=n
    self.mother=[-1 for i in range(self.vertices)]
  
  def root(self,x):
    normalize_v=[]
    while x!=-1:
      normalize_v.append(x)
      y=x
      x=self.mother[x]
    for i in normalize_v[:-1]:
      self.mother[i]=y
    return y
  
  def union(self,x,y):
    root_y=self.root(y)
    if self.root(x)!=root_y:
      self.mother[x]=self.root(y)
  
  def find(self,x,y):
    if self.root(x)==self.root(y):
      return True
    else:
      return False

n,m=map(int,input().split())
uf=UF(n)
for i in range(m):
  q=list(map(lambda x:int(x)-1,input().split()))
  uf.union(q[0],q[1])

l=[0 for i in range(n)]
ml=0
for i in range(n):
  r=uf.root(i)
  l[r]+=1
  ml=max(ml,l[r])

print(ml)

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit
input = stdin.buffer.readline
setrecursionlimit(10 ** 7)

from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict, Counter
from itertools import combinations, permutations, combinations_with_replacement
from itertools import accumulate
from math import ceil, sqrt, pi, radians, sin, cos

MOD = 10 ** 9 + 7
INF = 10 ** 18

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

root = [i for i in range(N + 1)]
size = [1] * (N + 1)
 
def find(x):
  y = root[x]
  if x == y:
    return x
  else:
    z = find(y)
    root[x] = z
    return z

def union(x, y):
  rx = find(x)
  ry = find(y)
  sx = size[rx]
  sy = size[ry]
  if rx == ry:
    return 0
  else:
    if sx >= sy:
      root[ry] = rx
      size[rx] = sx + sy
    else:
      root[rx] = ry
      size[ry] = sx + sy
    return sx * sy

cnt = [0] * (N + 1)

for a, b in AB:
    union(a, b)
for r in root[1:]:
    cnt[r] += 1

print(max(cnt))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, M = map(int, input().split())

class UnionFind():
  def __init__(self, n):
    self.n = n
    self.root = [-1] * (n + 1)
    self.rnk = [0] * (n + 1)

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

  def Unite(self, x, y):
    x = self.Find_Root(x)
    y = self.Find_Root(y)
    if x == y:
      return 
    elif self.rnk[x] > self.rnk[y]:
      self.root[x] += self.root[y]
      self.root[y] = x
    else:
      self.root[y] += self.root[x]
      self.root[x] = y
      if self.rnk[x] == self.rnk[y]:
        self.rnk[y] += 1

  def isSameGroup(self, x, y):
    return self.Find_Root(x) == self.Find_Root(y)

  def Count(self, x):
    return -self.root[self.Find_Root(x)]

uf = UnionFind(N)
res = 0
for _ in range(M):
  u, v = map(int, input().split())
  uf.Unite(u, v)
  res = max(res, uf.Count(u))
print(res)

Submission status: Wrong Answer

Solution code:
import sys, math
import io, os
#data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
# from itertools import permutations,combinations
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var): sys.stdout.write(' '.join(map(str, var)) + '\n')
def out(var): sys.stdout.write(str(var) + '\n')
from decimal import Decimal
# from fractions import Fraction
# sys.setrecursionlimit(100000)
INF = float('inf')
mod = int(1e9) + 7


n,m=mdata()
Group = [i for i in range(n + 1)]
Nodes = [1] * (n + 1)

def find(x):
    while Group[x] != x:
        x = Group[x]
    return x

def Union(x, y):
    if find(x) != find(y):

        if Nodes[find(x)] < Nodes[find(y)]:
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)

        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

for i in range(m):
    x, y = mdata()
    Union(x, y)
d=dd(int)
for i in range(n):
    d[find(i)]+=1
out(max(d.values()))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
e = [set() for _ in range(N + 1)]
for _ in range(M):
  u, v = map(int, input().split())
  e[u].add(v)
  e[v].add(u)
res = 0
for i in range(1, N + 1): res = max(res, len(e[i]))
print(res + 1)

Submission status: Time Limit Exceeded

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


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

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

ans = 0
for l in list(uf.all_group_members().values()):
    ans = max(ans, len(l))

print(ans)

Submission status: Time Limit Exceeded

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

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

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

ans = 0
for l in list(uf.all_group_members().values()):
    ans = max(ans, len(l))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
class UnionFind:
  def __init__(self, n):
    self.parents=[i for i in range(n)]
    self.rank=[0]*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 unite(self, x, y):
    x=self.find(x)
    y=self.find(y)
    if x!=y:
      if self.rank[x]<self.rank[y]:
        self.parents[x]=y
      else:
        self.parents[y]=x
        if self.rank[x]==self.rank[y]:
          self.rank[x]+=1

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

N,M=map(int,input().split())
uf=UnionFind(N)
for _ in range(M):
  a,b=map(int,input().split())
  uf.unite(a-1,b-1)
par=uf.parents
ans=0
parset=set(par)
for p in parset:
  s=par.count(p)
  ans=max(ans,s)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
class UnionFind:
  def __init__(self, n):
    self.parents=[i for i in range(n)]
    self.rank=[0]*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 unite(self, x, y):
    x=self.find(x)
    y=self.find(y)
    if x!=y:
      if self.rank[x]<self.rank[y]:
        self.parents[x]=y
      else:
        self.parents[y]=x
        if self.rank[x]==self.rank[y]:
          self.rank[x]+=1

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

N,M=map(int,input().split())
uf=UnionFind(N)
for _ in range(M):
  a,b=map(int,input().split())
  uf.unite(a-1,b-1)
par=uf.parents
ans=0
for p in par:
  s=par.count(p)
  ans=max(ans,s)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M = (int(d) for d in input().split())

C = [[] for i in range(N+1)]
for i in range(M):
    A, B = (int(d) for d in input().split())
    C[A].append(B)
    C[B].append(A)

# BFS
Q = []
ungrouped = [i for i in range(1, N+1)]
G = []

while len(ungrouped) > 0:
    init = ungrouped.pop(0)
    Q += C[init]
    G.append([])
    G[-1].append(init)

    while len(Q) > 0:
        I = Q.pop(0)
        if I in ungrouped:
            Q += C[I]
            G[-1].append(I)
            ungrouped.remove(I)

print(max([len(g) for g in G]))

Submission status: Time Limit Exceeded

Solution code:
N, M = list(map(int, input().split()))
A = [0] * M
B = [0] * M
R = [0] * N
for i in range(N):
    R[i] = set()

for i in range(M):
    A[i], B[i] = list(map(int, input().split()))
    R[A[i]-1].add(B[i]-1)
    R[B[i]-1].add(A[i]-1)

stack = []
visited = set()
max_groups = 0

for i in range(N):
    if not i in visited:
        stack.append(i)
        groups = 0
        while True:
            current = stack.pop()
            visited.add(current)
            groups += 1
            stack.extend(list(R[current] - visited - set(stack)))
            if not stack:
                break
        max_groups = max(max_groups,groups)

print(max_groups)

Submission status: Time Limit Exceeded

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 main():
    n, m = map(int, input().split())
    uf = UnionFind(n)

    for _ in range(m):
        a, b = map(int, input().split())
        uf.union(a-1, b-1)
    
    print(len(max(uf.all_group_members().values(), key=lambda x: len(x))))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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


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

uf = UnionFind(n+1)
for _ in range(m):
    a, b = map(int, input().split())
    uf.union(a, b)

ans = 0
for k, v in uf.all_group_members().items():
    ans = max(ans, uf.size(k))

print(ans)

Submission status: Time Limit Exceeded

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


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

uf = UnionFind(n+1)
for _ in range(m):
    a, b = map(int, input().split())
    uf.union(a, b)

ans = 0
for v in uf.all_group_members().values():
    ans = max(ans, len(v))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#template
def inputlist(): return [int(j) for j in input().split()]
#template

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 all_group_members(self):
        return {r: self.members(r) for r in self.roots()}


N,M = inputlist()
uf = UnionFind(N+1)
for i in range(M):
    A,B = inputlist()
    uf.union(A,B)

ans = 0
lis = uf.all_group_members().values()
for i in lis:
    ans = max(ans,len(i))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#template
def inputlist(): return [int(j) for j in input().split()]
#template

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 all_group_members(self):
        return {r: self.members(r) for r in self.roots()}


N,M = inputlist()
uf = UnionFind(N+1)
for i in range(M):
    A,B = inputlist()
    uf.union(A,B)

ans = 0
lis = uf.all_group_members()
key = uf.roots()
for i in key:
    ans = max(ans,len(list(lis[i])))
print(ans)

Submission status: Time Limit Exceeded

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

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

Flag = [0]*(N+1)

def count_num(i):
    Flag[i] = 1
    member = [i]
    new_i = 0
    tmp_n = 0
    while tmp_n < len(member):
        tmp_n = len(member)
        for i in range(new_i,len(member)):
            i_ = member[i]
            for j in range(len(F[i_])):
                if (F[i_][j] in member)==False:
                    member.append(F[i_][j])
                    Flag[F[i_][j]]=1
        new_i = tmp_n
    #print(member)
    return len(member)

ans = 1  
for i in range(1,N+1):
    if Flag[i] == 0:
        c = count_num(i)
        if ans < c:
            ans =c

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import *
N,M=map(int,input().split())
AB=[list(map(int,input().split())) for i in range(M)]

lst=[[] for i in range(N+1)]

for a,b in AB:
    lst[a].append(b)
    lst[b].append(a)



used=set(list(range(1,N+1)))
value=-10**18
while used:
    a=used.pop()
    stack=[a]

    visited={a}
    while stack:
        a=stack.pop()
        for p in lst[a]:
            if not p in visited:
                visited.add(p)
                stack.append(p)
    value=max(len(visited),value)
print(value)

Submission status: Time Limit Exceeded

Solution code:
N,M = (int(x) for x in input().split())
s = [{i} for i in range(1,N+1)]
for i in range(M):
  a,b = (int(x) for x in input().split())
  for j in range(len(s)):
    if {a,b} & s[j] :
      s[j] = {a,b}|s[j]
#print(s)
s_len = [len(x) for x in s]
#print(s_len)
print(max(s_len))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
V=[]
for _ in range(M):
    s=set(map(int,input().split()))
    f=True
    for v in V:
        if not v.isdisjoint(s):
            f=False
            v.update(s)
            break
    if f:
        V.append(s)
if M == 0:
    print(1)
else:
    print(max(map(len,V)))

Submission status: Time Limit Exceeded

Solution code:
N,M=map(int,input().split())
V=[]
for _ in range(M):
    s=set(map(int,input().split()))
    f=True
    for v in V:
        l=len(s&v)
        if l>0:
            f=False
            if l==1:
                v.update(s)
            break
    if f:
        V.append(s)
if M == 0:
    print(1)
else:
    print(max(map(len,V)))

Submission status: Time Limit Exceeded

Solution code:
# coding:UTF-8

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

if __name__ == '__main__':
    # ------ 入力 ------#
    # 1行入力
    n, m = list(map(int, input().split()))     # スペース区切り連続数字

    # 定数行入力
    x = m
    abList = [list(map(int, input().split())) for _ in range(x)]     # スペース区切り連続数字(行列)

    # ------ 処理 ------#
    uf = UnionFind(n)
    for i in range(m):
        uf.union(abList[i][0]-1, abList[i][1]-1)

    out = max(len(uf.all_group_members()[l]) for l in uf.all_group_members().keys())
    # ------ 出力 ------#
    print("{}".format(out))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
friend = [[0]*n for i in range(n)]
for i in range(m):
    a, b = map(int, input().split())
    friend[a-1][b-1] = 1
    friend[b-1][a-1] = 1
s = 0
for i in range(n):
    s = max(s, sum(friend[i]))
print(s+1)

Submission status: Time Limit Exceeded

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

N, M = map(int, input().split())
ur = UnionFind(N)
for _ in range(M):
  ab = list(map(int, input().split()))
  ur.union(ab[0]-1, ab[1]-1)

members = ur.all_group_members()
max_n = 0
for v in members.values():
  if len(v) > max_n:
    max_n = len(v)
print(max_n)

Submission status: Time Limit Exceeded

Solution code:
import collections

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

q = collections.deque()
visited = [0 for i in range(n)]

ans = 0
for i in range(n):
    tmp = 0
    if visited[i] == 0:
        visited[i] = 1
        tmp += 1
        for j in friend[i]:
            if visited[j] == 0:
                visited[j] = 1
                tmp += 1
                q.append(j)
        while q:
            s = q.pop()
            for k in friend[s]:
                if visited[k] == 0:
                    visited[k] = 1
                    tmp += 1
                    q.append(k)
    ans = max(ans,tmp)

print(max(ans,1))

Submission status: Time Limit Exceeded

Solution code:
import collections

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

q = collections.deque()
visited = [0 for i in range(n)]

ans = 0
for i in range(n):
    if visited[i] == 0:
        visited[i] = 1
        tmp = 1
        for j in friend[i]:
            if visited[j] == 0:
                visited[j] = 1
                tmp += 1
                q.append(j)
        while q:
            s = q.pop()
            for k in friend[s]:
                if visited[k] == 0:
                    tmp += 1
                    q.append(k)
    ans = max(ans,tmp)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

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


q = collections.deque()
visited = [0 for i in range(n)]

ans = 0
for i in range(n):
    if visited[i] == 0:
        visited[i] = 1
        tmp = 1
        for j in friend[i]:
            if visited[j] == 0:
                visited[j] = 1
                tmp += 1
                q.append(j)
        while q:
            s = q.pop()
            for k in friend[s]:
                if visited[k] == 0:
                    tmp += 1
                    q.append(k)
    ans = max(ans,tmp)

print(ans)

Submission status: Time Limit Exceeded

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

for _ in range(M):
  a, b = map(int,input().split())
  D[a-1].add(b-1)
  D[b-1].add(a-1)

Q = []
ans = 0
S = set()
for i in range(N):
  if i in S:continue
  vstd = set()
  Q.append(i)
  while Q:
    a = Q.pop()
    S.add(a)
    vstd.add(a)
    for j in D[a]:
      if j in S:continue
      if not j in vstd:
        Q.append(j)
  ans = max(ans,len(vstd))
print (ans)