From the 1996 UF ACM High School Programming Competition

Who hit whom?

Lit E. Gator is a prominent attorney who specializes in bar brawls. In order to maximize his profit, he wants to blame as many people as possible for hitting his client. Thus, he also has to find who was hit the most and by whom. He tries to convince courts that bar brawls are like runaway reactions; the blame should follow the entire chain of who hit whom.

Unfortunately (?), Mr. Gator has a very small brain. He loses track of who hit whom very quickly. Being a prominent attorney, he is rich. Being a computer science student, you are poor. There is an opportunity here...

Problem Statement

Write a program to find who ultimately hit whom. Person A is defined as hitting person B if either A hit B directly or someone A hit directly hit B.

Input will be the number of people involved along with pairs of integers indicating who hit whom. Data will be ended with a pair of zeros.

Notes

There are at most 20 people involved, and the numbering begins with 1.

Incorrect, out of range data may be given and should be acknowledged with a warning and then ignored. The examples are elaborate for explanation's sake. Your program does not need to specify the error so precisely.

Examples

Example 1:
Number of people in the scuffle? 4

Who hit whom? 1 2
Who hit whom? 2 3
Who hit whom? 4 3
Who hit whom? 0 0

Person 1 was not hit.
Person 2 was hit by person 1.
Person 3 was hit by person 1, person 2, and person 4.
Person 4 was not hit.

Example 2:
Number of people in the scuffle? 3

Who hit whom? 1 3
Who hit whom? 4 2
There are only 3 people involved, person 4 is innocent.
Who hit whom? 2 4
There are only 3 people involved, person 4 is innocent.
Who hit whom? 5 6
There are only 3 people involved, person 5 and person 6 are innocent.
Who hit whom? 3 2
Who hit whom? 0 0

Person 1 was not hit.
Person 2 was hit by person 1 and person 3.
Person 3 was hit by person 1.


Grader's Test Data

Example 3:
Number of people in the scuffle? 2

Who hit whom? 1 2
Who hit whom? 2 1
Who hit whom? 0 0

Person 1 was hit by person 2.
Person 2 was hit by person 1.

Example 4:
Number of people in the scuffle? 5

Who hit whom? 1 2
Who hit whom? 2 3
Who hit whom? 3 4
Who hit whom? 4 2
Who hit whom? 5 4
Who hit whom? 5 3
Who hit whom? 0 0

Person 1 was not hit.
Person 2 was hit by person 1, person 3, person 4, and person 5.
Person 3 was hit by person 1, person 2, person 4, and person 5.
Person 4 was hit by person 1, person 2, person 3, and person 5.
Person 5 was not hit.

Example 5:
Number of people in the scuffle? 3

Who hit whom? -1 3
People are represented by positive integers (and lawyers).
Who hit whom? 2 2
Who hit whom? 1 2
Who hit whom? 3 1
Who hit whom? 4 5
There are only 3 people involved, person 4 and person 5 are innocent.
Who hit whom? 0 0

Person 1 was hit by person 3.
Person 2 was hit by person 1, person 2, and person 3.
Person 3 was not hit.


Solutions

C solution by Jason Riedy:
hit.c