编程介的小学生 2019-02-20 11:17 采纳率: 20.5%
浏览 171

子树的便利算法,采用图的形式,如何利用C语言算法来实现呢?

Problem Description
Miceren finds a huge country named HY. HY has N cities numbered from 1 to N connected by N − 1 bidirectional roads. There exists a path between any two cities.

It can be imagined as a tree with n vertices rooted at vertex 1.

Miceren wants to occupy some cities here. Each city has a value vi. (Notice that the value of a city may be negative. Nevertheless, Miceren wants to occupied this city.)

As some usual stories, someone named Cloud wants to "steal" some cities from Miceren.

At the beginning, Miceren and Cloud don't occupy any city.

In the following Q days, one of three events may happen

  1. Miceren will walk from the a-th city to the b-th city and all cities visited in this trip will belong to Miceren. (1 ≤ a,b ≤ N)

  2. Cloud will steal the x-th city. If Miceren occupied the x-th city before, Miceren will lost the control of this city. (1 ≤ x ≤ N)

  3. Miceren will occupy the subtree rooted at x.(1 ≤ x ≤ N)

As Miceren's friend, you must tell Miceren the total value of all cities which belong to Miceren after each day.

Input
The first line contains a single integer T, indicating the number of test cases.

Each test case begin with one integer N, indicating the number of cities in HY.

The next line contains N integer Vi, indicating the value of each city.

The next N − 1 lines contain the details of the roads. Each line contains two integers u, v meaning that there is a road between cities u and v.

The next line contains one integer Q.

The next Q lines contain the details of event. If the format is "1 a b", it means the first event happened where Miceren walks from a-th city to b-th city. If the format is “2 x”, it means the second event happened where Cloud "steal"s the x-th city. Otherwise the format is “3 x” and the third event happened where Micron occupied the subtree rooted at x.

T is about 100.

1 ≤ N,Q ≤ 100000.

−1000 ≤ Vi ≤ 1000.

The ratio of test cases with N > 100 is less than 5%.

Output
For each test queries, print the answer.

Sample Input
1
10
1 2 3 4 5 6 7 8 9 10
1 2
1 3
2 4
2 5
5 9
5 10
3 6
3 7
3 8
6
1 10 4
1 9 7
2 5
3 4
2 4
1 6 10

Sample Output
21
41
36
36
32
43

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?