As suggested by the task name, we are going to join two trees together!
You are given two trees: $$$T_1$$$ with $$$N_1$$$ vertices and $$$T_2$$$ with $$$N_2$$$ vertices. The vertices of $$$T_1$$$ are numbered from $$$1$$$ to $$$N_1$$$, while those of $$$T_2$$$ are numbered from $$$N_1 + 1$$$ to $$$N_1 + N_2$$$. A tree of $$$N$$$ vertices is a connected undirected graph with $$$N - 1$$$ edges.
Let $$$dist(x, y)$$$ be the distance between vertices $$$x$$$ and $$$y$$$. The distance between two vertices is the number of edges on the simple path between them.
The diameter of a graph is defined to be the maximum distance between any two vertices. You may have noticed that there may be multiple vertex pairs that have their distance equal to the diameter.
Now, you need to add exactly one edge between a vertex in $$$T_1$$$ and a vertex in $$$T_2$$$ to create a new tree $$$T$$$. Find the maximum number of vertex pairs in $$$T$$$ that have their distance equal to the diameter of $$$T$$$.
In other words, maximize the number of unordered pairs $$$(x, y)$$$ such that $$$dist(x, y) = \max_{1 \leq u, v \leq N_1 + N_2} dist(u, v)$$$ in the resulting tree $$$T$$$.
The first line contains two integers $$$N_1$$$ and $$$N_2$$$ ($$$2 \leq N_1, N_2 \leq 10^5$$$).
The next $$$N_1 - 1$$$ lines each contains two integers $$$x$$$ and $$$y$$$, representing an edge connecting vertex $$$x$$$ and vertex $$$y$$$ in $$$T_1$$$ ($$$1 \leq x, y \leq N_1$$$, $$$x \neq y$$$).
The next $$$N_2 - 1$$$ lines each contains two integers $$$x$$$ and $$$y$$$, representing an edge connecting vertex $$$x$$$ and vertex $$$y$$$ in $$$T_2$$$ ($$$N_1 + 1 \leq x, y \leq N_1 + N_2$$$, $$$x \neq y$$$).
A single integer, the maximum number of vertex pairs in $$$T$$$ such that their distance is equal to the diameter of $$$T$$$.
5 5 1 2 1 3 1 4 1 5 6 7 6 8 6 9 6 10
16
8 3 1 2 2 3 3 4 4 5 5 6 3 7 7 8 9 10 10 11
6
In the first sample, adding an edge between vertex $$$1$$$ and vertex $$$6$$$ will yield $$$16$$$ vertex pairs with distances equal to the diameter of $$$T$$$, which is the maximum number of vertex pairs you can get.
In the second sample, the two given trees are shown in the following figure: