Manthan, Codefest 17
G
Harry Vs Voldemort
Codeforces
3300
原题
← F→
翻译中…

After destroying all of Voldemort's Horcruxes, Harry and Voldemort are up for the final battle. They each cast spells from their wands and the spells collide.

翻译中…

The battle scene is Hogwarts, which can be represented in the form of a tree. There are, in total, $*n*$ places in Hogwarts joined using $*n* - 1$ undirected roads.

翻译中…

Ron, who was viewing this battle between Harry and Voldemort, wondered how many triplets of places $(*u*, *v*, *w*)$ are there such that if Harry is standing at place $*u*$ and Voldemort is standing at place $*v*$, their spells collide at a place $*w*$. This is possible for a triplet only when $*u*$, $*v*$ and $*w*$ are distinct, and there exist paths from $*u*$ to $*w*$ and from $*v*$ to $*w*$ which do not pass through the same roads.

翻译中…

Now, due to the battle havoc, new paths are being added all the time. You have to tell Ron the answer after each addition.

翻译中…

Formally, you are given a tree with $*n*$ vertices and $*n* - 1$ edges. $*q*$ new edges are being added between the nodes of the tree. After each addition you need to tell the number of triplets $(*u*, *v*, *w*)$ such that $*u*$, $*v*$ and $*w*$ are distinct and there exist two paths, one between $*u*$ and $*w*$, another between $*v*$ and $*w*$ such that these paths do not have an edge in common.

翻译中…

Input

翻译中…

First line contains an integer $*n*$ ($1 ≤ *n* ≤ 10^{5}$), the number of places in Hogwarts.

翻译中…

Each of the next $*n* - 1$ lines contains two space separated integers $*u*$ and $*v*$ ($1 ≤ *u*, *v* ≤ *n*$) indicating a road between places $*u*$ and $*v*$. It is guaranteed that the given roads form a connected tree.

翻译中…

Next line contains a single integer $*q*$ ($1 ≤ *q* ≤ 10^{5}$), the number of new edges being added.

翻译中…

Each of the next $*q*$ lines contains two space separated integers $*u*$ and $*v*$ ($1 ≤ *u*, *v* ≤ *n*$) representing the new road being added.

翻译中…

Note that it is possible that a newly added road connects places that were connected by a road before. Also, a newly added road may connect a place to itself.

翻译中…

Output

翻译中…

In the first line print the value for the number of triplets before any changes occurred.

翻译中…

After that print $*q*$ lines, a single integer $*ans*_{*i*}$ in each line containing the value for the number of triplets after $*i*$-th edge addition.

翻译中…

Note

翻译中…

In the first sample case, for the initial tree, we have $(1, 3, 2)$ and $(3, 1, 2)$ as the only possible triplets $(*u*, *v*, *w*)$.

翻译中…

After addition of edge from $2$ to $3$, we have $(1, 3, 2)$, $(3, 1, 2)$, $(1, 2, 3)$ and $(2, 1, 3)$ as the possible triplets.

翻译中…

样例

Input #1
3
1 2
2 3
1
2 3
Output #1
2
4
InputCopy OutputCopy
Input #2
4
1 2
2 3
2 4
2
1 4
3 4
Output #2
6
18
24
InputCopy OutputCopy
Input #3
5
1 2
2 3
3 4
4 5
1
1 5
Output #3
20
60
InputCopy OutputCopy
题目信息
题号
855G
时间限制
2 s
内存限制
256 MB
标签
DFS 及类似
动态规划
图论
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。