#C7460. Matchmaking Pairing Mechanism

    ID: 51334 Type: Default 1000ms 256MiB

Matchmaking Pairing Mechanism

Matchmaking Pairing Mechanism

You are tasked with developing a pairing mechanism for a matchmaking service. Each individual is described by a unique ID, age, three partner preferences, and a location specified by Cartesian coordinates. Two individuals can form a valid pair if they satisfy all the following conditions:

  • The absolute difference in ages is at most 7 years (i.e. \(|age_1 - age_2| \le 7\)).
  • The Euclidean distance between their locations is at most 50 units (i.e. \(\sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2} \le 50\)).
  • They share at least one common preference among the three provided by each individual.

The input consists of one or more datasets. For each dataset, your program should output a single integer denoting the number of valid pairs that can be formed following the above constraints.

Note that the end of input is indicated by a line containing a single zero.

inputFormat

The input is read from stdin and consists of multiple datasets. Each dataset begins with an integer n (1 ≤ n ≤ 500), representing the number of individuals. This is followed by n lines, each containing the following space-separated values:

  1. id: An integer representing the unique ID.
  2. age: An integer representing the age.
  3. preference1, preference2, preference3: Three strings representing partner preferences (each string is an alphanumeric sequence without spaces).
  4. x, y: Two decimal numbers representing the Cartesian coordinates of the individual.

A line containing a single zero indicates the end of all datasets.

outputFormat

For each dataset provided in the input, output a single integer on a new line representing the number of valid pairs that satisfy all the constraints.

Output is written to stdout.

## sample
4
101 25 hiking photography reading 10.0 20.0
102 27 hiking cooking music 15.0 24.0
103 30 fitness music drawing 14.0 13.0
104 35 reading traveling hiking 40.0 50.0
0
2

</p>