#K81147. Dynamic Connectivity with Reversible Edge Operations
Dynamic Connectivity with Reversible Edge Operations
Dynamic Connectivity with Reversible Edge Operations
This problem involves managing connectivity among n nodes under a sequence of operations, where each operation may either connect two nodes or disconnect them. Initially, the nodes are isolated (i.e. each node forms its own connected component).
An addition operation is denoted by + u v
, which connects node u and node v if they are not already connected. A disconnection operation is denoted by - u v
and it breaks the connection between u and v if such a connection exists. After each operation, you are required to output the current number of connected components.
You are encouraged to use a disjoint set union (DSU) data structure with path compression and union-by-rank optimizations to manage the dynamic connectivity efficiently. Note that the disconnect operation is a non-standard operation in DSU and a naive simulation is applied here by isolating one of the nodes.
Remark: In practical competitive programming problems, deleting an edge in DSU is complex. The provided problem uses a simplified version of disconnection.
inputFormat
The first line contains two integers n and m — the number of nodes and the number of operations respectively. Nodes are numbered from 1 to n.
The next m lines each contain an operation in one of the following formats:
+ u v
: Connect node u and node v.- u v
: Disconnect node u and node v.
outputFormat
Output exactly m lines, each line containing a single integer — the number of connected components after each corresponding operation is processed.
## sample5 6
+ 1 2
+ 2 3
+ 4 5
- 1 2
+ 3 4
- 2 3
4
3
2
3
2
2
</p>