Count Connected Nodes
Given an undirected graph with N nodes and M edges, determine the size of the connected component that node 1 belongs to. The graph nodes are numbered from 1 to N.
For example, if node 1 is connected to nodes 2 and 3, and node 2 is connected to node 3, these three nodes form a connected component of size 3. If node 4 is not connected to any of these, it forms its own component of size 1.
Input
The first line contains two integers N and M, the number of nodes and edges. The next M lines each contain two integers u and v, representing an edge between node u and node v.
Output
Print a single integer: the number of nodes in the connected component containing node 1.
Constraints
1 <= N <= 1000 0 <= M <= N*(N-1)/2 1 <= u, v <= N u != v
Example 1
5 3 1 2 2 3 4 5
3
Nodes 1, 2, 3 are connected. Nodes 4, 5 are connected. Node 1's component has size 3.
Example 2
4 2 1 2 3 4
2
Node 1 is connected to node 2. Node 3 is connected to node 4. Node 1's component has size 2.
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
4 2 1 2 3 4