#C583. Participant Rearrangement

    ID: 49522 Type: Default 1000ms 256MiB

Participant Rearrangement

Participant Rearrangement

A company is organizing an event where participants are arranged in a single line. Each participant is assigned a unique ID from 1 to n. The event planner wishes to shuffle the order of the participants based on certain ordering constraints. You are given an integer n and a list of m pairs. Each pair \( (a, b) \) indicates that the participant with ID \( a \) must appear before the participant with ID \( b \) in the final arrangement.

Your task is to find one valid ordering of the participants that satisfies all the given constraints. Specifically, you should use a topological sorting approach. If there exists a valid ordering, output the sequence of participant IDs in that order; otherwise, output an empty list represented as [].

Note: When there are no constraints (i.e., m = 0), the participants should be arranged in increasing order of their IDs.

inputFormat

The input is given via standard input (stdin) in the following format:

  • The first line contains an integer \( n \) denoting the number of participants.
  • The second line contains an integer \( m \) representing the number of ordering constraints.
  • The next \( m \) lines each contain two space-separated integers \( a \) and \( b \) indicating that participant \( a \) must come before participant \( b \).

outputFormat

Output the valid rearranged order of the participants as a single line of space-separated integers. If no valid ordering exists (i.e. due to a cycle in the constraints), output [] (without quotes).

## sample
4
3
1 2
2 3
4 1
4 1 2 3

</p>