We need to parse the problem: we have a graph on n vertices, with edges complement of given missing edges. That is, we are given a list of m non-edges (pairs that are not connected). All other pairs are edges (complete graph minus these non-edges). So the graph is dense if m is small, but n up to 200k, m up to 200k? Actually condition: 0 ≤ m ≤ min(200000, n(n-1)/2). So m can be up to 200k. The graph might be very dense (almost complete) but the complement (the missing edges graph) is sparse (since m is at most 200k). So we want connected components in the original graph (the complement of missing edges). Equivalent to find connected components in the complement graph? Wait careful: Original graph G = K_n \ set of non-edges. So it's the complement of the graph H where H has edges exactly the given m pairs (i.e., H is graph of missing edges). But note: in complete graph, complement of H is G. So G = complement of H (where H includes only the given non-edges). Since H is relatively sparse (m <= 200k), we can compute connected components of G efficiently using BFS/DFS on the complement? Typically, for dense graphs we can use a set of unvisited vertices and for each vertex explore its neighbors by considering the complement adjacency, i.e., we need to know which vertices are NOT directly connected (i.e., missing edges). To traverse G, from a vertex u, we want to go to all vertices v such that edge (u,v) exists in G, i.e., that are not in the non-edge list for u. Since the graph is dense, we can maintain a set of unvisited vertices. When we process u, we iterate over all v in the unvisited set that are adjacent to u in G. However, that's all v except those that are non-neighbors? Actually edge exists if (u,v) is NOT in the missing list. So for a given u, the set of vertices that are NOT directly reachable from u (i.e., missing edges) is small (at most deg_H(u) which can be up to n but H is sparse overall but some vertex might have high degree if many missing edges incident). m up to 200k, so total sum of degrees in H is 2m <= 400k. So average degree small, but could be some high-degree vertex if many missing edges concentrate, but still total missing edges 200k, so max degree could be up to 200k. So we need to handle that: a vertex may have many non-neighbors. But we want to find all v that are neighbors in G: that's all v not in missing list and v != u. So we can consider the set of all vertices not yet visited, and for each candidate v in unvisited set, we need to check if (u,v) is a missing edge; if not, then we can traverse. But we can't check each pair if unvisited set is large; we need to efficiently find all v in unvisited that are not in the missing neighbors of u.
Common technique: maintain a global set S of unvisited vertices (e.g., using a boolean array and a linked list or balanced BST). Then for each component start BFS/DFS: pick a vertex u, remove it, and then recursively explore its neighbors. To get neighbors of u in G, we need to iterate over all vertices v in S that are not in N_H(u) (the missing neighbors). Since S is the set of vertices we haven't visited yet, and we need to consider all v in S that are connected to u via an edge (i.e., not missing). That means we need to go through S and for each v, we check if (u,v) is a missing edge. But S can be large; however, we can instead iterate over the missing neighbors of u (which are relatively few), and realize that the vertices in S that are NOT in N_H(u) are exactly S \ (N_H(u) ∪ {u})? Actually neighbors in G are all vertices except u and vertices in N_H(u) (non-neighbors). So if we consider S, the vertices that are neighbors of u and still unvisited are S \ (N_H(u) ∪ {u})? But u is already removed from S when we start processing? We'll remove u initially. So we have S = set of unvisited vertices. The set of vertices to traverse from u is S \ N_H(u) (since u not in S). So we need to remove from S all vertices that are in N_H(u) and keep the rest; those rest are exactly the vertices we want to add to the queue/stack for the BFS. Because they are connected to u via an edge (since not missing). So we can implement: For each vertex u we process, we have list of its missing neighbors (non-edges). We'll iterate over all v in S that are not in N_H(u). But if we iterate over S directly, that's O(n) per u which is too slow. But we can instead iterate over the missing neighbors of u, and for each such v, if v is in S, we "skip" it (i.e., we do not add it), but the ones we want to add are those in S minus those missing neighbors. So if we had a way to remove from S all vertices that are not missing? Actually we need to retrieve S minus N_H(u). One way: iterate over all v in N_H(u), and for each v, mark that they are "blocked". But then we still need to get all other vertices from S. Another approach: iterate over all v in S, but that would be too large. However, note that total number of times we need to delete vertices from S equals n, because each vertex is removed exactly once when visited. So the total cost of iterating over S entries can be made linear if we process each removal efficiently. But we need to for each u, find the set T = S \ N_H(u). We can achieve this by scanning the entire S and for each v, if (u,v) is missing then skip else take. But scanning the whole S for each u would be O(n^2). But if we maintain S as a data structure that allows us to iterate over its elements in a way that we only process vertices that we will remove? For each u, we want to remove from S all vertices in T (i.e., those that are neighbors in G). Actually, in BFS, when we process u, we want to add to the queue all unvisited vertices that are neighbors in G, and mark them visited (i.e., remove from S). So we need to compute T = { v ∈ S | v not in N_H(u) }. This is exactly S minus N_H(u). Since N_H(u) is small (degree in H), we can compute T by copying S to a list, but S is large. However, we can use a technique: iterate over all v in S, but we only do that for vertices u that have high degree? That's not good.
Instead, we can use the approach: maintain an array "next" or a linked list of unvisited vertices. Then for each vertex u, we can iterate over all v in the list, but we skip those that are in N_H(u). However, if we iterate over the entire list each time, it's O(n^2). But we can do it cleverly: we only need to remove the vertices that are in T from S, and then recursively process them. Since total vertices removed over the whole algorithm is n, we can try to for each u, iterate over the missing neighbors and "mark" them as not to be taken, and then walk through the global list of unvisited vertices and collect all vertices that are not marked, while removing them from the list. But this still means that each time we process a vertex u, we might walk through the entire global list? No, we can avoid that by only processing the global list when the degree of u is large? Actually there is a standard method for BFS in complement of a sparse graph: we maintain a set of not yet visited vertices. For the current vertex u, we iterate over all v in the set that are NOT in the adjacency list of u (in H). To do this efficiently, we can simply try to iterate over all v in the set, but we can early stop? Not directly.
Alternative: Instead of scanning the set each time from u, we can do BFS on the complement graph (the missing edges graph) to find components? Let's think: We want connected components in G. Since G is dense, its complement H is sparse. Components in G might be many? For example, if H is empty, G is complete, then one component of size n. If H is a complete graph? That would be many missing edges? Actually if H is a complete graph, G would have no edges, so each vertex isolated -> n components. But H can't be complete because m is at most 200k, n up to 200k, but complete graph would have ~n^2/2 edges, too many. So H is sparse.
We can also use DSU (union-find) but union of all edges in G would be O(n^2) if we tried to union all pairs. Not feasible.
The standard solution for this problem (I recall) is to use a data structure that keeps a set of unvisited vertices, and for each vertex start BFS/DFS, and when exploring neighbors, for each unvisited vertex v we check if (u, v) is a missing edge by checking adjacency of u in H. But we want to avoid checking each v individually. However, we can use the trick: For each vertex u, we have its missing neighbors (non-edges). When exploring from u, we want to add all unvisited vertices except those missing neighbors. So we can iterate over the missing neighbors of u, and for each such v, if v is in the unvisited set, we "mark" it as "not allowed". But then the allowed vertices are the rest of the unvisited set. How to get them? Instead of iterating over the whole set, we can iterate over the unvisited set and skip the marked ones. But we can combine: we can loop through the unvisited set (say stored in a list) and for each vertex v in the list, we check if v is a missing neighbor of u. This check can be done O(1) if we have adjacency sets for u. But if the unvisited set is huge (like most vertices), we would have to iterate over all unvisited vertices for each u, leading to O(n^2) total. However, note that after we process u, we will remove many vertices from the unvisited set (the ones that are reachable). If we always start BFS from a vertex u, we will remove a whole component at a time. So for each component, we might scan the entire unvisited set? Actually consider: we have a set of unvisited vertices S. For each component, we pick a starting vertex u, we want to find all vertices in S that are connected to u (directly) and then continue. But if we process u, we need to find its neighbors in G. To find all neighbors of u in G that are still in S, we could iterate over S and test each v. However, after we process u and remove its neighbors, we process each neighbor similarly. So total work might be sum over each vertex of |S| at the time of processing that vertex, which could be O(n^2). But we can improve by noting that each time we iterate over S, we are essentially scanning the entire set of remaining vertices. If we do that for each vertex, it's O(n^2). However, we can do it differently: For each vertex u, we only need to consider the missing neighbors, and then we can "remove" from S the complement of missing neighbors? Actually we can perform a BFS by maintaining the unvisited set and for each vertex we process, we only need to check the missing neighbors, but we need to retrieve all other vertices from S. That can be done by iterating over S, but we can do it in such a way that the total work of iterating over S across all steps is O(n + m) by using a technique: we recursively split the set? Another standard method: Use a data structure that supports query: given a vertex u, delete all vertices from the set that are not in N_H(u) and return them. Since N_H(u) is small, we can do: iterate over all v in S, but we skip those in N_H(u). But we can instead iterate over N_H(u) and for each missing neighbor, we know that they are not to be removed, but they remain in S. However, the rest of S (which could be huge) are the vertices we want to remove and process. So we could simply "remove" all vertices from S that are not in N_H(u) and then process them. To do this efficiently, we need a way to iterate over S and exclude the ones in N_H(u). But if S is stored as a list, we could traverse it and collect the vertices that are not in N_H(u), but that would be O(|S|) for each u. However, we can improve by noticing that the set S shrinks, and we can use an approach where for each u we only iterate over its missing neighbors, and the rest of the vertices are "removed" by a global sweep? There is known technique: BFS in complement of a sparse graph using a "unvisited set" and for each vertex u, we iterate over all v in the set of unvisited vertices, but we use that we are going to remove many vertices each time, and the total cost of iterating over the set is O(n+m) if we use an efficient data structure that allows us to "walk" through the list and skip removed vertices, and we only perform a full scan of the list when we start a new component? I've seen problem: "Find connected components in complement of a graph" or "BFS in graph complement using set of unvisited vertices". The typical algorithm:
- Keep a global set of unvisited vertices (e.g., a boolean array and a linked list of all vertices, or a balanced BST).
- For each component:
- Pick a start vertex from the set, remove it, push to queue.
- While queue not empty:
- Pop u.
- For each vertex v in the unvisited set (we need to iterate over all currently unvisited vertices):
- If (u,v) is not a missing edge (i.e., edge exists in G) then we want to add v to the component (and queue) and remove v from unvisited.
- But iterating over all unvisited vertices each time is expensive.
However, there is an optimization: Since the graph is complement of sparse graph, we can for each u, iterate over the missing neighbors of u, and mark them as "forbidden". Then we iterate over the unvisited set, and for each v not forbidden, we add to queue and remove. But we still iterate over the whole unvisited set for each u. That yields O(n^2).
But we can reverse: Instead of iterating over all unvisited vertices for each u, we can do a BFS on the complement graph H to find components? Not directly.
Alternate viewpoint: The graph G's complement H is sparse. The connected components of G are exactly the co-components? Actually, consider the complement: If we take the complement of G (which is H), then the components of G are not directly related to components of H.
Another approach: Use DSU with edges from G, but we can add edges efficiently by considering vertices with low degree in H? Possibly we can use the fact that the missing edges are few, so vertices with high degree in H are few. But still could be some.
Better: Use the technique where we maintain the set of unvisited vertices, and when we process a vertex u, we only iterate over its missing neighbors, but we also need to find all other unvisited vertices that are not missing neighbors. To get them efficiently, we can have for each vertex a list of its neighbors in H. Then, for the current component, we can attempt to "collect" all vertices that are not in the union of missing neighbors of processed vertices? Hmm.
I recall a solution: Use a data structure that supports the operation: "Given a vertex u, retrieve all vertices in the current unvisited set that are not adjacent to u in H." This can be done by storing the unvisited vertices in a balanced BST, and for each missing neighbor v of u, we skip it. But to retrieve all others, we would need to iterate over the whole set again. However, if we had a structure that can split the set based on membership? Not straightforward.
Instead, we can use the standard BFS for complement graph by scanning the unvisited set only when we start a new component, and then use a DFS from each vertex, but when we go to a new vertex, we don't need to scan the entire set again if we maintain a "candidate set" of vertices that might be reachable from any vertex in the component. For a given component, we can start with the set of all unvisited vertices. Then for each vertex we add to the component, we remove from the candidate set all vertices that are not neighbors of that vertex in G? That is, we want to find the intersection of neighborhoods. Actually, we can compute the component as the set of vertices that are reachable via edges in G. Starting from a seed vertex s, we can compute the component as follows: initially component = {s}, candidate set = all other unvisited vertices. Then while there exists a vertex u in the component that we haven't processed for this component: we look at candidate set and remove those vertices that are not adjacent to u (i.e., missing edge). The ones that remain are those adjacent to u. For each such vertex v (adjacent to u), we move v from candidate set to component (and will process it later). So we need to, for a given u, compute the intersection of candidate set with the complement of N_H(u) (i.e., vertices not missing). That is: candidate_set \ N_H(u). To compute this efficiently, we can iterate over N_H(u) and for each missing neighbor v, if v is in candidate set, we remove it? Wait we want to keep vertices that are NOT in N_H(u). So we could remove from candidate set those vertices that are in N_H(u) and are also in candidate set? Actually if a vertex v is in N_H(u), then (u,v) is missing, so v is not directly connected from u, but might still be connected through other vertices. However, when we are at u, we only want to add vertices that are directly connected to u, because BFS/DFS from u should add its immediate neighbors. So we need to extract from candidate set those vertices v such that (u,v) is not missing. That is candidate_set minus N_H(u). To compute that, we could iterate over the missing neighbors of u and "mark" them as blocked, then iterate over the candidate set to collect the rest, but that again scans candidate set. However, we can alternatively: iterate over the missing neighbors of u and if they are in candidate set, we remove them from candidate set (since they are not directly reachable from u), but then after processing all u's missing neighbors, the remaining candidate set would be exactly the vertices that are not missing? But careful: removing them from candidate set would mean we lose them for future processing because they might be reachable via other vertices, but since they are not directly reachable from u, we should not add them now, but they might be reachable later from other vertices in the component. So we cannot just remove them permanently; we just need to skip them for this u's iteration. So we need to separate: we have candidate set of vertices that are still unvisited and potentially could join the component. For each u we process, we want to find those candidates that are adjacent to u. For each such v, we add v to component and remove from candidate set (so they won't be considered again). For candidates that are not adjacent to u, they stay in candidate set, to be possibly reached later from other vertices.
Thus, for each u, we need to find candidate_set ∩ (V \ N_H(u)). How to compute this efficiently? Since N_H(u) is small (degree in H), we can instead iterate over candidate_set, but we want to avoid O(|candidate_set|) for each u. However, note that candidate_set shrinks as we discover vertices. Initially candidate_set size ≈ n - component_size. For each u, we might need to examine candidate_set. In worst case, if the component is large, candidate_set becomes empty gradually. In total, if we for each u scan the entire candidate_set, it's O(n^2). But there is a better approach: Instead of scanning candidate_set for each u, we can process the missing neighbors and for each such neighbor, we can mark that it is not directly reachable from u, but we still need to find which candidates are directly reachable. We can also do it by iterating over candidate_set only once per "component" using a technique: For each component, we maintain a queue of vertices to process (like BFS). For each vertex u popped, we iterate over all missing neighbors of u, and for each such neighbor v that is still in candidate_set, we mark it as "blocked for this u". But we still need to find the unblocked ones. However, we can take a different perspective: The algorithm for BFS in the complement of a graph can be implemented using a data structure that supports the operation: "For a given u, find all vertices v in the set S that are not adjacent to u in H." The standard solution is to use a set of unvisited vertices (like a boolean array + linked list), and for each u, iterate over all vertices v in S, but skip those that are in adjacency list of u. The trick is that for each u, we only need to iterate over S once, but we can reuse S across multiple u in a component by ensuring that S is the set of vertices not yet visited and not yet added to the component. Actually, we can do the following: For the current component, we start with S = all unvisited vertices (global). Then we pick a start vertex s, remove s from S, add to component. Then we do BFS: we have a queue Q containing s. While Q not empty: u = Q.pop(). We need to find neighbors of u in G that are still in S. To do this, we can create a list of vertices to be removed from S: those v in S such that (u,v) is not missing. But instead of iterating over all v in S, we can iterate over the missing neighbors of u, and for each such v, we mark it as "not to be taken" for this u. However, we still need to collect the rest. So we could iterate over all v in S, but S is shrinking, and we only need to do that for u's that have many missing neighbors? Actually there is a known technique: We can process the vertices in the component by maintaining a global list of unvisited vertices, and for each vertex u in the component (popped from queue), we iterate over the list of unvisited vertices, but we only do it once per component, not per u. Specifically, we can do:
- Global set unvisited = {1..n}
- While unvisited not empty:
- Start new component, pick arbitrary vertex start, remove start from unvisited, add to queue.
- While queue not empty:
- u = queue.front(); queue.pop()
- Create a list to store vertices to be added (neighbors).
- For each v in unvisited:
- if (u,v) is not a missing edge (i.e., v not in H adjacency list of u) then add v to list.
- For each v in list: remove v from unvisited, add v to queue, add to component.
- After queue empty, record component size.
But this still has for each u, we scan all unvisited vertices globally, which could be O(n^2). However, note that we can modify: For a component, we can scan the unvisited set only once, using the idea of BFS in complement with two sets. Another known algorithm: Use BFS on complement graph but using a trick to reduce complexity: For each vertex, we maintain its degree in the missing graph, but also use a global "candidate" set of unvisited vertices. When we process a vertex u, we iterate over the missing neighbors of u, and for each such neighbor v that is still in the candidate set, we mark it as "not directly reachable from u". Then we take all other vertices from the candidate set (i.e., those not marked) as neighbors, remove them from candidate set and add to queue. But to take all other vertices, we still need to iterate over the candidate set to collect them, which is O(|candidate_set|). However, we can instead process by iterating over the candidate set once per u, but we can also use the fact that each vertex is removed from candidate set exactly once when it gets added to a component. And we can structure the algorithm so that for each u, we iterate over the candidate set, but we only do that for u's that have small degree in H? Actually we can implement the BFS in complement as follows:
unvisited = set of all vertices.
while unvisited not empty:
pick start, remove, Q = [start]
while Q not empty:
u = Q.pop()
for each v in copy_of_unvisited:
if v not in missing[u]:
remove v from unvisited; add v to Q; (and to component)
But we can't afford to copy unvisited for each u.
Better approach: Use the fact that we can treat the missing edges as constraints. For the current component, we can start with a candidate set = unvisited. Then we process a queue of vertices in the component. For each vertex u popped from queue, we want to extract from candidate set all vertices that are adjacent to u in G. Since adjacency in G is complement of H, this is candidate_set minus N_H(u). To compute this, we can iterate over all v in N_H(u). For each v, if v is in candidate_set, we temporarily mark it (or move it to a "blocked" set). Then the remaining candidate_set are the vertices adjacent to u. We can then add all those remaining vertices to the component (and remove them from candidate_set). However, we also need to restore the temporarily removed vertices (the ones that are in N_H(u)) back to candidate_set for consideration by other vertices. So we cannot permanently remove them. We need to handle them separately.
Idea: For each u, we need to split candidate_set into two groups: those adjacent to u (call group A) and those not adjacent (group B). We'll move group A to component and queue; group B stays as candidate_set for next vertices. We need to compute group A efficiently without scanning entire candidate_set.
We can achieve this by iterating over the missing neighbors of u and for each such v, if v is in candidate_set, we move it to a temporary list or mark it as "not A". But we still need to collect all vertices in candidate_set that are not missing neighbors. So we need a way to iterate over candidate_set minus a set of vertices (the missing neighbors). If candidate_set is stored as a linked list, we can traverse the list and for each v, check if v is in N_H(u). Since N_H(u) is small, we can use a boolean array (or a set) to test membership in O(1) (by marking with a timestamp or boolean flag). So we could do:
- For current u, create a boolean array "blocked" initially false for all vertices? But we can't clear it for each u globally (O(n) per u). Instead, we can use a list of vertices that we mark, and unmark after.
Better: For each u, we can iterate over N_H(u) and set a flag (e.g., "in_nbr[u] = true") for those vertices. But we need to clear those flags after we finish processing u, which we can do by iterating again over N_H(u) and resetting. Since total sum of degrees is O(m), that's fine.
Then we traverse the candidate_set (the list of still unvisited vertices not yet in component) and for each v in candidate_set, if blocked[v] is false, then v is adjacent to u -> we add v to component and remove from candidate_set. If blocked[v] is true, we skip v (it stays in candidate_set). However, traversing candidate_set for each u could be expensive if candidate_set is large and many u's are processed. But note that once we add a vertex to component, it's removed from candidate_set, so candidate_set shrinks. In worst case, if the component is large, we might process many u's and each time we traverse the entire candidate_set? Let's analyze.
Suppose we have a component of size C. Initially candidate_set size = n - (size of already discovered components). For this component, candidate_set initially contains all vertices not yet assigned to any component. At the start of component, we pick a start vertex s, remove it from candidate_set, and candidate_set now contains n-1 vertices (maybe less if previous components exist). Then we process s: we traverse the entire candidate_set (size ~ n) and add all vertices that are neighbors of s. That could be O(n) time. After processing s, candidate_set may become smaller (maybe many added to component). Next we process the first neighbor u1 from queue. At that point, candidate_set contains only vertices not yet added to component (i.e., those that are not neighbors of s or were neighbors but we haven't processed?). Actually after processing s, we moved all vertices adjacent to s from candidate_set to the component and queue. So candidate_set now contains only vertices that are NOT adjacent to s (i.e., those that are missing edges with s). The number of such vertices is exactly deg_H(s) (the number of missing edges incident to s) because all other vertices are adjacent. Since deg_H(s) is the degree in H, which could be up to n but typical total m is small, but some vertices could have high degree. However, deg_H(s) is not necessarily small? For a vertex, its missing edges can be many; e.g., if m = 200k and n = 200k, a single vertex could have degree 200k (connected to all others missing), making candidate_set after processing s of size up to 200k. That is large.
Now, when we process u1, we would again traverse candidate_set (which now could be large). But note: candidate_set at this point consists of vertices that are not adjacent to s. For u1, we need to find among these vertices those that are adjacent to u1. Since candidate_set size may be large (like 200k) and we may process many u's, total complexity could be O(n * size of candidate_set) worst-case O(n^2). However, we can try to reduce the work by noting that each vertex, once added to component, is removed from candidate_set, and we never process it again. So the total number of times we traverse a vertex in candidate_set across all processing steps equals the number of times it is examined and found to be either added or skipped. If we traverse the entire candidate_set for each u, a vertex may be examined many times before it is eventually added (or remains in candidate_set until the component is done). This could be many examinations. For example, consider a graph where H is a star: vertex 1 is missing edges with all other vertices (i.e., deg_H(1) = n-1). In G, vertex 1 is isolated? Actually if vertex 1 has missing edges to all others, then in G, vertex 1 has no edges, so it forms its own component of size 1. But other vertices are all connected among themselves because any two other vertices are not missing? If only missing edges are (1, v) for all v, then H is a star. In G, vertex 1 is isolated, and vertices 2..n form a clique (complete subgraph) -> component of size n-1. Let's see our algorithm: start with candidate_set = all vertices. Pick start s: if we pick vertex 1, then we process s: we traverse candidate_set (size n-1). For each v in candidate_set, we check if (1,v) is missing? Since all are missing, blocked[v] true for all. So we add none. So component size 1. Then candidate_set remains n-1 vertices. Then we start a new component, pick vertex 2. Process vertex 2: traverse candidate_set (size n-1). For each v, check if (2,v) missing? Only missing edges from 2 are (2,1) but 1 is already removed, so none others. So we add all remaining vertices to component and queue. So total traversals: For component of vertex 1, we traversed candidate_set once (n-1). For component of vertex 2, we traversed candidate_set once (n-1). So total O(n). That's okay.
But consider a graph where H is a matching? Or more complex: We need to worry about case where a component is large and many internal missing edges cause candidate_set to remain large after each step. For example, suppose H is a complete bipartite graph between two halves, but then G is disconnected? Let's think worst-case scenario for traversal: we have a component that is large and we need to process many vertices, each time traversing the entire candidate_set (which may remain large until near the end). Could that happen? Let's try to construct. Suppose we have n vertices, and missing edges form a graph that is a collection of cliques? Actually G's complement is H. In G, we want a component that requires many BFS steps, and each step we only remove a few vertices from candidate_set because most vertices are not adjacent to the current vertex (i.e., have missing edges). That would cause candidate_set to stay large for many steps. For candidate_set to stay large, the current vertex u must have many missing edges to the remaining candidate_set. That means deg_H(u) is large relative to remaining vertices. Over many steps, if many vertices have high degree in H, then each step we traverse a large candidate_set but only remove a few vertices. Could lead to O(n^2) if we traverse the full candidate_set each time. However, note that the sum of degrees in H is 2m ≤ 400k. So if many vertices have high degree, total degrees would exceed m. Indeed, the total number of missing edges is limited to 200k. So the sum of all degrees in H is at most 400k. Therefore, the number of vertices with high degree is limited. For a vertex, its degree in H can be up to n-1, but if one vertex has degree n-1, that consumes O(n) edges. With m=200k, the maximum possible sum of degrees is 400k. So on average degree is small. But worst-case, there could be a vertex with degree ~200k, and then other vertices have small degree. In that case, for the component that includes that high-degree vertex? Let's analyze.
Suppose we have a vertex v0 with high degree (e.g., 200k). In G, v0 is missing edges to many vertices. But if v0 is part of a component, it may still be connected via other vertices. Consider component containing v0. In BFS from a start, when we process v0, we traverse candidate_set (size maybe large) but we will add only those vertices not in N_H(v0). Since v0 has high degree, it will have few neighbors in G (i.e., those not in N_H(v0)). So we add few vertices (maybe zero). Then candidate_set remains large. Then we process next vertex u from queue (if any). If u also has high degree, again few additions. But note that high-degree vertices are limited because sum of degrees is bounded. So the total number of high-degree vertices is at most something like O(sqrt(m))? Actually, with total sum 400k, the number of vertices with degree > k is at most 400k/k. For k = sqrt(m) ~ 447, number is about 894. So many vertices have degree <= sqrt(m). For vertices with small degree, when we process them, we may add many vertices (since candidate_set is large and they have many neighbors). However, the candidate_set traversal for each such vertex would still be large. But we could perhaps limit traversal by not scanning the entire candidate_set for each small-degree vertex. Instead, we can invert the iteration: For each vertex u, we iterate over its missing neighbors (which are few for small-degree vertices) and use that to update candidate_set efficiently, without scanning the whole set.
Let’s think more carefully about the standard solution for complement graph BFS. I recall a known problem "BFS on complement graph" or "connected components in complement graph" appears in Codeforces problem (maybe "500B - New Year Permutation" not exactly). There's a classic technique using a set of unvisited vertices and for each BFS step, we iterate over the unvisited set, but we remove vertices that are not adjacent to the current vertex. However, we can do it more efficiently by only iterating over the unvisited set once per BFS, not per vertex, by using a queue and a temporary list. Actually, there is a well-known method:
unvisited = set of all vertices.
while unvisited not empty:
start = any vertex from unvisited; remove start; queue.push(start); component_size = 1.
while queue not empty:
u = queue.pop()
for v in list(unvisited):
if (u,v) is edge in G:
remove v from unvisited; queue.push(v); component_size++.
But this scans the entire unvisited set for each u, leading to O(n^2). However, there's optimization: we can do it as:
unvisited = set of all vertices.
while unvisited not empty:
start = any; remove start; queue.push(start); component_size = 1.
while queue not empty:
u = queue.pop()
to_remove = []
for v in unvisited:
if v not in missing_adj[u]:
to_remove.append(v)
for v in to_remove:
remove v from unvisited; queue.push(v); component_size++.
But again scanning unvisited each time.
But there is a known technique to perform BFS on complement graph in O(n + m) by using a data structure that allows us to "iterate over unvisited vertices that are not neighbors in H" without scanning all each time. The trick: Use a global set of unvisited vertices. For each vertex u, we want to find all v in unvisited that are not in H-neighbors of u. Instead of scanning unvisited, we can iterate over H-neighbors of u, and for each such neighbor v, if v is in unvisited, we mark it as "blocked". Then, the vertices in unvisited that are not blocked are exactly the ones we want. But to collect them without scanning whole unvisited, we can use the fact that the number of blocked vertices is small (since degree of u in H is small). However, the unblocked vertices may be many. We need to extract them efficiently. How can we extract all unblocked vertices from a set without scanning all? We could iterate over the blocked vertices and remove them from the set temporarily, then iterate over the set (which now contains only unblocked) and move them to queue, and then add back the blocked vertices. But if we temporarily remove blocked vertices, we need to know which ones to remove; we can do that by, for each blocked vertex v, we remove it from the set and store it in a temporary list. Then after processing u, we add back the blocked vertices to the set. But the crucial point: when we remove the blocked vertices temporarily, the set becomes exactly the unblocked vertices. Then we can move all vertices from this set to the queue (since they are all neighbors of u), and then add back the blocked vertices. However, moving all vertices from the set to the queue would empty the set. But that's not correct because the unblocked vertices are exactly those that we want to add to the component, and after we add them, they should be permanently removed from the global unvisited set (since they become visited). So indeed, for u, we want to move all unblocked vertices from unvisited to the component. That will permanently remove them from unvisited. The blocked vertices stay in unvisited because they are not directly reachable from u. So we don't need to temporarily remove blocked and then add back; we just need to identify the unblocked vertices and move them out. The blocked vertices remain.
Thus, we need to separate unvisited set into two groups: group A (neighbors of u in G) and group B (non-neighbors). Group A we move out; group B stays. Since group A is likely large, we want to move them efficiently. The operation "move all except some small set of blocked vertices" can be done by iterating over the blocked vertices and removing them from unvisited temporarily? That doesn't help to get group A. Actually, if we could quickly get all elements of a set except those in a given small set, we could do: for each blocked vertex, mark it as "do not remove". Then we iterate over the entire set and collect those not marked. But again that's O(|unvisited|). However, note that after processing u, the size of unvisited will decrease by |group A|. So if |group A| is large, scanning the whole set to collect them might be acceptable because the total cost of scanning can be amortized over the number of vertices removed. Because each time we scan the entire unvisited set, we remove many vertices. But if we scan the entire unvisited set many times, we might have overhead. But perhaps we can design that we scan the entire unvisited set only when we start a new component, and then within a component, we avoid scanning the whole set repeatedly. Let's think about that.
Alternative approach: Use DSU but adding edges efficiently. Since H is sparse, we can consider building the graph G by focusing on vertices with low degree in H. For each vertex, we can connect it to all other vertices except its missing neighbors. Instead of adding all edges, we can connect using DSU: For a vertex u, we can union u with all vertices that are not in its missing neighbor set. However, this would still be O(n * something). But if we iterate over all v not in N_H(u), that's many. However, we can use the fact that we only need to union u with a representative set. For DSU, if we union u with all other vertices, we can simply union u with the first vertex not in N_H(u) and then rely on transitivity? Not sufficient.
Better: The graph is complement of a sparse graph, so it is "almost complete". We can compute its connected components by considering the complement graph H and its degrees. There is a known result: In a graph with complement sparse, the number of components is small, or large components are cliques? Not exactly.
Another direction: We can compute the connected components by focusing on vertices with small degree in H. Suppose we pick the vertex with smallest degree in H (i.e., fewest missing edges). That vertex is connected to almost all others. Then we can merge it with all vertices except its non-neighbors. That will form a large component. Then we can remove that component and repeat on the remaining vertices. This is similar to the technique used in the problem "Connected Components in a graph where edges are complement of given pairs" often solved by maintaining a set of unvisited vertices and processing vertices in order.
Let's recall a known solution: Use a data structure (like a balanced BST or a boolean array with a linked list of unvisited vertices). Perform BFS/DFS on the complement using the following method:
- Maintain an array "not_visited" list (like a linked list of indices).
- For each vertex, store its adjacency list of missing edges (H edges).
- While there are unvisited vertices:
- Start from a vertex start, remove it from list, push to stack.
- While stack not empty:
- u = pop.
- For each v in missing adjacency list of u:
- Mark v as "blocked" (using a temporary array, e.g., in_current_step[v] = true).
- Then iterate through the list of unvisited vertices (using the linked list), but we will only check each vertex once per iteration of the outer while? Actually the typical algorithm:
For each vertex u in the component (processed from stack), we iterate through the current list of unvisited vertices (global list). For each v in the list, if v is not marked as blocked, then we add v to component (remove from list and push to stack). Else, we skip v and keep it in list.
- After processing, we unmark the blocked vertices.
This algorithm's complexity: For each u, we iterate over the entire current list of unvisited vertices. However, note that the list shrinks as vertices are removed. But still, if we process many u's, each u might scan a large list. However, a crucial observation: Within a single component, we only need to scan the unvisited list once, not for each u. Because after we scan the list for the first u in the component, we remove all vertices that are adjacent to u (i.e., not blocked). Then, for the next u, the unvisited list only contains vertices that were blocked during all previous scans. That set is exactly the set of vertices that are not adjacent to any of the previously processed vertices. This set may be much smaller. In fact, if we process u1, we remove all vertices that are neighbors of u1; the remaining unvisited vertices are those that are non-neighbors of u1. When we process u2, which is a vertex that we added from the neighbor set of u1, its neighbors may include some of those remaining vertices, but not all. However, we can again scan the (now smaller) unvisited list to find neighbors of u2. So the total work of scanning the unvisited list across all u in a component is proportional to the sum of sizes of the list at the times of scanning. Since each scan processes each element of the list exactly once, and the list shrinks over time, the total number of times an element is examined across scans within a component might be O(size of component * something). Actually, if we scan the whole list for each u, an element that remains in the list for many scans will be examined multiple times. This could be problematic if many scans occur while the list remains large. But how many scans can occur while the list is large? Each scan corresponds to processing a vertex u from the component's BFS queue. If the component is large, there could be many u's. However, note that after we process the first vertex, we remove many vertices (maybe almost all), leaving only a few. So the list size drops dramatically. But is it guaranteed? Not necessarily. Consider a graph where the component is a "star" in G? Actually, G is complement of H. If H is a matching, G is almost complete but missing a few edges. Then in a component, after processing the first vertex, we remove all vertices except those that are missing edges with it. That number is small (degree in H). So subsequent scans will be over small lists. So total work is O(n + sum of degrees). That seems efficient.
But what if H is such that there is a vertex with high degree? Then after processing that vertex, we remove only few vertices, leaving many in the unvisited list. But that vertex is itself part of the component? If it has high degree, it's missing edges to many vertices, meaning it's not connected to many vertices directly. So if we start BFS from a vertex that has high degree, we might remove only a few vertices. However, that vertex could be isolated (component size 1) if it's missing edges to all others? Then it would be its own component. In that case, we start a component with that vertex, scan the list (size n-1) and find no neighbors (since all are missing), so we remove none. Then component ends. That's one scan O(n). Then next component we start from a low-degree vertex, and after processing that, we remove many vertices. So total scans: two scans of roughly O(n) each = O(n). Good.
What about a component where there are several high-degree vertices that are connected indirectly through low-degree vertices? For example, suppose we have two high-degree vertices a and b that are not directly connected (missing edge between them) but both are connected to many low-degree vertices. In that component, if we start BFS from a low-degree vertex, we might first remove many vertices, leaving a and b and maybe some others. Then we process a, but its missing neighbors might be many (including b and many others already removed). The list of remaining unvisited vertices might be just a few (including b). So scanning the list (which is small) is cheap. So the expensive scans are those that happen when the unvisited list is large. That occurs only at the beginning of each component, and possibly if we have a component where many vertices have high degree but are still connected? But high-degree vertices have many missing edges, meaning they are not directly connected to many vertices, so they will tend to be left in the unvisited list after processing low-degree vertices, making the list small. So the expensive scans are limited.
Let's attempt to bound the total complexity. In the algorithm where for each popped vertex u we scan the entire current unvisited list (let's call it S), we can analyze the total work as: each time we scan S, we iterate over all elements in S. Suppose we have a sequence of scans. The total work = sum_{each scan} |S|. When we scan, we might remove some vertices from S (the ones that are neighbors of u). The vertices that are not removed remain. Over the course of processing a component, each vertex can be present in S for multiple scans until it is removed. The number of times a specific vertex v is examined (i.e., appears in a scan) equals the number of scans that occur while v remains in S. If v is removed early, it's examined few times. If v stays for many scans, it's examined many times. But can v stay for many scans? v stays in S as long as it is not adjacent to any of the vertices processed so far in the BFS. That means v is missing edges with all vertices that have been processed. So if we process many vertices, v must be missing edges with all of them. Since each missing edge counts towards degree in H, the number of processed vertices that v is missing edges with is at most deg_H(v). Therefore, v can stay in S for at most deg_H(v) + 1 scans? Actually, consider: we start component with some vertex u1. v is in S initially. After processing u1, v will be removed if (u1, v) is an edge in G, i.e., if (u1, v) is not missing. If it is missing, v stays. So after processing u1, if v is missing edge with u1, it stays. Then we process u2 (which is neighbor of u1, added earlier). If v is missing edge with u2, it stays. This continues. v will be removed as soon as we process some vertex u for which (u, v) is not missing. Since v has at most deg_H(v) missing edges, it can be missing edges with at most deg_H(v) vertices in the component. However, the BFS processes vertices from the component; some of them might be those that v is connected to. v will be removed when we process the first vertex u that is connected to v (i.e., not missing). So v can stay in S for at most (number of vertices processed before encountering a neighbor) steps. That number is at most the number of vertices in the component that are missing edges with v. But that number is at most deg_H(v). In the worst case, if v is missing edges with all vertices in the component, then v will never be removed (but then v would not be in the same component? Actually if v is missing edges with all vertices in the component, then there is no edge connecting v to any vertex in the component, so v is not in the same component. Thus v belongs to a different component. In that case, v will be in S when processing this component, but it will never be removed because it's not connected. However, after finishing this component, v will be the start of a new component. So during processing of the current component, v stays in S for all scans of that component. The number of scans in that component equals the number of vertices processed in that component (the BFS queue size). That could be large. But if v is not in the component, then during processing of this component, v will be examined many times, leading to O(component size * number of external vertices) which could be O(n^2) if there are many external vertices and many scans. However, note that if v is not in the component, it will never be removed, and it remains in S throughout the entire BFS of that component. If the component is large (say n/2) and there are many external vertices (also n/2), then each scan (which occurs for each vertex in the component) will iterate over all external vertices, causing O(n^2). So that is a problem.
But is that scenario possible? Let's imagine component A of size ~n/2, and component B of size ~n/2. The graph G has no edges between A and B. That means for any a in A, b in B, the edge (a,b) is missing (i.e., present in H). So H contains all cross edges between A and B. That's a complete bipartite graph between A and B. The number of missing edges would be |A||B|, which could be about n^2/4, exceeding m (200k) for large n (n up to 200k). Since m ≤ 200k, such a dense missing cross would be impossible if n is large. Because m is limited to 200k, the total number of missing edges is at most 200k. So large components with many cross missing edges between them cannot both be large. Indeed, if we have two large components, then between them there must be missing edges for every pair, which would be huge, exceeding m. So the structure of G given the sparsity of H is constrained: either there is one large component and many small ones, or the graph is almost complete, etc. More precisely, the complement of a sparse graph is dense and has certain properties: The number of connected components in G is either 1 or can be many, but if there are multiple components, each component must be a clique? Actually, consider two components C1 and C2 in G. For any u in C1, v in C2, edge (u,v) is missing (since they are in different components, there is no edge; but is that necessarily true? In G, if u and v are in different components, there is no path, but there could still be an edge between them? Actually, if there was an edge between u and v, they would be in the same component. So if they are in different components, there cannot be an edge between any vertex of C1 and any vertex of C2. Therefore, all cross pairs are non-edges, i.e., missing edges. So the number of missing edges between C1 and C2 is |C1| * |C2|. Since total m is ≤ 200k, if both components are large, product could exceed 200k. For n up to 200k, the maximum product for two components would be if one is about 200k/2=100k and the other 100k, product = 10^10, impossible. So at most one component can be large, and the rest must be very small. In fact, if there is a component of size s, then all other vertices are each missing edges to every vertex in that component? Actually, for any vertex v outside the component, since there is no edge between v and any vertex in the component (otherwise v would be connected), all edges between v and the component are missing. Therefore, the number of missing edges contributed by v is at least s (all edges to component). If there are t vertices outside the component, the total missing edges at least s * t. Since m ≤ 200k, s * t ≤ 200k. Given n = s + t, this imposes constraints. For large n (e.g., 200k), if s is large, t must be small; conversely, if there are many small components, s might be small? Let's analyze: Suppose we have k components of sizes s1, s2, ..., sk. Then for each pair of distinct components, all cross edges are missing. So total missing edges at least sum_{i<j} si * sj. This sum can be approximated as ( (sum si)^2 - sum si^2 ) / 2 = (n^2 - sum si^2)/2. Since m ≤ 200k, this forces n^2 - sum si^2 ≤ 400k, so sum si^2 ≥ n^2 - 400k. If n is large (say 200k), n^2 is huge (~4e10), impossible. Therefore, for large n, it must be that there is one giant component and the rest are tiny. More precisely, if n > ~ 1000, the product constraints force that there can be at most one component of size > 600? Let's compute: If we have two components of sizes a and b with a≥b, then missing edges between them at least ab. Since a*b ≤ m ≤ 200k, for a=2000, b must be ≤100. For a=20000, b must be ≤10. For a=200000, b must be ≤1. So indeed, there can be at most one component of significant size; all other components are of size at most something like m / size_of_large_component. Since n can be 200k, the large component size could be close to n, and then other components are at most floor(m / (n - t))? Actually if large component size is s, then each other vertex v (outside) contributes at least s missing edges (to each vertex in the large component). So t * s ≤ m => t ≤ m/s. So the number of vertices outside the large component is at most m/s. Since s could be as low as n/2, t