Graph Node Count
EasyPractice library · +10 XPgraphsbfsdfs
Given an undirected graph, find the total number of nodes present in it. The graph is represented by its adjacency list. You are given the number of nodes, N, and the number of edges, M. The nodes are numbered from 0 to N-1. The following M lines describe the edges.
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 undirected edge between node u and node v.
Output
Print a single integer, the total number of nodes in the graph.
Constraints
1 <= N <= 1000, 0 <= M <= 1000. Nodes are numbered from 0 to N-1.
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
5 3 0 1 1 2 3 4
Output
5
The graph has 5 nodes (0 to 4) and 3 edges. The output is 5.
Example 2
Input
3 0
Output
3
A graph with 3 nodes and no edges. The output is 3.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 3 0 1 1 2 3 4
Example 2 input
3 0