#P8359. [SNOI2022] 垃圾回收

[SNOI2022] 垃圾回收

Problem Description

Normally, programming languages make one of the following choices when managing memory:

  • Let users manage memory manually (C, C++, Rust, etc.). This usually gives good performance, but also places a heavy programming burden on users.
  • Use a garbage collection system (Java, Go, etc.). This requires maintaining a runtime system, and it introduces many unpredictable costs in memory usage and program performance.

Despite many issues, the most widely used automatic memory management approach is still the Tracing Garbage Collector. The basic idea is to maintain reference relationships between objects to form a graph. Each time collection happens, it scans the reference graph to deduce which objects can no longer be reached, and frees the memory they occupy. The biggest problem with this traditional approach is that maintaining reference chains is expensive, and as the number of maintained objects increases, the scanning cost also increases.

Little L is a girl who likes thinking. She found that maintaining a Garbage Collector is very complicated, so she decided to consider a simpler model (note that it may be completely different from any real-world GC rules!).

You are given an undirected graph with nn vertices and mm edges, with no multiple edges or self-loops. Vertices and edges are both numbered starting from 11. Each vertex represents an object that occupies some amount of memory, and each edge corresponds to a reference relationship (note that the reference relationship here is undirected). The program starts running at time 00 and ends at time q+1q + 1. For each time i=1,2,3,,qi = 1, 2, 3, \dots, q, exactly one of the following two operations happens:

  • DELETE ii: delete the edge (xi,yi)(x_i, y_i), and it is guaranteed that you will not delete an edge that has already been deleted.
  • GC: perform one garbage collection, i.e., kill all vertices that are not reachable from the start vertex, and free the memory they occupy. (Note that deleting vertices here does not delete the edges connected to these vertices.)

You may assume that these operations are completed instantly. After all operations are done, i.e., at time q+1q + 1, the program ends and deletes all remaining vertices (including vertex 11).

The memory occupied by vertex ii is aia_i. You need to compute i=1naialivei\sum_{i = 1}^{n} a_i \cdot \mathit{alive}_i, where alivei\mathit{alive}_i is the time that vertex ii stays alive. At time 00, all vertices are alive.

Input Format

The first line contains three positive integers n,m,qn, m, q, representing the number of objects, the number of reference relationships, and the number of operations.

The next mm lines each contain two positive integers xi,yix_i, y_i, describing the two endpoints of the ii-th reference relationship.

The next qq lines are each in one of the following two forms. The ii-th line describes the operation at time ii:

  • The string DELETE and a positive integer xx, with the meaning described in Description.
  • The string GC, with the meaning described in Description.

The next line contains nn positive integers a1,a2,,ana_1, a_2, \dots, a_n, representing the memory size occupied by each object.

Output Format

Output one line with one integer, the answer as described in Description.

6 6 8
1 2
2 3
2 4
1 4
2 5
1 6
GC
DELETE 5
DELETE 3
GC
DELETE 1
GC
DELETE 2
GC
1 2 3 4 5 6

149

样例 2 见附件 garbage2.in
本组数据满足测试点 6 的限制。
样例 2 见附件 garbage2.ans
样例 3 见附件 garbage3.in
本组数据满足测试点 11 的限制。
样例 3 见附件 garbage3.ans

Hint

Sample 1 Explanation

At time 44, vertex 55 is deleted.

At time 66, vertices 2,32, 3 are deleted.

At time 99, vertices 1,4,61, 4, 6 are deleted.

So the answer is $5 \times 4 + (2 + 3) \times 6 + (1 + 4 + 6) \times 9 = 20 + 30 + 99 = 149$.

Constraints

For all testdata, 1n,m,q4×1051 \leq n, m, q \leq 4 \times 10^5, 1ai1081 \leq a_i \leq 10^8.

The detailed constraints are shown in the table below.

Test Point ID nn mm qq Special Restriction
121 \sim 2 500\leq 500
353 \sim 5 3000\leq 3000
6106 \sim 10 5000\leq 5000
111411 \sim 14 2×105\leq 2 \times 10^5 n1n-1 2×105\leq 2 \times 10^5 It is guaranteed that the initial graph is a tree.
151615 \sim 16 2×105\leq 2 \times 10^5
172017 \sim 20 4×105\leq 4 \times 10^5

Translated by ChatGPT 5