P18899 [GeeksforGeeks] Level of nodes 1587115620
普及
题目描述

Given a Undirected Graph with V vertices and E edges, Find the level of node X. if X does not exist in the graph then print -1.
Note: Traverse the graph starting from vertex 0.

Example 1:
Input:
X = 4
Output:
2
Explanation:
We can clearly see that Node 4 lies at Level 2.
Example 2:
Input:
X = 5
Output:
-1
Explanation:
Node 5 isn't present in the Graph, so the
Output is -1.

Your task:
You dont need to read input or print anything. Your task is to complete the function nodeLevel() which takes two integers V and X denoting the number of vertices and the node, and another adjacency list adj as input parameters and returns the level of Node X. If node X isn't present it returns -1.
Expected Time Complexity: O(V + E)
Expected Auxiliary Space: O(V)

Constraints:
2 ≤ V ≤ 10^{4}
1 ≤ E ≤ (N*(N-1))/2
0 ≤ u, v < V
1 ≤ X ≤ 10^{4}
Graph doesn't contain multiple edges and self loops.

输入格式

见题面

输出格式

见题面

样例输入
X = 4
样例输出
2