PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000126. Kết nối động (Dynamic Connectivity)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

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 nn vertices numbered 11 through nn and mm initially present edges. No two initial edges represent the same unordered pair of endpoints.

Process kk operations that change the edge set:

  • 1 a b: add the undirected edge between aa and bb. It is guaranteed to be absent immediately before this operation.
  • 2 a b: remove the undirected edge between aa and bb. 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 k+1k+1 integers. The order of the endpoints of an undirected edge does not matter.

Input

  • The first line contains n,m,kn,m,k.
  • The next mm lines each contain a,ba,b describing an initially present edge.
  • The next kk lines each contain t,a,bt,a,b with t∈{1,2}t\in\{1,2\}.
  • Every endpoint is in [1,n][1,n], initial edges are unique, and every addition/removal is valid.

Output

Print exactly k+1k+1 integers separated by whitespace: the initial number of connected components, followed by the counts after operations 1,2,…,k1,2,\ldots,k.

Subtasks

General guarantees: 2≤n≤100 0002\le n\le100\,000, 1≤m,k≤100 0001\le m,k\le100\,000; edges are undirected and all changes are valid.

  • Subtask 1 (10 points): 2≤n≤702\le n\le 70, 1≤m≤1001\le m\le 100, 1≤k≤1001\le k\le 100; all general guarantees remain in force.
  • Subtask 2 (20 points): 2≤n≤2,0002\le n\le 2,000, 1≤m≤3,0001\le m\le 3,000, 1≤k≤3,0001\le k\le 3,000; all general guarantees remain in force.
  • Subtask 3 (30 points): 2≤n≤30,0002\le n\le 30,000, 1≤m≤20,0001\le m\le 20,000, 1≤k≤20,0001\le k\le 20,000; all general guarantees remain in force.
  • Subtask 4 (40 points): 2≤n≤100,0002\le n\le 100,000, 1≤m≤100,0001\le m\le 100,000, 1≤k≤100,0001\le k\le 100,000; 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 (1,4)(1,4) and (2,3),(3,5)(2,3),(3,5) form two components, so the first count is 22. Adding (2,5)(2,5) creates a cycle inside an existing component, leaving 22. Removing (3,5)(3,5) does not disconnect that component because of path 3−2−53-2-5, so the count stays 22. Adding (1,2)(1,2) joins both components, giving 11 and output 2 2 2 1.