【LA7402】colorful tree 数据结构

这道题目描述了一个关于树形结构的问题,其中树上的每个节点代表一只青蛙,每只青蛙有不同的颜色,并且可以在一天内改变颜色。题目要求处理修改颜色的操作以及查询子树内不同颜色青蛙的数量。解决方案涉及树的深度优先搜索、平衡树和线段树的数据结构,以及处理路径更新和单点查询的技巧。
摘要由CSDN通过智能技术生成

As we all know, frogs live on trees and have different colors.
N frogs are living on a tree. The tree consists of N nodes with node 1 as the root, each frog occupies
a node.
Frogs have different colors, and can change colors as they like. On each day, all the frogs living on
a certain sub-tree will change its color. The root of the sub-tree, and the color they change to, is given
to the frog king.
As the frog king, sometimes he may wonder, how many different colors of frog are there in a certain
sub-tree? It turns to you to solve the problem for the king.
Input
First line contains an integer T, which indicates the number of test cases.
Every test case begins with an integers N, which is the numbers of nodes in the tree.
The following N − 1 lines describe the edges of the tree, and every line is formatted as ‘u v’, which
indicates there is a edge between node u and node v.
The next line contains N intergers, c1, c2, · · ·, cN , and ci
is the initial color of the frog living at
node i.
Then a number M follows, which indicates the number of queries, and following M lines describe
the quries as format bellow.
operation format description
modify color 0 u c change the color of all frogs in the sub-tree rooted at
node u to c
query 1 u query how many different colors of frog are there in
the sub-tree rooted at node u
Restrictions:
• 1 ≤ T ≤ 100.
• For 85% data, 1 ≤ N, M ≤ 1000.
• for 100% data, 1 ≤ N, M ≤ 105
.
• for every node, 1 ≤ ci ≤ N.
• for every edge, 1 ≤ u, v ≤ N.
• for every query, 1 ≤ u, c ≤ N.
Output
For every test case, you should output ‘Case #x:’ first, where x indicates the case number and counts
from 1.
Then for each query operation, output the number of different colors.
Sample Input
1
5
1 2
1 3
2 5
2 4
1 2 3 4 5
6
1 1
0 4 2
1 1
1 2
0 2 3
1 2
Sample Output
Case #1:
5
4
2
1

原题传送门
WC上刘汝佳先生推荐的题,当时和许多小伙伴想了许久,有人给出了一个好像还不错的做法(然而忘了……)后来给刘发了邮件询问,它给我发了一篇题解,看了之后总算大概明白该怎么做了。通过这个题真的是明白了 树上做的事情比起直接在序列上做,不见得更糟糕。 (想引用WC上营员交流时……好像是小火车说的话?“现在有些出题人,总喜欢把序列上的问题出到树上,让选手强行写个树链剖分……其实,出到仙人掌上也是可以的……希望大家不要拿这种东西出题……”),zgy神犇的一句话很好地解释了这个看起来不太合理的事实背后的原因:“子树只有n个(区间有 O(n2)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值