Last Updated: 26 May, 2022

Good Bad Graph

Hard
Asked in company
Ola

Problem statement

There are ‘N’ cities in ninja Kingdom and Mayer of the city wants to connect all the cities using roads by performing the following operations ‘N - 1’ times. Initially, there are no edges between any cities.

At every operation, you are given two cities, ‘A’ and ‘B’, and you have to connect the city if they are not already connected.

Connected component is that part of the graph in which we have at least two city and we can reach one city to other cities and no two connected components share a common city.

After every input edge, there can be multiple connected components.

Component is good if the number of cities of that component is even; otherwise, it is a bad component.

You need to find the absolute difference between total good and bad components after adding each edge.

Input Format :
The first line contains a single integer ‘T’ denoting the number of test cases. The test cases are as follows.

The first line of each test case contains the value ‘N’, denoting the number of cities.

Following ‘N - 1’ lines contain two space-separated integers, ‘u’ and ‘v’, which we have to connect the city ‘u’ and ‘v’.
Output format :
For each test case, return an array of size ‘N - 1’, where the ‘i’th ( 0 <= i < N - 1 )  value represents the absolute difference between total good and bad components after adding the ‘ith’ edge.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10

2 <= N <= 10^5

Time Limit: 1 sec

Approaches

01 Approach

 

APPROACH : 
 

  • First, we will add the given edges to the graph for each operation.
  • Then we will traverse the graph and find all the connected components.
  • After that, we will iterate through every connected component to check whether it is good or bad.
  • We will perform these steps for every operation.
  • Then, we will print the absolute difference between the total good and bad components after each operation.


 

ALGORITHM :
 

  • Initialize the ‘adj’ matrix of size ‘N’.
  • Initialize the empty ‘ans’ array which we have to return at the end.
  • For every operation:
    • Initialize an integer variable ‘cnt’ with the value zero.
    • Initialize the integers  ‘p’, ‘q’ denoting the cities to which we have to connect.
    • Initialize the ‘vis’ array of length ‘N + 1’ and set all its values to zero.
    • Add vertices ‘p’ and ‘q’ if not already added in the ‘adj’ matrix.
    • We will iterate from ‘i = 1’ till ‘i <= N’:
      • If ‘vis[ i ]’ == 0 :
        • Call dfs which will find all the cities which are connected to ‘ith’ cities and mark them visited.
    • Count all the good and bad connected components.
    • Update the ‘cnt’ with the absolute difference between the good and bad components.
    • Insert the ‘cnt’ to ‘ans’ array.
  • Return ‘ans’;

02 Approach

APPROACH : 

 

  • For each operation, we have to check how our graph is changing if we add some edge,  when we are connecting an edge we first need to check if the first city is connected to the second city if they are connected we don't need to do anything as they are already connected otherwise we will create an edge between both the node with this we will be merging two connected components and creating a single connected component since this is basically DSU itself so we will use DSU and now update the count of the good and bad component after every operation.


 

ALGORITHM :
 

  • Initialize the ‘adj’ matrix of size ‘N’.
  • Initialize the DSU of length ‘N’.
  • Initialize the empty ‘ans’ array which we have to return at the end.
  • Initialize the integer ‘cnt_good’ and ‘cnt_bad’  with the value zero.
  • For every operation:
    • Initialize an integer variable ‘ans’ with the value zero.
    • Initialize an integer variable ‘cnt’ with the value zero.
    • Initialize the integers  ‘p’, ‘q’ denoting the city which we have to connect
    • Call ‘merge( ‘p’ , ‘q’ )’ function which will merge city ‘p’ and city ‘q’ and inside the merge function, first decrease the count of good or bad components where ‘p’  belongs to by one and similarly for ‘q’, and increase the count of good or bad component where final connected component (‘p’+’q’) belongs to.
    • Update the ‘cnt’ with the absolute difference between the cnt_good and cnt_bad value.
    • Insert the ‘cnt’ to ‘ans’ array.
    • Return ‘ans’;