Under construction

Still being built. Things may change

Constellations

Interview

Time limit1sMemory limit128 MB

Summary
Given up to 500 star coordinates, connect each star to its nearest neighbor(s) and count the connected components of the resulting graph.
Level

Medium5 of 10

Topics
Graph, Union-find, Geometry, Implementation
Solved
No attempts yet

Problem

One night, while camping out under the open sky, Big Ed was gazing at the stars. Ed had never bothered to learn the constellations, but he decided that grouping the stars together was a sensible thing to do. Being methodical, he settled on the following simple rules:

  • Every star belongs to the same constellation as its closest neighbor.
  • If the closest neighbor is not unique, then the star and all of its closest neighbors belong to the same constellation.
  • If A is in the same constellation as B, then B is in the same constellation as A.
  • If A is in the same constellation as B, and B is in the same constellation as C, then A is in the same constellation as C.

Here, "closest" is measured by the ordinary Euclidean distance between the two stars' coordinates.

For example, if the sky looked like the following:

then there are 3 constellations: {1, 2, 3, 4, 5}, {6, 7, 8}, {9, 10}.

Input

The input consists of a sequence of sky descriptions. Each description begins with a line containing a single integer nn (0<n≤5000 < n \le 500), the number of stars.

The next nn lines each give the coordinates of one star as two integers xx and yy (0≤x,y≤10000 \le x, y \le 1000).

A line containing n=0n = 0 marks the end of the input.

Output

For each sky description, print a single line of the form

Sky s contains c constellations.

where ss is the number of the sky description (starting from 1) and cc is the number of constellations.

Examples2

  1. Example 1

    Input
    10
    0 1
    16 3
    1 0
    2 7
    9 0
    4 1
    2 2
    8 1
    9 3
    15 5
    5
    10 10
    10 11
    20 10
    20 11
    15 5
    0
    
    Expected output
    Sky 1 contains 3 constellations.
    Sky 2 contains 1 constellations.
    
  2. Example 2

    Input
    4
    0 0
    1 0
    100 0
    101 0
    0
    
    Expected output
    Sky 1 contains 2 constellations.