Problems > Components over Time

Components over Time

Difficulty: 1850 4000 ms 128 MB

union-find

Edges arrive one by one on N initially-isolated nodes. After EACH edge, print the number of connected components.

Input: the first line has N and M; each of the next M lines has an edge u v.

Output: M lines: the component count after each edge.

Constraints:

  • 1 ≤ N, M ≤ 105

Sample Tests

Input 1
4 3
1 2
3 4
2 3
Output 1
3
2
1
Input 2
2 2
1 1
1 2
Output 2
2
1

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.