Find the Largest Component Size
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.
Example 1
5 3 1 2 2 3 4 5
3
Components are {1, 2, 3} (size 3) and {4, 5} (size 2). Largest is 3.
Example 2
7 5 1 2 2 3 4 5 5 6 6 7
4
Components are {1, 2, 3} (size 3) and {4, 5, 6, 7} (size 4). Largest is 4.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
5 3 1 2 2 3 4 5
7 5 1 2 2 3 4 5 5 6 6 7