2445: Tree
| Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
|---|---|---|---|---|---|
| 2s | 16384K | 384 | 40 | Standard |
in this problem , i will give you a n-vertex-01-tree with the vertex numbered with 1 as root , as it states,the tree has n vertexes, each node is marked from 1 to n, the node marked with 1 is the root of tree. each node has a value either 1 or 0, in the initial state, each node has the value 0;
in the process,there are two operations on the tree:
1. Q x: return the sum of the value on the subtree with vertext x as root ( include the value of on the vertex x );
2. C x: inverse all the value on the subtree with vertex x as root ( include vertex x ), inverse means turn 0 to 1 ,1 to 0;
Input
the input has multicases, in each case , the first line is n, the number vertex of tree; following will be n - 1 lines, each line has two integers u, v (1 <= u,v <= n) which means there is a side between u and v; following is a integer m which means there are m operations, the following m lines is either "Q x" or "C x" ( quotation for clarification ) , 1<=n,m<=20000;
Output
for each "Q x" operation return the sum of the value on the subtree with vertex x as root
Sample Input
5 1 2 1 3 2 4 2 5 5 Q 1 C 1 Q 1 C 2 Q 1
Sample Output
0 5 2
Hint
the tree looks as
1
/ /
2 3
/ /
4 5
the first "Q 1" return 0 becaus all the node with value 0 ( the initial state ), the second return 5 because we have a operation "C 1" on the tree which inverse all the value on the tree ( turn all 0 to 1, turn all 1 to 0 ( in this state no 1) ) include the value on the node 1, so return 5, the third turn the value on the node 2, 4, 5 from 1 to 0 so return 2;
Problem Source: kscinow
This problem is used for contest: 95
Submit / Problem List / Status / Discuss
本文介绍了一道关于树的数据结构操作问题。题目要求实现两种操作:一是查询子树中所有节点的值之和;二是反转子树中所有节点的值。通过深度优先搜索建立离线线段树,并使用递归更新和查询,有效地解决了问题。
712

被折叠的 条评论
为什么被折叠?



