CF855G.Harry Vs Voldemort

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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, nn places in Hogwarts joined using n1n-1 undirected roads.

Ron, who was viewing this battle between Harry and Voldemort, wondered how many triplets of places (u,v,w)(u,v,w) are there such that if Harry is standing at place uu and Voldemort is standing at place vv , their spells collide at a place ww . This is possible for a triplet only when uu , vv and ww are distinct, and there exist paths from uu to ww and from vv to ww 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 nn vertices and n1n-1 edges. qq 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)(u,v,w) such that uu , vv and ww are distinct and there exist two paths, one between uu and ww , another between vv and ww such that these paths do not have an edge in common.

输入格式

First line contains an integer nn ( 1<=n<=1051<=n<=10^{5} ), the number of places in Hogwarts.

Each of the next n1n-1 lines contains two space separated integers uu and vv ( 1<=u,v<=n1<=u,v<=n ) indicating a road between places uu and vv . It is guaranteed that the given roads form a connected tree.

Next line contains a single integer qq ( 1<=q<=1051<=q<=10^{5} ), the number of new edges being added.

Each of the next qq lines contains two space separated integers uu and vv ( 1<=u,v<=n1<=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.

输出格式

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

After that print qq lines, a single integer ansians_{i} in each line containing the value for the number of triplets after ii -th edge addition.

输入输出样例

  • 输入#1

    3
    1 2
    2 3
    1
    2 3
    

    输出#1

    2
    4
    
  • 输入#2

    4
    1 2
    2 3
    2 4
    2
    1 4
    3 4
    

    输出#2

    6
    18
    24
    
  • 输入#3

    5
    1 2
    2 3
    3 4
    4 5
    1
    1 5
    

    输出#3

    20
    60
    

说明/提示

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

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

首页