Jonathan Sumabat 5

View as PDF

Submit solution

Points: 7
Time limit: 2.5s
Python 3 6.0s
Memory limit: 64M
Python 3 256M

Author:
Problem type

Jonathan Sumabat is on vacation at the beach. Alongside sunbathing, snorkeling and swimming, Jonathan Sumabat also enjoys building sandcastles. Jonathan Sumabat builds a series of sandcastles with each of them having their own separate moat. Jonathan Sumabat now begins connecting these moats to form sand kingdoms. A sand kingdom contains all sandcastles with the same interconnected moat. Knowing the connections Jonathan Sumabat made, determine the number of sand kingdoms that now exist on the beach.

Input Specification

The first line is an integer n (1 \le n \le 10^6) representing the amount of sandcastles Jonathan Sumabat built. The next line is q (0 \le q \le 10^6) representing the number of moats Jonathan Sumabat connected. Note that he can connect the same two moats at multiple different points.
The next q lines each have two space separated integers q_1, q_2 (1 \le q_1, q_2 \le n) representing the sandcastles who's moats that Jonathan Sumabat connects.

Output Specification

Output the number of independent sand kingdoms after making all of these connections. Note that a single independent sandcastle counts as a sand kingdom.
Note: For Java, use of Scanner may lead to passable solutions timing out. Faster input reading is encouraged.

Sample Input

7
4
1 3
2 4
5 7
1 4

Sample Output

3
Jonathan Sumabat in his favorite swimsuit!

Comments

There are no comments at the moment.