Given N rooms in a row and M wall-breaking actions, count how many rooms remain after all actions merge neighboring rooms.
Easy3Union-findImplementationNo attempts yetTime limit1sMemory limit512 MBByeongchan wanted a club room, so he asked the LINK office and got a chance to receive one of N rooms. The building is a single straight corridor with N rooms in a row. The leftmost room is numbered 1, the numbers grow by 1 toward the right, and the rightmost room is numbered N. Between every pair of neighbouring rooms there is one wall that separates them.
Plenty of other people want a club room too. Rooms were plentiful, so Byeongchan was not worried.
Then the Big Jongbin Villain showed up and started knocking down the walls of the building. The villain follows these rules.
As the number of rooms dropped, Byeongchan grew nervous. To work out his chance of getting a club room he first needs the number of rooms that are left. Given the number of actions M and the starting number of rooms N, find how many rooms remain after every action is done.
The first line contains a positive integer N (2≤N≤100), the starting number of rooms. The second line contains a non-negative integer M (0≤M≤100), the number of actions. Each of the next M lines describes one action with two positive integers x and y (1≤x<y≤N), meaning that every wall between room x and room y is knocked down.
The villain is clumsy, so the same action may appear several times.
Print the number of rooms that remain after every action is done, on one line.
Take N=5 with the actions (1,2) and (2,4). The first action merges rooms 1 and 2, leaving (1, 2), (3), (4), (5). The second action merges rooms 2, 3 and 4, leaving (1, 2, 3, 4) and (5). So 2 rooms remain.