Find the Largest Component SizeMedium

Find the Largest Component Size

MediumPractice library · +20 XPgraphsbfsconnected components

Given an undirected graph with N nodes and M edges, find the size of the largest connected component. The graph is guaranteed to be connected.

A connected component is a subgraph where any two nodes are connected to each other by paths, and which is connected to no additional nodes in the supergraph. The size of a connected component is the number of nodes it contains.

Input

The first line contains two integers N and M (1 <= N <= 200000, 0 <= M <= 200000). The following M lines each contain two integers u and v, representing an undirected edge between node u and node v (1 <= u, v <= N). Nodes are 1-indexed.

Output

Print a single integer: the number of nodes in the largest connected component.

Constraints

1 <= N <= 200000 0 <= M <= 200000 1 <= u, v <= N The graph is guaranteed to be connected.

Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests

Example 1

Input
5 3
1 2
2 3
4 5
Output
3

Components are {1, 2, 3} (size 3) and {4, 5} (size 2). Largest is 3.

Example 2

Input
7 5
1 2
2 3
4 5
5 6
6 7
Output
4

Components are {1, 2, 3} (size 3) and {4, 5, 6, 7} (size 4). Largest is 4.

Hints