Given a tree with N vertices and N-1 undirected edges. 1<=N<=250000 There are certain queries to be performed. 1<=Q<=10^5 In each query we will be given two vertices,say a and b. We have to tell the number of pairs of vertices such that the path between those contain exactly one vertex common with the vertices present in the path between the vertices a and b. For example: N=6.So N-1 edges.--> 1 2, 1 3, 1 4, 2 5, 2 6. (Meaning 1 is connected to 2,3,4 and 2 is connected to 1,5,6)
Let our query be a and b--> 4 5.
Answer will be 6.(Means there exist 6 pairs of vertices in the graph with the given condition) Those 6 pairs are: 1 1, 2 2, 4 4, 5 5, 1 3, 2 6.
I have been thinking this question for 2 days but I am not able to get any perfect logic for this. I first thought that we can find by using LCA (lowest common ancestor) but then I could not find how.
Please help me to get some of the logic so I can solve it efficiently.