{"raw_statement":[{"iden":"statement","content":"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.\n\nThe 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.\n\nRon, 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.\n\nNow, due to the battle havoc, new paths are being added all the time. You have to tell Ron the answer after each addition.\n\nFormally, 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."},{"iden":"input","content":"First line contains an integer _n_ (1 ≤ _n_ ≤ 105), the number of places in Hogwarts.\n\nEach 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.\n\nNext line contains a single integer _q_ (1 ≤ _q_ ≤ 105), the number of new edges being added.\n\nEach of the next _q_ lines contains two space separated integers _u_ and _v_ (1 ≤ _u_, _v_ ≤ _n_) representing the new road being added.\n\nNote 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."},{"iden":"output","content":"In the first line print the value for the number of triplets before any changes occurred.\n\nAfter 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."},{"iden":"examples","content":"Input\n\n3\n1 2\n2 3\n1\n2 3\n\nOutput\n\n2\n4\n\nInput\n\n4\n1 2\n2 3\n2 4\n2\n1 4\n3 4\n\nOutput\n\n6\n18\n24\n\nInput\n\n5\n1 2\n2 3\n3 4\n4 5\n1\n1 5\n\nOutput\n\n20\n60"},{"iden":"note","content":"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_).\n\nAfter 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."}],"translated_statement":[{"iden":"statement","content":"在摧毁了伏地魔的所有魂器后，哈利与伏地魔迎来了最终对决。他们各自从魔杖中施放咒语，咒语在空中相撞。\n\n这场战斗的场景是霍格沃茨，可以用一棵树来表示。霍格沃茨共有 #cf_span[n] 个地点，通过 #cf_span[n - 1] 条无向道路连接。\n\n罗恩正在观看哈利与伏地魔的战斗，他想知道有多少个三元组地点 #cf_span[(u, v, w)] 满足：当哈利站在地点 #cf_span[u]、伏地魔站在地点 #cf_span[v] 时，他们的咒语在地点 #cf_span[w] 相撞。只有当 #cf_span[u]、#cf_span[v] 和 #cf_span[w] 互不相同，且存在从 #cf_span[u] 到 #cf_span[w] 和从 #cf_span[v] 到 #cf_span[w] 的两条路径，且这两条路径不经过相同的道路时，该三元组才成立。\n\n由于战斗混乱，新的路径正不断被添加。你需要在每次添加后告诉罗恩答案。\n\n形式化地说，你被给定一棵包含 #cf_span[n] 个顶点和 #cf_span[n - 1] 条边的树。随后将添加 #cf_span[q] 条连接树中节点的新边。每次添加后，你需要计算满足以下条件的三元组 #cf_span[(u, v, w)] 的数量：#cf_span[u]、#cf_span[v] 和 #cf_span[w] 互不相同，且存在两条路径——一条连接 #cf_span[u] 与 #cf_span[w]，另一条连接 #cf_span[v] 与 #cf_span[w]——这两条路径没有公共边。\n\n第一行包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 105])，表示霍格沃茨的地点数量。\n\n接下来的 #cf_span[n - 1] 行，每行包含两个用空格分隔的整数 #cf_span[u] 和 #cf_span[v] (#cf_span[1 ≤ u, v ≤ n])，表示地点 #cf_span[u] 与 #cf_span[v] 之间有一条道路。保证给出的道路构成一棵连通树。\n\n下一行包含一个整数 #cf_span[q] (#cf_span[1 ≤ q ≤ 105])，表示将要添加的新边数量。\n\n接下来的 #cf_span[q] 行，每行包含两个用空格分隔的整数 #cf_span[u] 和 #cf_span[v] (#cf_span[1 ≤ u, v ≤ n])，表示将要添加的新道路。\n\n注意，新添加的道路可能连接原本已由道路相连的地点，也可能连接一个地点到其自身。\n\n第一行输出在任何更改发生前的三元组数量。\n\n随后输出 #cf_span[q] 行，每行一个整数 #cf_span[ansi]，表示在第 #cf_span[i] 条边添加后的三元组数量。"},{"iden":"input","content":"第一行包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 105])，表示霍格沃茨的地点数量。接下来的 #cf_span[n - 1] 行，每行包含两个用空格分隔的整数 #cf_span[u] 和 #cf_span[v] (#cf_span[1 ≤ u, v ≤ n])，表示地点 #cf_span[u] 与 #cf_span[v] 之间有一条道路。保证给出的道路构成一棵连通树。下一行包含一个整数 #cf_span[q] (#cf_span[1 ≤ q ≤ 105])，表示将要添加的新边数量。接下来的 #cf_span[q] 行，每行包含两个用空格分隔的整数 #cf_span[u] 和 #cf_span[v] (#cf_span[1 ≤ u, v ≤ n])，表示将要添加的新道路。注意，新添加的道路可能连接原本已由道路相连的地点，也可能连接一个地点到其自身。"},{"iden":"output","content":"第一行输出在任何更改发生前的三元组数量。随后输出 #cf_span[q] 行，每行一个整数 #cf_span[ansi]，表示在第 #cf_span[i] 条边添加后的三元组数量。"},{"iden":"examples","content":"输入31 22 312 3输出24输入41 22 32 421 43 4输出61824输入51 22 33 44 511 5输出2060"},{"iden":"note","content":"在第一个样例中，初始树上仅有两个可能的三元组 #cf_span[(1, 3, 2)] 和 #cf_span[(3, 1, 2)]。在添加从 #cf_span[2] 到 #cf_span[3] 的边后，可能的三元组变为 #cf_span[(1, 3, 2)]、#cf_span[(3, 1, 2)]、#cf_span[(1, 2, 3)] 和 #cf_span[(2, 1, 3)]。"}],"sample_group":[],"show_order":[],"formal_statement":"**Definitions**\n\nLet $G = (V, E)$ be an undirected multigraph where $V$ is the set of vertices and $E$ is the multiset of edges.\nLet $\\Pi(x, y; G)$ denote the set of all simple paths between vertices $x$ and $y$ in $G$.\nFor a path $\\pi$, let $E(\\pi)$ denote the set of edges in that path.\n\nDefine the set of valid triplets $\\mathcal{T}(G)$ as:\n$$\n\\mathcal{T}(G) = \\left\\{ (u, v, w) \\in V^3 \\ \\middle|\\ \n\\begin{aligned}\n& 1.\\ u \\neq v, u \\neq w, v \\neq w \\\\\n& 2.\\ \\exists \\pi_1 \\in \\Pi(u, w; G), \\exists \\pi_2 \\in \\Pi(v, w; G) \\text{ such that } E(\\pi_1) \\cap E(\\pi_2) = \\emptyset\n\\end{aligned}\n\\right\\}\n$$\n\n**Input Data**\n\n1.  An integer $n$ ($1 \\le n \\le 10^5$).\n2.  A set of edges $E_{init} = \\{(u_i, v_i)\\}_{i=1}^{n-1}$ such that the graph $G_0 = (V, E_{init})$ is a connected tree, where $V = \\{1, \\dots, n\\}$.\n3.  An integer $q$ ($1 \\le q \\le 10^5$).\n4.  A sequence of edge additions $A = \\langle (x_1, y_1), (x_2, y_2), \\dots, (x_q, y_q) \\rangle$, where $1 \\le x_j, y_j \\le n$. Note that $G$ may contain self-loops and multiple edges.\n\n**Problem Statement**\n\nLet $G_0 = (V, E_{init})$.\nFor $k = 1, \\dots, q$, let $G_k = (V, E_k)$ where $E_k = E_{k-1} \\cup \\{(x_k, y_k)\\}$.\n\n**Objective**\n\nCompute and output the sequence of integers $ans_0, ans_1, \\dots, ans_q$, defined as:\n$$ ans_k = |\\mathcal{T}(G_k)| $$\nfor $k = 0, 1, \\dots, q$.","simple_statement":null,"has_page_source":false}