You are given a connected, unweighted and undirected graph with ‘N’ nodes [0 to N - 1] and ‘M’ edges. Your task is to answer ‘Q’ queries on this graph. Each query consists of four integers ‘A’, ‘B’, ‘C’, and ‘D’. For the current query add an edge between nodes numbered ‘A’ and ‘B’ (note that this operation is temporary and only for the current query). Now, output the maximum number of bridge edges occurring on any path between ‘C’ and ‘D’.
Note: An edge between node ‘u’ and ‘v’ is said to be a bridge edge, if after removal of an edge ‘u’ - ‘v’, there is no path between node ‘u’ and ‘v’.
For example:
For given N = 4, M = 3, Q = 1, A = 2, B = 3, C = 0 and D = 3
For all queries, we are temporarily adding an edge between nodes, and here, after the addition of an edge 2 - 3, the graph looks like this:

So, the number of bridge edges on the path from node 0 to 3 is 1 i.e 0 - 1.
The first line of input contains three integers ‘N’, ‘M’, and ‘Q, denoting the number of nodes, number of edges, and number of queries.
The next ‘M’ line contains two space-separated integers ’u’ and ‘v’, denoting an undirected edge between ‘u’ and ‘v’.
The next ‘Q’ line contains four space-separated integers ‘A’, ‘B’, ‘C’ and ‘D’.
Output Format:
For each query, print a single integer ‘X’, denoting the maximum number of bridge edges occurring on any path between ‘C’ and ‘D’.
The output of each test case will be printed on a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= Q <= 100
1 <= N, M <= 100
0 <= u, v <= N - 1
0 <= A, B, C, D < N - 1
Time Limit: 1 sec.
4 3 2
0 1
1 2
1 3
2 3 0 3
2 3 2 3
1
0
In the first query, after the addition of an edge 2 - 3, the graph looks like this:

So, the number of bridge edges on the path from node 0 to 3 is 1 i.e 0 - 1.
In the second query, there is no bridge edge in the above graph after adding edges 2-3. So the number of bridge edges is equal to 0.
Note: For each query, we are temporarily adding an edge between nodes.
6 5 1
0 1
1 2
1 3
3 4
4 5
0 4 2 5
2
In the first query, after the addition of an edge 0 - 4 the graph looks like this:

So, the number of bridge edges are 2 i.e( 2 - 1 and 4 - 5)
Take help from a bridge tree.
The idea is to first build a “Bridge Tree” and then handle the query using this “Bridge Tree”.
Let’s say for the given query ‘A’, ‘B’, ‘C’ and ’D’.The Node ‘A’ lies in component ‘Ca’ of the “Bridge tree”. Similarly, node ‘B’ lies in 'Cb’, ‘C’ lies in ‘Cc’ and ‘D’ lies in ‘Cd’ component of “Bridge Tree”.
After adding nodes ‘A’ and ‘B’ in the graph, all bridges from ‘Ca’ to ‘Cb’ in the bridge tree are destroyed ( property of bridge tree). So, our answer would be the distance between components of ‘C’ and ‘D’ (‘Cc’ and ‘Cd’ ) in the tree minus the edges which occur on the path from ‘Ca’ to ‘Cb’ (ie. a number of edges in the intersection of paths Ca' - ‘Cb’ and ‘Cc’ - ‘Cd’).
Now, we just have to find the length of intersection of paths ‘A' - ‘B’ and ‘C’ - ‘D’. components Let's break down each path into two parts because finding intersections between straight chains would be easier. So, let u = LCA(A, B) and v = LCA(C, D) , so total intersection would be intersection(u, a, v, c) + intersection(u, a, v, d) + intersection(u, b, v, c) + intersection(u, b, v, d).
Note, the above ‘A’, ‘B’, ‘C’ and ‘D’ are components of given nodes respectively.
To find the distance between two nodes in a tree we will use the formula, D( A, B) = Distance of node ‘A’ from root + Distance of node ‘B’ from the root - 2 * distance of node LCA(A, B) from the root, where distances from root to all nodes can be calculated using a DFS and for calculating LCA we can use Dynamic Programming.
Consider the following global variables
Let ‘countBridge(n, m, q, edges, operation) ’ be the function to count the number of bridges between edge ‘C’ and ‘D’ after adding edge ‘A’ and ‘B’.
This function finds the discovery time of each node in the given graph, also it marks the ‘isBridge’ array if the edge between U[i] - V[i] is a bridge. This function returns the ‘low’ value of node ‘u’.
This function is used to construct a bridge tree.
This function returns the LCA of node ‘p’ and ‘q’
This function returns a common path between path u -> a and v -> b.
This function checks whether node ‘u’ is the ancestor of node ‘a’ or not. If yes then return true else return false.
This function returns the distance between node ‘a’ and node ‘b’ with the help of lca node.
O((M + N) * logN) where ‘N’ is the number of nodes ‘M’ is the number of edges
Every dfs function is O(M + N) and for each query, we are finding LCA that takes O(logN).
In the worst case, the number of queries is equal to the number of nodes ‘N. So the overall time complexity is O((M + N) log(N)).
O(M + N) where ‘N’ is the number of nodes ‘M’ is the number of edges
Mainly we are using space in creating graphs and trees, which takes O(N + M) space.