Connection and Disconnection
Have you ever heard of depth-first search? This algorithm allows you to determine if a graph is connected in O(E) time. It can also be used to count the number of connected components within the same time complexity.
Are you familiar with a disjoint set system? This data structure enables efficient handling of queries like "Add an edge to the graph" and "Count the number of connected components in the graph."
Have you encountered the dynamic connectivity problem? This problem involves processing three types of queries:
Add an edge to the graph.
Remove an edge from the graph.
Count the number of connected components in the graph.
Input
Initially, the graph is empty.
The first line contains two integers, N and K — the number of vertices and the number of queries, respectively (1 ≤ N ≤ 300000, 0 ≤ K ≤ 300000). The following K lines each contain a query. There are three types of queries:
+ u v: Add an edge between vertices u and v. It is guaranteed that this edge does not already exist.
- u v: Remove the edge between vertices u and v. It is guaranteed that this edge exists.
?: Count the number of connected components in the graph.
Vertices are numbered from 1 to N. In all queries, u ≠ v. The graph is undirected.
Output
For each query of type "?", output the number of connected components at that point in time.