Check whether the graph is bipartide or not

Given a undirected graph having A nodes.
A matrix B of size M x 2 is given which represents the edges such that there is an edge between
B[i][0] and B[i][1].

Find whether the given graph is bipartide or not.

A graph is bipartite if we can split it's set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B

Note:

  1. There are no self-loops in the graph.

  2. No multiple edges between two pair of vertices.

  3. The graph may or may not be connected.

  4. Nodes are Numbered from 0 to A-1.

  5. Your solution will run on multiple testcases. If you are using global variables make sure to clear them.



Input Format

The first argument given is an integer A.
The second argument given is the matrix B.

Output Format

Return 1 if the given graph is bipartide else return 0.

Constraints

1 <= A <= 100000
1 <= M <= min(A*(A-1)/2,200000)
0<= B[i][0],B[i][1] < A

For Example

Input 1:
    A : 10
    B : [   [7, 8]
            [1, 2]
            [0, 9]
            [1, 3]
            [6, 7]
            [0, 3]
            [2, 5]
            [3, 8]
            [4, 8]  ]
Output 1:
    1

Input 2:
    A : 9
    B : [   [2, 5]
            [6, 7]
            [4, 8]
            [2, 3]
            [0, 3]
            [4, 7]
            [1, 8]
            [3, 8]
            [1, 3]  ]
Output 2:
    0
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Check whether the graph is bipartide or not on Interview Code Editor
Sign Up
to access hints and editorial solutions for Check whether the graph is bipartide or not

Discussion


Loading...
Click here to start solving coding interview questions