Disjoint Set Union

5

2 votes
Very-Easy
Problem

You are given number of nodes, N, and number of edges, M, of a undirected connected graph. After adding each edge, print the size of all the connected components (in increasing order).

Input:
First line contains two integers, N and M, number of nodes and number of edges.
Next M lines contains two integers each, X and Y, denoting that there is an edge between X and Y.

Output:
For each edge, print the size of all the connected components (in increasing order) after adding that edge.

Constraints:
1N103
1MN1
1X,YN

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

After adding first edge the connected components are [1, 2], 3, 4, 5 and their sizes are 1, 1, 1, 2.
After adding second edge the connected components are [1, 2], [3, 4], 5 and their sizes are 1, 2, 2.
After adding third edge the connected components are [1, 2]. [3, 4, 5] and their sizes are 2, 3.
After adding the fourth edge the connected component is [1, 2, 3, 4, 5] and its size is 5 .

Editor Image

?