972. Check if Edge Exists in Graph

EasyGraphsRepresentationHashGraph

Given an undirected graph with n nodes and a list of edges, plus a list of queries [u, v], return for each query whether an edge directly connects u and v. Return the boolean answers as an array in order. The input is JSON {n, edges, queries}.

Input: JSON {n, edges, queries}.

Output: Array — a boolean per query.

Examples

Example 1
Input: {"n":5,"edges":[[0,1],[0,2],[1,3]],"queries":[[0,1],[1,2],[3,0]]}
Output: [true,false,false]
Explanation: Only the first query is an edge.
Example 2
Input: {"n":2,"edges":[[0,1]],"queries":[[0,1],[1,0]]}
Output: [true,true]
Explanation: Undirected edge both ways.
Example 3
Input: {"n":3,"edges":[],"queries":[[0,1]]}
Output: [false]
Explanation: No edges.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →