#C7023. Event Organization
Event Organization
Event Organization
You are presented with a series of events, each of which may have certain prerequisites. Your job is to determine an ordering of these events such that every event occurs after all of its prerequisite events. If there is more than one valid ordering, output any one of them. However, if no ordering can satisfy the prerequisites (i.e. there is a cycle in dependencies), then output impossible
.
Formally, you are given an integer \( n \) (\(1 \le n \le 100\)) representing the number of events, followed by \( n \) lines. Each line contains an event name (a string of up to 20 characters) followed by zero or more prerequisites. If an event has no prerequisites, it is followed by the character -
.
Use \( \LaTeX \) format for any formulas.
inputFormat
The input is read from stdin
and is structured as follows:
- The first line contains a single integer \( n \) denoting the number of events.
- The next \( n \) lines each represent an event. Each line starts with the event name followed by either a '-' (if there are no prerequisites) or a list of prerequisite event names separated by spaces.
outputFormat
Output a single line to stdout
. This line should contain a valid ordering of the event names separated by a single space. If no valid ordering exists because the prerequisites form a cycle, output impossible
.
4
EventA -
EventB EventA
EventC EventA
EventD EventB EventC
EventA EventB EventC EventD