#FT0000195. Đảo màu đỉnh và tổng thành phần liên thông (Vertex Flip Query)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Đảo màu đỉnh và tổng thành phần liên thông (Vertex Flip Query)
Vertex Flip Query
Source: AtCoder Beginner Contest 460 – G
Edition: Phuoc Hung OJ; original semantics and input format retained
Problem Statement
You are given an undirected tree of vertices numbered from to . Every vertex has a positive integer weight and a binary color .
The monochromatic connected component containing consists of all vertices such that every vertex on the unique path from to has color . Vertex itself is included. A vertex of a different color blocks the path, even if further vertices share the original color.
Process operations in the given order:
1 v: Flip the color of vertex : . Its weight is unchanged.2 v x: Add to the weight of vertex : . This is addition, not assignment.3 v: Output the sum of the weights of all vertices in the monochromatic connected component containing under the current colors and weights.
Every modification takes effect immediately. A query of type 3 never modifies the tree.
Input
- The first line contains .
- The second line contains .
- The third line contains .
- Each of the next lines contains an edge .
- The last lines contain operations in one of the forms
1 v,2 v x,3 v.
Vertices are 1-indexed; there is exactly one test case.
Output
For each type-3 query, output one integer on a separate line in query order. If there are no such queries, output nothing. No modulus is applied; use a 64-bit integer.
Subtasks
- Subtask 1 (20 points): ; ; ; ; the edges satisfy and form a connected tree; all queries satisfy , and type-
2operations satisfy . - Subtask 2 (30 points): ; ; ; ; the edges satisfy and form a connected tree; all queries satisfy , and type-
2operations satisfy . - Subtask 3 (50 points): ; ; ; ; the edges satisfy and form a connected tree; all queries satisfy , and type-
2operations satisfy .
Note: These are PHOJ training subtasks; the original AtCoder task specifies only the full constraints, not these three scoring groups.
Examples
Example 1
Input
5 9
1 10 100 1000 10000
0 0 0 0 0
1 2
2 3
3 4
2 5
3 1
1 2
3 1
3 2
3 3
1 3
1 2
2 1 1
3 5
Output
11111
1
10
1100
10012
Step-by-step explanation
3 1: all five vertices have color and form one connected component. The sum is .1 2: flip vertex to color , separating vertex from vertices .3 1: the component is with sum .3 2: the component is with sum .3 3: the component is with sum .1 3: flip vertex to color , joining vertex but separating vertex .1 2: flip vertex back to color ; the component containing vertex becomes .2 1 1: increase the weight of vertex from to .3 5: sum the weights of to get .
Only five queries are of type 3, so exactly five output lines appear.
Example 2
Input
10 25
1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000
0 1 0 0 0 1 0 0 0 0
1 2
1 3
2 4
4 5
4 6
2 7
5 8
8 9
6 10
3 4
1 8
2 6 100000
3 3
1 3
1 7
1 7
1 7
3 8
2 1 1
2 6 100000
2 8 10000000
1 5
2 5 10000
1 9
2 7 1000000
1 7
2 10 1000000000
1 9
1 6
1 9
1 1
3 7
2 5 10000
3 6
Output
110011000
101
10000000
2000000
2000301000
Step-by-step explanation
- Query 1,
3 4: component , sum . - Query 4,
3 3: after the preceding changes, component , sum . - Query 9,
3 8: component , current weight . - Query 23,
3 7: component , whose weight has increased to . - Query 25,
3 6: component , whose weights are now , totaling .