217A.md (1485B)
1 Source: https://codeforces.com/problemset/problem/217/A 2 3 A. Ice Skating 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 stdin 16 17 output 18 19 stdout 20 21 Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any sequence of moves. He now wants to heap up some additional snow drifts, so that he can get from any snow drift to any other one. He asked you to find the minimal number of snow drifts that need to be created. 22 23 We assume that Bajtek can only heap up snow drifts at integer coordinates. 24 25 Input 26 27 The first line of input contains a single integer *n* (1 ≤ *n* ≤ 100) — the number of snow drifts. Each of the following *n* lines contains two integers *x**i* and *y**i* (1 ≤ *x**i*, *y**i* ≤ 1000) — the coordinates of the *i*-th snow drift. 28 29 Note that the north direction coinсides with the direction of *Oy* axis, so the east direction coinсides with the direction of the *Ox* axis. All snow drift's locations are distinct. 30 31 Output 32 33 Output the minimal number of snow drifts that need to be created in order for Bajtek to be able to reach any snow drift from any other one. 34 35 Examples 36 37 Input 38 39 ``` 40 2 41 2 1 42 1 2 43 ``` 44 45 Output 46 47 ``` 48 1 49 ``` 50 51 Input 52 53 ``` 54 2 55 2 1 56 4 1 57 ``` 58 59 Output 60 61 ``` 62 0 63 ```