CF1172E.Nauuo and ODT

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Nauuo is a girl who loves traveling.

One day she went to a tree, Old Driver Tree, literally, a tree with an old driver on it.

The tree is a connected graph consisting of nn nodes and n1n-1 edges. Each node has a color, and Nauuo will visit the ODT through a simple path on the tree in the old driver's car.

Nauuo wants to visit see more different colors in her journey, but she doesn't know which simple path she will be traveling on. So, she wants to calculate the sum of the numbers of different colors on all different paths. Can you help her?

What's more, the ODT is being redecorated, so there will be mm modifications, each modification will change a single node's color. Nauuo wants to know the answer after each modification too.

Note that in this problem, we consider the simple path from uu to vv and the simple path from vv to uu as two different simple paths if and only if uvu\ne v .

输入格式

The first line contains two integers nn and mm ( 2n41052\le n\le 4\cdot 10^5 , 1m41051\le m\le 4\cdot 10^5 ) — the number of nodes and the number of modifications.

The second line contains nn integers c1,c2,,cnc_1,c_2,\ldots,c_n ( 1cin1\le c_i\le n ), where cic_i is the initial color of node ii .

Each of the next n1n-1 lines contains two integers uu and vv ( 1u,vn1\le u,v\le n ), denoting there is an edge between uu and vv . It is guaranteed that the given edges form a tree.

Each of the next mm lines contains two integers uu and xx ( 1u,xn1\le u,x\le n ), which means a modification that changes the color of node uu into xx .

输出格式

The output contains m+1m+1 integers — the first integer is the answer at the beginning, the rest integers are the answers after every modification in the given order.

输入输出样例

  • 输入#1

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

    输出#1

    47
    51
    49
    45
    
  • 输入#2

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

    输出#2

    36
    46
    

说明/提示

Example 1

The number of colors on each simple path at the beginning:

首页