#SRD0000126. Kết nối động (Dynamic Connectivity)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Kết nối động (Dynamic Connectivity)
Dynamic Connectivity
Source: VNOJ
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an undirected graph with vertices numbered through and initially present edges. No two initial edges represent the same unordered pair of endpoints.
Process operations that change the edge set:
1 a b: add the undirected edge between and . It is guaranteed to be absent immediately before this operation.2 a b: remove the undirected edge between and . It is guaranteed to exist immediately before this operation.
A connected component is a maximal set of vertices where every pair is connected by a path. Determine the number of connected components before the first operation and after every operation, producing exactly integers. The order of the endpoints of an undirected edge does not matter.
Input
- The first line contains .
- The next lines each contain describing an initially present edge.
- The next lines each contain with .
- Every endpoint is in , initial edges are unique, and every addition/removal is valid.
Output
Print exactly integers separated by whitespace: the initial number of connected components, followed by the counts after operations .
Subtasks
General guarantees: , ; edges are undirected and all changes are valid.
- Subtask 1 (10 points): , , ; all general guarantees remain in force.
- Subtask 2 (20 points): , , ; all general guarantees remain in force.
- Subtask 3 (30 points): , , ; all general guarantees remain in force.
- Subtask 4 (40 points): , , ; all general guarantees remain in force (full original constraints).
Examples
Example 1
Input
5 3 3
1 4
2 3
3 5
1 2 5
2 3 5
1 1 2
Output
2 2 2 1
Explanation
Initially, edges and form two components, so the first count is . Adding creates a cycle inside an existing component, leaving . Removing does not disconnect that component because of path , so the count stays . Adding joins both components, giving and output 2 2 2 1.