1011. Check if Two Nodes are in Same Component

EasyGraphsDSUGraph

Given an undirected graph with n nodes and a list of edges, plus a list of queries [u, v], return for each query whether u and v belong to the same connected component. 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],[2,3]],"queries":[[0,1],[1,2],[2,3]]}
Output: [true,false,true]
Explanation: Component membership per query.
Example 2
Input: {"n":3,"edges":[],"queries":[[0,0],[0,1]]}
Output: [true,false]
Explanation: Self is same; others differ.
Example 3
Input: {"n":2,"edges":[[0,1]],"queries":[[0,1]]}
Output: [true]
Explanation: Connected pair.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →