There are n persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of m days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
Either this person does not go on the trip,
Or at least k of his friends also go on the trip.
Note that the friendship is not transitive. That is, if a and b are friends and b and c are friends, it does not necessarily imply that a and c are friends.
For each day, find the maximum number of people that can go on the trip on that day.
Input
The first line contains three integers n, m, and k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ m ≤ 2 ⋅ 10^5, 1 ≤ k < n) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The i-th (1 ≤ i ≤ m) of the next m lines contains two integers x and y (1≤ x, y≤ n, x≠ y), meaning that persons x and y become friends on the morning of day i. It is guaranteed that x and y were not friends before.
Output
Print exactly m lines, where the i-th of them (1≤ i≤ m) contains the maximum number of people that can go on the trip on the evening of the day i.
Examples
Input
4 4 2
2 3
1 2
1 3
1 4
Output
0
0
3
3
Input
5 8 2
2 1
4 2
5 4
5 2
4 3
5 1
4 1
3 2
Output
0
0
0
3
3
4
4
5
Input
5 7 2
1 5
3 2
2 5
3 4
1 2
5 3
1 3
Output
0
0
0
0
3
4
4
Note
In the first example,
1,2,3 can go on day 3 and 4.
In the second example,
2,4,5 can go on day 4 and 5.
1,2,4,5 can go on day 6 and 7.
1,2,3,4,5 can go on day 8.
In the third example,
1,2,5 can go on day 5.
1,2,3,5 can go on day 6 and 7.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to process a sequence of edge additions (friendships) to an undirected graph with n vertices. After each addition, we need to find the maximum size of a subset S of vertices such that for every vertex v in S, deg_S(v) >= k, i.e., v has at least k friends within S.
Essentially, we need the largest possible induced subgraph where each vertex has degree at least k within that subgraph. This is known as the k-core problem: the k-core of a graph is the maximal induced subgraph where each vertex has degree at least k. The size of the k-core can be found by repeatedly removing vertices with degree < k. The k-core is unique and can be computed via a peeling process.
Thus, after each edge addition, we need the size of the k-core. But we have dynamic edge additions only (no deletions). So we need to maintain the size of the k-core as edges are added.
The graph is initially empty (no edges). Initially, no vertex has degree >= k? Actually k >= 1, but with no edges, all vertices have degree 0 < k. So the k-core size is 0. As edges are added, vertices may accumulate enough neighbors within the core.
We need to output after each addition the size of the k-core.
This is a classic problem: dynamic maintenance of k-core under edge insertions. Since there are only insertions, we can maintain a data structure to quickly update the core. However, straightforward recomputation each time would be too slow (O(n+m) per query). But we can process offline: since all edges are added sequentially and we only need the final core after each addition, we can reverse the process: consider the final graph after all m edges, compute its k-core. Then process deletions in reverse order (i.e., starting from full graph and removing edges). For each state, we need the core size. Because deletions are harder? Actually, we can maintain the k-core under edge deletions efficiently using a degree threshold algorithm? But we can reverse: The problem is we have insertions, and we need core size after each insertion. If we process from final graph backwards, each step removes an edge. If we can maintain the k-core under edge deletions (or equivalently, we can recompute the core after each deletion by propagating deletions only if vertices drop below k). Since deletions can cause cascading removals, but we can handle it similarly to insertions: maintain for each vertex its current degree in the current graph and also its degree within the current core? Actually, it's easier: maintain the k-core in a dynamic graph with edge deletions. We can use a queue to peel vertices whose degree < k, and maintain a set of active vertices (in the core). When an edge is removed, the degrees of its endpoints decrease. If either endpoint's degree becomes < k and it is still in the core, we remove it and propagate. This is similar to maintaining the k-core under deletions.
Thus, we can compute the core for the final graph (with all m edges), then process edges in reverse order: for i = m down to 1, we output current core size, then remove the edge of that day. However, we need outputs in forward order. So we can store the results and then print in forward order.
But careful: In forward order, after day i, the graph has edges 1..i. In reverse, we start at state after all m edges (i = m). We then output for i = m, then remove edge m to get state after m-1 edges, output, etc. So after processing all reverse steps, we will have outputs for each day from m down to 1. We then reverse the outputs or store in array ans[i] = core size after processing day i (forward). So in reverse loop: for i = m down to 1, set ans[i] = current core size, then remove edge i.
Now we need to implement the dynamic maintenance of k-core under edge deletions efficiently.
We have up to n,m = 2e5. So O((n+m) log n) or near linear is fine.
The algorithm: maintain for each vertex its degree (in the current whole graph, not just core). But for core maintenance, we need to know which vertices are currently in the core. Initially, after computing core for the full graph, we have a set of vertices that are in the k-core, and we also need for each vertex its "effective degree" within the core? Actually, the peeling process for core uses the degree of each vertex in the current graph (the whole graph) but repeatedly removes vertices with degree < k. The order of removal doesn't matter; the remaining set is the maximal induced subgraph with min degree >= k. That set is unique. So if we start with the full graph and then remove edges, the core may shrink.
We need to maintain the core set after each deletion. For each vertex, we maintain its current degree in the full graph (since we are removing edges, degree decreases). However, the core condition depends on degrees in the induced subgraph of the core, which is essentially the degree of the vertex among neighbors that are also in the core. But we can compute core by a process similar to BFS: we maintain for each vertex its "current degree" (within the current active graph). Initially, we set active = all vertices, and compute degrees from the whole graph. Then we repeatedly remove vertices with degree < k, decreasing their neighbors' degrees. This is the standard k-core computation. After we've done it, we have the core set. Now, when we delete an edge, we need to update. Approach: Use a data structure to keep track of vertices that may violate the degree condition after deletion. We'll maintain for each vertex its "core degree" which is the number of neighbors that are currently in the core (i.e., active). Initially, after core computation, for each vertex in core, we have core_degree >= k; for vertices not in core, we don't care. Then, when we remove an edge (u,v), we consider the effect if both u and v are in the core. If either is not in core, removing the edge may not affect the core? Actually, if a vertex is not in the core, it's already removed, and its neighbors' core degrees only consider core neighbors. Since the vertex is not in core, it does not contribute to core degrees of its neighbors. So if either u or v is not in core, the removal of this edge does not affect core degrees of core vertices (since the edge was not counted because the non-core vertex is not active). However, could the removal cause a core vertex to later become non-core? Only edges among core vertices matter. So if at least one endpoint is not in core, the core remains unchanged. But we must be careful: Could an edge whose one endpoint is not in core have allowed that endpoint to later become part of core if we added edges? But we are deleting edges, so that endpoint won't become part of core anyway. So indeed, if either endpoint is not in the core, the core remains unchanged.
If both u and v are in the core, then we decrement their core_degree by 1 each. After decrement, if some vertex's core_degree becomes < k, we need to remove it from the core. That removal may trigger further removals: when we remove a vertex w from the core, we decrease the core_degree of all its neighbors that are still in the core by 1 (since w is no longer a core neighbor). This may cause those neighbors to fall below k, and so on.
Thus, we can implement a BFS/queue process. For each vertex, we store a flag indicating if it's in the core (active). Also, we store its current core degree (i.e., number of active neighbors). Initially, after computing core for the full graph, we set active for those in core, and compute core_degree for each vertex (the number of neighbors that are also active). Note that for vertices not in core, core_degree is irrelevant; they are inactive.
When we remove an edge (u,v), we first check if both u and v are active. If not, nothing happens. If both active, we decrement core_degree[u] and core_degree[v]. Then if core_degree[u] < k, we push u to queue for removal. Similarly for v. Then process queue: while queue not empty, pop vertex x, if it is already inactive, skip; else mark inactive, and for each neighbor y of x that is active, decrement core_degree[y] by 1, and if core_degree[y] < k, push y. This will propagate.
But careful: When we compute initial core for the full graph, we might have used a peeling process that removed vertices. That gives us a set of active vertices and core_degrees. However, when we then start deleting edges, we must ensure that our core_degrees are correct with respect to the current active set. They will be if we initialize correctly.
Implementation details: Graph representation: we need adjacency lists to iterate over neighbors efficiently. Since we are only deleting edges, we can store edges in a vector and also adjacency lists as sets or linked lists with deletion. But note that during the process we need to iterate over neighbors of a vertex when it is removed from core (to decrement core_degree of active neighbors). But we only need to consider neighbors that are currently active. However, we have to know which neighbors are active. If we iterate over all neighbors and check if active, that could be O(degree) per removal, which is fine overall because each edge is considered at most a few times? Let's analyze: Each time a vertex is removed from core, we iterate over all its neighbors. In the worst case, a vertex might be removed many times? But each vertex is removed at most once from the core during the whole process (once it becomes inactive, it stays inactive because we are only deleting edges). So total work over all deletions is sum of degrees of vertices that ever become inactive (i.e., those removed from core at some point). Since each vertex can be removed at most once, and each edge is incident to two vertices, each edge could be examined when either endpoint is removed. However, note that when we iterate over neighbors of a removed vertex, we check if the neighbor is active; we might examine edges where the neighbor is inactive (so we do a check but no further action). That still might be O(m) per removal? Actually, each edge can be examined multiple times if both endpoints are removed at different times? Suppose vertex u is removed; we examine all its neighbors, including v. If v is still active, we decrement core_degree[v]. Later, if v is also removed, we will examine its neighbors, including u (which is already inactive). So each edge can be examined up to twice (once from each endpoint) over the whole process. However, note that there may be many removals? But each vertex is removed at most once, so total neighbor iterations is sum over all vertices that are removed of degree(vertex). That sum could be O(n * avg degree) = O(m) because sum of degrees = 2*m. However, careful: The degree of a vertex is its total degree in the graph (including edges that might have been removed earlier?). But we are iterating over its adjacency list which contains all edges ever present (since we only delete edges, but the adjacency list still contains the edge; we need to know if that edge is still present? Actually, when we delete an edge, we physically remove it from the graph. In our process, we are deleting edges one by one. So the graph changes. So if we store static adjacency lists of all edges ever added, that's not accurate after deletions because some edges are removed. When we process a vertex's neighbors for core removal, we need to consider only current edges (those not yet deleted). Therefore, we must have a dynamic graph representation that supports edge deletions. For efficiency, we could store adjacency lists as sets or linked lists with O(1) deletions? Since m up to 2e5, we could store edges in vectors and also maintain for each vertex a list of its incident edges with pointers, but deletion from vector is O(degree) if we remove by scanning. That would be too heavy.
Alternative approach: Process deletions in reverse order: we start from the full graph (all edges present) and then delete edges one by one. So we can pre-build adjacency lists containing all edges that will ever exist. As we delete edges, we need to mark them as removed. When iterating over neighbors of a vertex, we need to know if an edge is still present. We can store for each edge a boolean indicating if it's still alive. When we iterate over adjacency list of a vertex, we skip edges that are marked dead. That way, we don't need to physically delete them. The adjacency list may contain many dead edges over time, but each edge becomes dead exactly once and stays dead. When we iterate over neighbors, we may traverse dead edges as well, which could cause O(total number of neighbor checks) to blow up because for each vertex removal, we might scan its entire adjacency list (including dead edges) each time? But we only remove each vertex once; we will scan its entire adjacency list exactly once when it is removed from core (or possibly earlier when we process edge deletions? Let's think.)
Actually, the only times we need to iterate over neighbors of a vertex are:
When an edge is deleted and both endpoints are active: we decrement core_degree of both. That's O(1) per edge deletion (no iteration over all neighbors). So that's fine.
When a vertex is removed from the core (due to falling below k), we need to iterate over all its current neighbors (i.e., edges that are still alive) to decrement core_degree of those neighbors that are active. For this, we need to traverse all edges incident to the vertex that are still alive. But as we process deletions, edges are being removed over time. However, a vertex can be removed from the core only after some deletions have happened, and at that time, some of its incident edges may already be deleted (dead). So we need to traverse its adjacency list, but we can skip dead edges. If we simply iterate over the entire adjacency list (which includes all edges ever incident), and check alive status for each, that would be O(degree of vertex) per removal. That's okay because sum of degrees over all vertices is 2m, and each vertex is removed at most once, so total checks = sum of degrees over vertices that are removed. However, note that vertices that remain in the core until the end are never removed; we never need to iterate over their full adjacency list for removal. So the total work is at most 2m (since each edge appears in the adjacency lists of two vertices, but if both vertices are removed, we traverse that edge twice? Actually, when we remove a vertex, we iterate over its adjacency list; for each edge, we check if it's alive, and if neighbor is active, we decrement. Even if the edge is dead (already removed), we skip it quickly. So the number of edge-checks across all vertex removals is total number of adjacency entries for vertices that are removed. That's at most sum_{v removed} deg(v). This sum could be as large as O(nm) in worst case if many vertices have high degree? But deg(v) is at most n-1, but m is up to 2e5, so total sum of all degrees = 2m = up to 4e5. So sum_{v removed} deg(v) ≤ 2m = 4e5, because every edge belongs to two vertices, and if both vertices are removed, that edge's adjacency entries are counted twice. So total adjacency entries across all vertices is 2m, regardless of which vertices are removed. Actually, careful: The sum of degrees over all vertices is exactly 2m. So sum_{v removed} deg(v) cannot exceed 2m, because it's a sum over a subset of vertices of their degrees, and degrees are non-negative. But is it possible that vertices not removed have high degree and removed vertices have low degree, so sum_{v removed} deg(v) ≤ 2m. Actually, 2m is total over all vertices; sum_{v removed} deg(v) ≤ 2m. So total adjacency list traversals across all vertex removals is at most 2m. That's fine.
But what about dead edges? They are still part of deg(v) because deg(v) originally is the total number of edges ever incident; after deletion, the alive edges are fewer. However, when we traverse adjacency list, we will check each incident edge; some of them may be dead. The check is constant time. So total work is O(m) for vertex removals. Also, for each edge deletion, we only do O(1) work unless both endpoints are active, then we decrement degrees and maybe push to queue. So overall O(m + n) if we manage queues properly.
Thus, we can implement with static adjacency lists (vector of incident edges) and a boolean alive per edge. Also, for each vertex, we maintain its current core_degree and active flag.
Now, we need to compute the initial k-core for the full graph (after all m edges). That's a standard linear-time process: compute degree of each vertex (total number of alive edges). Initially all edges are alive. Then we perform a peeling: use a queue, initially push all vertices with degree < k. Then while queue not empty, pop vertex v, mark it as inactive (if not already), and for each neighbor u of v, if u is still active (i.e., not yet removed), decrement degree[u] (since we remove v), and if degree[u] < k and u not already in queue, push u. The degree values we use are the current effective degrees in the remaining subgraph. At the end, active vertices are those not removed; their degree (the number of neighbors also active) is at least k. This yields the k-core.
We can store the degree array used in peeling as 'deg' which initially is the total degree of each vertex (alive edges count). After peeling, we have active array. Also we need core_degree for each vertex for later dynamic updates. But we can reuse the deg array after peeling? The deg array after peeling will represent the degree in the final core? Actually, during peeling we keep decrementing degrees, and the final deg for active vertices is the number of neighbors also active, which is exactly the core_degree we need for those vertices. So we can set core_degree = deg after peeling for active vertices; for inactive vertices, we don't care. So we can compute initial state easily.
But careful: In dynamic deletions, we will maintain core_degree for active vertices as the number of active neighbors. That matches the deg after peeling.
Thus, algorithm steps:
Read n, m, k.
Store edges list: for i=1..m, read x_i, y_i (1-indexed). Add to edges array.
Build adjacency lists: for each vertex, a vector of edge indices incident to it. Also store opposite endpoint for each edge.
Initialize alive array for edges: initially all true (since we start from full graph).
Compute initial degrees: deg[v] = number of incident edges (size of adjacency list) because all edges alive.
Compute k-core for full graph:
active[v] initially true for all.
queue q.
for v in 1..n: if deg[v] < k, push v to q, and set active[v] = false? Actually, we need to mark as removed when popped? Standard: push all vertices with degree < k initially. Then process:
while q not empty:
v = q.front; q.pop();
if (!active[v]) continue; // maybe already removed? Actually we set active to false when we process.
But we need to ensure we don't double remove.
Actually typical algorithm: we maintain an array deg_current. Initially deg_current = degree. We have a visited flag to indicate if already removed. For each vertex, if deg_current[v] < k, we push into queue. When we pop v, we mark removed; for each neighbor u of v that is not removed, we decrement deg_current[u]; if deg_current[u] < k, we push u. At the end, active = not removed.
So we can do:
vector cur_deg = deg (copy)
vector removed(n+1, false);
queue q;
for v=1..n if cur_deg[v] < k: q.push(v);
while not empty:
v = q.front(); q.pop();
if (removed[v]) continue;
removed[v] = true;
for each edge index e incident to v:
if (!alive[e]) continue; // edge might be alive? Actually all alive initially.
u = other endpoint
if (!removed[u]) {
cur_deg[u]--;
if (cur_deg[u] < k) q.push(u);
}
After loop, active[v] = !removed[v]; core_degree[v] = cur_deg[v] (which now is number of neighbors not removed, i.e., active neighbors).
Set active flag and core_degree. Also we might maintain a count of active vertices: core_size.
Now we process deletions in reverse order (i = m down to 1):
ans[i] = current core_size.
Remove edge i: let (x,y) = edges[i].
Set alive[e] = false.
But we need to update degrees? Actually, we are maintaining core_degree for active vertices. For vertices that are active, if both are active, we need to decrement core_degree of both, because the edge is removed from the graph, and since both are active, this edge contributed to their core_degree. If one or both are inactive, we don't need to change core_degree because the edge wasn't counting for core (since at least one endpoint inactive, the edge is not within the core induced subgraph). But wait, we must be careful: The core is defined on the current graph. In our dynamic process, we maintain active set and core_degree such that for each active vertex, core_degree[v] is the number of active neighbors (i.e., neighbors that are also active). This condition holds if we correctly update when edges are removed and when vertices are removed.
So for edge deletion:
if active[x] and active[y]:
core_degree[x]--;
core_degree[y]--;
if core_degree[x] < k: push x to queue (if not already)
if core_degree[y] < k: push y to queue (if not already)
Then process queue to remove vertices and propagate:
while queue not empty:
v = queue.front; queue.pop();
if (!active[v]) continue;
active[v] = false; core_size--;
// iterate over all neighbors u of v via edges that are alive:
for each edge index e incident to v:
if (!alive[e]) continue; // edge already removed
u = other endpoint
if (active[u]):
core_degree[u]--;
if (core_degree[u] < k) queue.push(u);
Note: When we remove a vertex, we also need to consider edges that are incident to it. But these edges remain in the graph? Actually, the edge is still present in the graph? In the deletion process, we have just removed a particular edge (the one of day i). The other edges remain alive. However, when we remove a vertex from the core, we conceptually "exclude" it from the active set. But the graph still has edges incident to that vertex, but those edges are not considered part of the core because the vertex is not active. For the purpose of core degrees of other vertices, we need to decrement core_degree for active neighbors because the edge (v,u) was previously counted as a connection between two active vertices. Now that v becomes inactive, for each active neighbor u, we must decrement core_degree[u]. That's what we do by iterating over all alive edges incident to v. However, note that some of those edges might have been already removed (dead) from the graph. But if an edge is dead, it was already not counted in core_degree because it's not present. So we only consider alive edges. In our loop, we skip dead edges.
Also, when we remove vertex v, we should not mark its edges as dead; they are still present in the graph but v is inactive, so they won't affect future core computations as long as we keep track of active status.
This process is correct.
One nuance: When we decrement core_degree due to edge removal, we may push vertices into queue. However, we might also push vertices that are already in the queue or already inactive. We'll handle by checking active when popping.
Also, we need to ensure that we don't double decrement core_degree when an edge is removed and then later one endpoint is removed: In the edge removal step, if both endpoints are active, we decrement core_degree. Later, if we remove one endpoint, we will iterate over its incident edges (including this removed one) and attempt to decrement core_degree of neighbor if neighbor is active. But note: For this particular edge, after we removed it, it is dead (alive=false). In the vertex removal loop, we skip dead edges. So we won't double decrement. Good.
But wait: In the edge removal step, we set alive[e]=false. Then in the vertex removal loop, we skip dead edges. So the decrement due to edge removal is separate from decrement due to vertex removal. That's correct.
However, there is a subtlety: Suppose both endpoints are active, we decrement core_degree for both and then possibly one of them gets removed immediately (or later). When we remove that vertex, we will iterate over all alive edges incident, but this edge is now dead, so we skip it. That's fine because the edge removal already accounted for its removal from core degrees; we don't want to count it again. Good.
Another nuance: When we remove a vertex, we also need to consider that some of its incident edges might have been removed earlier (dead). That's okay because we skip dead edges.
So algorithm is sound.
Now we need to compute initial core_size correctly. Also we need to handle that initially after full graph, there may be vertices with core_degree >= k; that's fine.
Edge Cases: k could be 1? Yes, 1 ≤ k < n. For k=1, the 1-core is the set of all vertices with degree at least 1 in the induced subgraph, which essentially is the whole graph minus isolated vertices? Actually, 1-core includes all vertices that are not isolated after considering the core? Wait, the k-core definition: the maximal induced subgraph where each vertex has degree at least k. For k=1, the 1-core is the graph formed by repeatedly removing isolated vertices (degree 0). So it's the set of vertices that are not isolated in the current graph after iterative removal. That's fine.
Now, we must ensure that the initial core computation (peeling) uses the full graph with all edges alive. We'll implement that with a queue and a copy of degrees. Complexity O(n+m).
But one more thing: The graph initially might not be connected etc.
Full graph edges: (2,3), (1,2), (1,3), (1,4). Compute 2-core.
Degrees: deg(1)=3, deg(2)=2, deg(3)=2, deg(4)=1.
Remove vertices with degree <2: vertex 4 (deg 1). Remove 4, decrement neighbors: neighbor 1 degree becomes 2 (from 3). Now degrees: 1:2, 2:2, 3:2. No more <2. So core = {1,2,3}, size=3.
Now reverse:
i=4: ans[4]=3. Remove edge 4: (1,4). Both endpoints? 1 is active, 4 is inactive. So no change. core_size stays 3.
i=3: ans[3]=3. Remove edge 3: (1,3). Both active? Yes, 1 and 3 active. core_degree[1]-- (from 2 to 1), core_degree[3]-- (from 2 to 1). Now core_degree[1]=1<2, push 1; core_degree[3]=1<2, push 3. Process queue: say pop 1, active[1]=false, core_size-- =>2. For each neighbor via alive edges: neighbors: 2 (via edge2 alive), 3 (via edge3 alive? Actually edge3 is just removed, but we set alive[3]=false, so we skip it), 4 (edge4 dead). So only neighbor 2 is active and edge alive. Decrement core_degree[2] from 2 to 1. Now core_degree[2]=1<2, push 2. Next pop 3: active[3]=false (already false? Actually we need to check if still active; we just popped 3, but we must ensure we don't double process. We push 3 after edge removal, then when processing queue we pop 1, then after processing 1, we might push 2, then next pop 3. When we pop 3, we see active[3] is still true? Actually we haven't yet set active[3]=false because we haven't processed 3 yet. So we set active[3]=false, core_size-- =>1. Iterate neighbors of 3: neighbors: 2 (via edge1 alive), 1 (edge3 dead). So neighbor 2 is active, core_degree[2]-- from 1 to 0. Now core_degree[2]=0<2, push 2 (but already in queue, maybe). Then pop 2: active[2] is still true? Actually after processing 3, we might push 2 again, but when we pop 2, we check active[2] and it's true, so set active[2]=false, core_size-- =>0. Now all are inactive. So after removal, core_size=0. That's correct for state after edges 1 and 2 only? Actually we are at step i=3 removal: after removing edge 3, we get state with edges 1,2,4? Wait, careful: Reverse order: we start with all edges present: edges 1,2,3,4. After i=4 removal, we remove edge4, remaining edges: 1,2,3. So after i=4 removal, we output 3. Then i=3 removal: we remove edge3, remaining edges: 1,2. That state should have core size? Let's compute for edges (2,3) and (1,2). Graph: vertices 1-2-3, with edge 1-2 and 2-3. Degrees: 1:1,2:2,3:1. 2-core: remove vertices with degree<2: 1 and 3 have degree1, remove them, then 2's degree becomes 0, so remove 2. So core size 0. So ans[3] should be 0. But our algorithm gave ans[3]=3 before removal, then after removal we updated core_size to 0, and then we set ans[2] after next step? Actually, we output ans[i] before removal. For i=3, we output current core_size, which is 3 (since before removal, edges are 1,2,3,4? Wait careful: In reverse loop, at start (i=m) we have full graph (edges 1..4). We output ans[4]=3. Then we remove edge 4, update core to still 3. Now we are at i=3, we output current core_size, which is still 3 (since after removal of edge4, core remains 3). Then we remove edge3 and update core to 0. Then i=2: output current core_size (0), remove edge2? Actually after i=3 removal, graph has edges 1,2. So core_size=0. Then i=2: output ans[2]=0, then remove edge2? Edge2 is (1,2). After removal, graph has only edge1 (2,3). Compute? Let's continue: after removing edge2, graph has edge1 only: (2,3). Degrees: 2:1,3:1, others 0. 2-core: remove both, size 0. So core remains 0. Then i=1: output ans[1]=0, remove edge1, graph empty, core stays 0. So ans array becomes: ans[1]=0, ans[2]=0, ans[3]=3, ans[4]=3. That matches example output: 0,0,3,3 (day1,2,3,4). Wait output order is day1..4: 0 0 3 3. So yes ans[1]=0, ans[2]=0, ans[3]=3, ans[4]=3. Good.
Full graph after all edges:
Let's list edges:
1: (2,1)
2: (4,2)
3: (5,4)
4: (5,2)
5: (4,3)
6: (5,1)
7: (4,1)
8: (3,2)
So vertices: 1 connects to 2,5,4 => deg3
2 connects to 1,4,5,3 => deg4
3 connects to 4,2 => deg2
4 connects to 2,5,3,1 => deg4
5 connects to 4,2,1 => deg3
All degrees >=2? 3 has deg2, so all >=2. So 2-core initially includes all 5 vertices? But we must check: after peeling, do we remove any? All initial degrees >=2, so no vertex with degree<2, so core includes all 5. Indeed output for day8 is 5 (as per example). So core_size=5 initially.
Now reverse steps:
i=8: ans[8]=5, remove edge8: (3,2). Both active? Yes. So core_degree[3]-- (from 2 to 1), core_degree[2]-- (from 4 to 3). Check core_degree[3]=1<2 => push 3. Process: pop 3, remove: active[3]=false, core_size=4. For each neighbor of 3 via alive edges: neighbors: 4 (edge5 alive), 2 (edge8 just removed, dead). So only neighbor 4 is active, core_degree[4]-- from 4 to 3. 3 still >=2? core_degree[4]=3>=2, so no push. End. Now active: 1,2,4,5. Check core_degrees: we need to recalc? After removal, we have: edges except 8. Let's compute manually: without edge8, graph: edges 1,2,3,4,5,6,7. Degrees within core among active set: we need to ensure all active have >=2 neighbors among active. Vertex 1 neighbors: 2 (active), 5 (active), 4 (active) => deg 3 >=2. Vertex 2 neighbors: 1 (active), 4 (active), 5 (active) => deg3. Vertex 4 neighbors: 2,5,3? 3 inactive, so neighbors: 2,5,1 => deg3. Vertex 5 neighbors: 4,2,1 => deg3. So core remains {1,2,4,5} size 4. So after removal edge8, core_size=4. That matches step: after day7? Wait we are at reverse step: after removing edge8, we are at state with edges 1..7, which corresponds to day7 (after 7 additions). The example says day7 output = 4. So ans[7]=? In reverse, we output ans[8] before removal, then after removal we have core size for state after 7 edges. Then at i=7, we output ans[7] = current core size = 4. That matches.
Proceed similarly: i=7: ans[7]=4, remove edge7: (4,1). Both active? Yes (4 and 1). core_degree[4]-- (3->2), core_degree[1]-- (3->2). Both still >=2? core_degree[4]=2, core_degree[1]=2, so no pushes. core_size stays 4. So state after edges 1..6? Actually after removal edge7, graph has edges 1..6. According to example, day6 output is 4. So ans[6] should be 4, which will be output at i=6 step before removal. So after removal edge7, core_size remains 4. Then i=6: ans[6]=4, remove edge6: (5,1). Both active? Yes. core_degree[5]-- (3->2), core_degree[1]-- (2->1). Now core_degree[1]=1<2 => push 1. Process: remove 1: active[1]=false, core_size=3. For each neighbor of 1 via alive edges: neighbors: 2 (edge1 alive), 5 (edge6 just removed dead), 4 (edge7 removed dead). So only neighbor 2 active, core_degree[2]-- from 3 to 2. 2 still >=2. Next, after removal, check if others? Now active {2,4,5}. Check core_degrees: 2 neighbors: 1(inactive),4(active),5(active) => deg2? Actually edges: 2-4 (edge2 alive), 2-5 (edge4 alive). So core_degree[2]=2. 4 neighbors: 2(active),5(active),3(inactive),1(inactive) => so core_degree[4]=2 (edges 2 and 5). 5 neighbors: 4(active),2(active),1(inactive) => core_degree[5]=2. So all >=2. core_size=3. That matches example: day5 output = 3? Actually example output: day1 0, day2 0, day3 0, day4 3, day5 3, day6 4, day7 4, day8 5. Wait day5 output is 3. After removal edge6, we are at state after edges 1..5. That should be day5 state? Actually day6 removal in reverse corresponds to day6 output before removal (which is for state with edges 1..6). So we output ans[6]=4, then remove edge6 to get state with edges 1..5, which should have core size 3. So after i=6 removal, core_size becomes 3. Next i=5: ans[5]=3 (output for day5), remove edge5: (4,3). Both active? 4 active, 3 inactive (since removed earlier). So no change. core_size remains 3. Good.
i=4: ans[4]=3, remove edge4: (5,2). Both active (5 and 2). core_degree[5]-- (2->1), core_degree[2]-- (2->1). Both <2, push both. Process: maybe remove 5 and 2. Let's simulate: after decrement, core_degree[5]=1, core_degree[2]=1. Queue contains 5 and 2. Pop 5: remove active[5]=false, core_size=2. For each neighbor of 5 via alive edges: neighbors: 4 (edge3 alive), 2 (edge4 just removed dead), 1 (edge6 dead). So only neighbor 4 active, core_degree[4]-- from 2 to 1 (<2), push 4. Pop 2: remove active[2]=false, core_size=1. For neighbors of 2: 1 (edge1 alive? Actually edge1 is (2,1) still alive), 4 (edge2 alive), 5 (edge4 dead), 3 (edge8 dead). So neighbors: 1 inactive? 1 is inactive (removed earlier), 4 active. So core_degree[4]-- from 1 to 0 (<2), push 4 again (but already in queue). Then pop 4: remove active[4]=false, core_size=0. neighbors: 2 (inactive), 5 (inactive), 3 (inactive), 1 (inactive). No active neighbors. So final core_size=0. But example says day4 output is 3 and day3 output is 0? Actually example output: day4 = 3, day3 = 0. So after removal edge4, state with edges 1,2,3,5? Wait edges after removal of edge4 (i=4) from state after previous removal (which had edges 1,2,3,5? Let's see sequence:
We started full graph: edges 1,2,3,4,5,6,7,8.
After i=8 removal: edges 1-7.
After i=7 removal: edges 1-6.
After i=6 removal: edges 1-5.
After i=5 removal: edges 1-4? Actually i=5 removal is edge5 (4-3). That's removed, so remaining edges: 1,2,3,4? Wait edges: 1 (2-1), 2 (4-2), 3 (5-4), 4 (5-2). That's edges 1,2,3,4. So after i=5 removal, we have edges 1,2,3,4. According to example, day4 output = 3, day3 output = 0. So after i=5 removal, state corresponds to day4 (since after removal we are at state after 4 edges). At that state, core_size should be 3 (as per ans[4]=3). Indeed after i=5 removal we computed core_size=3? Wait we computed after i=5 removal core_size remained 3. That matches. Then i=4: we output ans[4] = 3 (for day4), then remove edge4 (5-2) to get state after edges 1,2,3 (since we remove edge4). That state should have core size 0 (day3 output 0). And after our i=4 removal processing, we got core_size=0. So that matches. So algorithm works.
Thus, the reverse deletion method is correct.
Now we need to implement:
Data structures:
edges: vector<pair<int,int>> of size m+1 (1-indexed for convenience).
adj: vector<vector> for each vertex, list of edge indices.
alive: vector of size m+1, initially true.
active: vector for vertices.
core_deg: vector for vertices (only meaningful for active vertices, but we can maintain for all).
current core_size: int.
Initial core computation:
Compute deg (total alive edges count) for each vertex: deg[v] = adj[v].size() (since all alive). Could also use a vector cur_deg = deg.
Use a queue q.
removed flag (or we can reuse active set later). We'll have active initially all true, but we will peel. Simpler: create vector cur_deg = deg; vector in_core(n+1, true); queue q; for v: if cur_deg[v] < k: q.push(v). Then while q not empty: v = q.front(); q.pop(); if (!in_core[v]) continue; in_core[v]=false; for each edge index e in adj[v]: if (!alive[e]) continue; (but alive all true initially) u = other; if (in_core[u]) { cur_deg[u]--; if (cur_deg[u] < k) q.push(u); }.
After that, core_deg = cur_deg for those still in_core; but note cur_deg for inactive vertices is whatever. So set core_deg = cur_deg; active = in_core; core_size = count of active.
But careful: When we later delete edges, we will need to know for each vertex its core_degree. But for vertices not active, we might still need to maintain core_degree? Not really because they will never become active again (only deletions, no insertions). However, there is a nuance: Could a vertex that was removed from core later re-enter the core after some edge deletions? No, because edge deletions only reduce degrees, so the condition for being in core can only become harder. So once a vertex is removed, it will never become active again. So we can just keep them inactive forever.
Thus, after initial core computation, we have active set and core_deg for active vertices is the number of active neighbors.
Now, we process deletions in reverse order.
Pseudo:
vector ans(m+1); // 1-indexed
for (int i = m; i >= 1; --i) {
ans[i] = core_size;
int u = edges[i].first, v = edges[i].second;
alive[i] = false;
code
if (active[u] && active[v]) {
core_deg[u]--;
core_deg[v]--;
if (core_deg[u] < k) q.push(u);
if (core_deg[v] < k) q.push(v);
while (!q.empty()) {
int x = q.front(); q.pop();
if (!active[x]) continue; // already removed
active[x] = false;
core_size--;
// for each neighbor y via edge e in adj[x] that is alive and active[y]
for (int e : adj[x]) {
if (!alive[e]) continue;
int y = (edges[e].first == x ? edges[e].second : edges[e].first);
if (active[y]) {
core_deg[y]--;
if (core_deg[y] < k) {
q.push(y);
}
}
}
}
}
}
After loop, we have ans[1..m]. Print ans[1..m] each on new line.
But wait, we must be careful about the queue: we used a queue for BFS removal. In the deletion step, we might push both u and v, and then process cascading removals. That's fine. However, we need to ensure that we don't push vertices that are already inactive. We check after popping but also before pushing? Possibly we can check when pushing: if active[x] and core_deg[x] < k, then push. But if it's already inactive, pushing is wasteful but we skip later. To avoid many pushes, we can check if active[x] before pushing, but it might be that we push a vertex that becomes inactive during processing from another path before we pop it. That's okay; we'll skip. Efficiency still O(total pushes) which is O(n). So fine.
Now we need to be careful about the initial core computation: we used a queue and removed vertices. But note that we also used the same alive array (all true). That's fine.
Now complexity: O(n + m) for initial core, and O(n + m) for deletions because each edge and vertex is processed a limited number of times. Specifically, each vertex can be removed at most once, and each edge can be considered in neighbor loops at most twice (once from each endpoint when that endpoint is removed) plus each edge is looked at during the initial core peeling (each edge considered at most twice as well). So total O(n+m).
Memory: adjacency lists with edge indices: for each vertex, vector of incident edges. That's O(m) total.
Now consider corner cases: k may be large, up to n-1. In such case, the core might be empty for a long time. Our algorithm still works.
Potential issues: The initial core computation may need to handle vertices with high degree but we are okay.
One nuance: In the initial core computation, we used cur_deg which is the degree in the full graph. Then we peel. At the end, we set core_deg = cur_deg for active vertices. However, note that for active vertices, cur_deg is the number of neighbors that are still active after peeling (since we only decremented for neighbors that were removed). That's correct.
But later, when we delete edges, we maintain core_deg as the number of active neighbors. However, we need to ensure that the adjacency lists are up-to-date with alive edges. In the deletion step, when we remove an edge, we set alive[e]=false. For active vertices, we decrement core_deg only if both were active. That's correct. When a vertex is removed, we iterate over its incident edges that are alive, and decrement core_deg of active neighbors. This correctly updates core_deg for those neighbors.
But is there a scenario where an edge is dead but we still consider it in core_deg? No, because we only decrement core_deg when the edge is removed (if both endpoints active) and we skip dead edges when iterating during vertex removal.
Now, what about edges that are removed after a vertex becomes inactive? If a vertex is inactive, we don't care about its core_deg. But when an edge is deleted and one endpoint is inactive, we do nothing. That's fine because that edge did not contribute to core_deg of the active endpoint? Actually, consider an edge (u,v) where u is active, v is inactive. Should core_deg[u] be decremented when the edge is deleted? In the definition of the core, only edges among active vertices matter. Since v is inactive, the edge (u,v) is not part of the induced subgraph on active vertices. Therefore, core_deg[u] should not count that edge. In our data structure, is core_deg[u] counting that edge? We need to ensure that core_deg[u] is defined as the number of active neighbors of u. So if v is inactive, the edge (u,v) should not be counted. When v becomes inactive, we should have already decremented core_deg[u] by 1 (during the removal of v). Indeed, when v was removed from core, we iterated over all its alive edges and decremented core_deg of active neighbors, including u. So after v is inactive, core_deg[u] no longer counts that edge. So later, when we delete the edge (u,v) from the graph, u is active and v is inactive, we skip because we only process if both active. This is correct because the edge was not contributing to core_deg[u] anyway. So core_deg remains correct.
Now, what about edges that are deleted before either endpoint becomes inactive? In that case, both are active, we decrement core_deg accordingly. That's correct.
Thus, the dynamic maintenance is consistent.
Now we must output answers in order of days 1..m. We'll store ans[i] when processing reverse: at the start of iteration i (i from m down to 1), we record the current core_size (which corresponds to the state after processing day i in forward order). Then we delete edge i to go to state after i-1 edges. At the end, we have ans[1..m] filled. We need to print each.
Edge case: After all deletions, core_size becomes 0 (since graph empty). That's fine.
Now, what about initialization: after initial core computation, we have core_size for full graph. That's ans[m] (since state after m edges). So we set ans[m] = core_size in the first iteration i=m before deleting edge m. That's correct.
Full graph edges:
(1,5), (3,2), (2,5), (3,4), (1,2), (5,3), (1,3)
Compute 2-core:
Degrees:
1: edges to 5,2,3 => deg3
2: edges to 3,5,1 => deg3
3: edges to 2,4,5,1 => deg4
4: edge to 3 => deg1
5: edges to 1,2,3 => deg3
Remove vertex 4 (deg1). After removal, decrement neighbor 3: deg3 becomes 3.
Now all others have deg >=2: 1:3,2:3,3:3,5:3. So core = {1,2,3,5} size 4.
Example output day7 = 4 (last line). Yes.
Now reverse:
i=7: ans[7]=4, remove edge7 (1,3). Both active: core_deg[1]-- (3->2), core_deg[3]-- (3->2). Both >=2, no push. So core remains {1,2,3,5} size 4. (State after edges 1..6 -> day6 output should be 4, matches)
i=6: ans[6]=4, remove edge6 (5,3). Both active: core_deg[5]-- (3->2), core_deg[3]-- (2->1). core_deg[3]=1<2 push 3. Process: remove 3: active[3]=false, core_size=3. Iterate neighbors of 3 via alive edges: edges: (3,2) alive? yes, (3,4) alive? yes but 4 inactive, (3,5) edge6 just removed (dead), (3,1) edge7 dead. So neighbor 2 is active (via edge2), core_deg[2]-- from 3 to 2. core_deg[2] still >=2. No further pushes. Now active {1,2,5}. Check core_degrees: 1 neighbors: 5 (alive), 2 (edge5 alive), 3 (dead). So core_deg[1]=2? Actually after removal of edge6 and edge7, and vertex3 removal, core_deg[1] was 2 after decrement from edge7, and not changed by vertex3 removal because edge (1,3) is dead? Actually when we removed vertex3, we iterated over its alive edges; edge (3,1) is dead (alive false because we removed edge7), so no effect. So core_deg[1]=2. 2 neighbors: 3 (inactive),5 (alive),1 (alive). So core_deg[2]=2. 5 neighbors: 1 (alive),2 (alive),3 (inactive). So core_deg[5]=2. So core size 3. (State after edges 1..5 -> day5 output should be 3, matches)
i=5: ans[5]=3, remove edge5 (1,2). Both active: core_deg[1]-- (2->1), core_deg[2]-- (2->1). Both <2, push 1,2. Process: remove 1: active[1]=false, core_size=2. neighbors of 1: edges: (1,5) alive, (1,2) dead, (1,3) dead. So neighbor 5 active, core_deg[5]-- from 2 to 1 (<2) push 5. Next pop 2: active[2]=false, core_size=1. neighbors of 2: (2,3) alive? edge2 (3,2) alive? yes, (2,5) alive, (2,1) dead. So neighbors: 3 inactive, 5 active. core_deg[5]-- from 1 to 0 (<2) push 5 again. Then pop 5: active[5]=false, core_size=0. So core empty. (State after edges 1..4 -> day4 output 0, matches)
i=4: ans[4]=0, remove edge4 (3,4). Both? 3 inactive, 4 inactive -> skip.
i=3: ans[3]=0, remove edge3 (2,5). 2 inactive,5 inactive -> skip.
i=2: ans[2]=0, remove edge2 (3,2). 3 inactive,2 inactive -> skip.
i=1: ans[1]=0, remove edge1 (1,5). 1 inactive,5 inactive -> skip.
So ans: [0,0,0,0,3,4,4] matches example.
Thus algorithm works.
Now, we need to be careful about the queue usage. In the deletion step, we use a queue for cascading removals. We need to declare it each iteration or reuse a global queue? We can declare a queue at each iteration, but that could be O(m) queue creations. Better to have a single queue and clear it each time? But we can just create a queue inside the loop; the total number of pushes across all deletions is O(n+m), so constructing new queues each iteration may cause some overhead but it's fine. However, we need to ensure that we process the queue until empty before next deletion, which we do.
One more nuance: In the initial core computation, we used a queue for peeling. That's fine.