Farmer John operates a collection of NN farms (1≤N≤1051≤N≤105), conveniently numbered 1…N1…N. Initially, there are no roads connecting these farms to each-other, and each farm is actively producing milk.
Due to the dynamic nature of the economy, Farmer John needs to make changes to his farms according to a series of QQ update operations (0≤Q≤2⋅1050≤Q≤2⋅105). Update operations come in three possible forms:
A farm xx that is actively producing milk, or that can reach another active farm via a series of roads, is called a "relevant" farm. For each farm xx, please calculate the maximum ii (0≤i≤Q0≤i≤Q) such that xx is relevant after the ii-th update.
The first line of input contains NN and QQ. The next QQ lines each contain an update of one of the following forms:
D x A x y R e
It is guaranteed that for updates of type R, ee is at most the number of roads that have been added so far, and no two updates of type R have the same value of ee.
Please output NN lines, each containing an integer in the range 0…Q0…Q.
5 9 A 1 2 A 2 3 D 1 D 3 A 2 4 D 2 R 2 R 1 R 3
7 8 6 9 9
In this example, roads are removed in the order (2,3),(1,2),(2,4)(2,3),(1,2),(2,4).
Problem credits: Benjamin Qi
[/hide]
(Analysis by Benjamin Qi)
The idea is to process the updates in reverse order and maintain which farms are relevant. Note that once a farm becomes relevant, it never becomes irrelevant due to the assumption that roads are only added between active farms.
For updates of type R, add the ee-th edge to the graph. If exactly one endpoint of this edge was previously relevant, mark all vertices in the connected component of the other endpoint as relevant as well.
For updates of type D, if vertex xx was not previously relevant, then mark every vertex in its connected component as relevant.
For updates of type A, do nothing.
We can process these operations in O(N+Q)O(N+Q) time.
Danny Mittal's code:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class GarbageCollection { static List<Integer>[] adj = null; static int[] answers; // marks everything in the connected component of a // as relevant static void dfs(int a, int time) { if (answers[a] == 0) { answers[a] = time; for (int b : adj[a]) { dfs(b, time); } } } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer tokenizer = new StringTokenizer(in.readLine()); int n = Integer.parseInt(tokenizer.nextToken()); int q = Integer.parseInt(tokenizer.nextToken()); adj = new List[n + 1]; for (int a = 1; a <= n; a++) { adj[a] = new ArrayList<>(); } answers = new int[n + 1]; List<Edge> added = new ArrayList<>(); Edge[] removed = new Edge[q]; int[] deactivated = new int[q]; Set<Integer> alwaysActive = new HashSet<>(); for (int a = 1; a <= n; a++) { alwaysActive.add(a); } for (int j = 0; j < q; j++) { tokenizer = new StringTokenizer(in.readLine()); char type = tokenizer.nextToken().charAt(0); if (type == 'D') { int a = Integer.parseInt(tokenizer.nextToken()); deactivated[j] = a; alwaysActive.remove(a); } else if (type == 'A') { int a = Integer.parseInt(tokenizer.nextToken()); int b = Integer.parseInt(tokenizer.nextToken()); added.add(new Edge(a, b)); } else { int k = Integer.parseInt(tokenizer.nextToken()); removed[j] = added.get(k - 1); added.set(k - 1, null); } } for (Edge edge : added) { if (edge != null) { adj[edge.from].add(edge.to); adj[edge.to].add(edge.from); } } for (int a : alwaysActive) { dfs(a, q); } for (int j = q - 1; j > 0; j--) { if (deactivated[j] != 0) { dfs(deactivated[j], j); } else if (removed[j] != null) { int a = removed[j].from; int b = removed[j].to; adj[a].add(b); adj[b].add(a); if (answers[a] != 0 || answers[b] != 0) { dfs(a, j); dfs(b, j); } } } StringBuilder out = new StringBuilder(); for (int a = 1; a <= n; a++) { out.append(answers[a]).append('\n'); } System.out.print(out); } static class Edge { final int from; final int to; Edge(int from, int to) { this.from = from; this.to = to; } } }
[/hide]
翰林课程体验,退费流程快速投诉邮箱: yuxi@linstitute.net 沪ICP备2023009024号-1