#C7947. Connected Components in an Undirected Graph

    ID: 51874 Type: Default 1000ms 256MiB

Connected Components in an Undirected Graph

Connected Components in an Undirected Graph

You are given an undirected graph with \( n \) vertices and \( m \) edges. The vertices are numbered from 1 to \( n \). Two vertices are connected if there exists a path between them. Your task is to compute the number of connected components \( C \) and the size \( L \) of the largest connected component.

If the graph consists of only one connected component, output just \( C \) (which will be 1). Otherwise, output two numbers: \( C \) and \( L \), separated by a space.

The connectivity can be described by the following formulas:

  • \( C = \text{number of connected components} \)
  • \( L = \text{size of the largest component} \)

This is a typical graph traversal problem that can be solved using either breadth-first search (BFS) or depth-first search (DFS).

inputFormat

The input is read from standard input (stdin). The first line contains an integer ( n ) representing the number of vertices. The second line contains an integer ( m ) representing the number of edges. Each of the following ( m ) lines contains two integers ( u ) and ( v ), indicating that there is an undirected edge connecting vertex ( u ) and vertex ( v ).

outputFormat

Output to standard output (stdout). If there is only one connected component, output a single integer ( C ). Otherwise, output two integers ( C ) and ( L ) separated by a space.## sample

6
4
1 2
2 3
4 5
5 6
2 3