Codeforces Round 564 (Div. 1)
E
Nauuo and ODT
Codeforces
3300
原题
← DF →
翻译中…

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 $n$ nodes and $n-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 $m$ 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 $u$ to $v$ and the simple path from $v$ to $u$ as two different simple paths if and only if $u\ne v$.

翻译中…

Input

翻译中…

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

翻译中…

The second line contains $n$ integers $c_1,c_2,\ldots,c_n$ ($1\le c_i\le n$), where $c_i$ is the initial color of node $i$.

翻译中…

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

翻译中…

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

翻译中…

Output

翻译中…

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

翻译中…

Note

翻译中…

Example 1

翻译中…

image

翻译中…

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

翻译中…

image

翻译中…

样例

Input #1
5 3
1 2 1 2 3
1 2
1 3
3 4
3 5
3 3
4 1
4 3
Output #1
47
51
49
45
InputCopy OutputCopy
Input #2
6 1
1 1 1 1 1 1
1 2
2 3
3 4
4 5
5 6
1 2
Output #2
36
46
InputCopy OutputCopy
题目信息
题号
1172E
时间限制
7.5 s
内存限制
512 MB
标签
数据结构

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