#K81602. Item Distribution Based on Preferences
Item Distribution Based on Preferences
Item Distribution Based on Preferences
You are given an integer N and an N × N matrix representing the preferences of N participants. The ith row of the matrix contains the ranked preferences for the ith participant, where the first element is the most preferred item and the last element is the least preferred.
Your task is to assign each participant the first available item from their list so that no item is assigned more than once. Formally, let \( assigned[k] \) indicate whether item \( k \) has been assigned. For each participant \( i \) (in order from \( 1 \) to \( N \)), assign the smallest index \( j \) (from \( 1 \) to \( N \)) such that the \( jth \) preferred item is not yet assigned. Print the resulting assignment as a sequence of N integers, where the ith integer is the item assigned to the ith participant.
Note: All items are labeled from 1 to N.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer N, the number of participants.
- The next N lines each contain N space-separated integers. The ith of these lines represents the preferences of the ith participant.
outputFormat
Output to standard output (stdout) a single line containing N space-separated integers representing the assigned item for each participant in order.
## sample4
1 2 3 4
4 1 2 3
3 4 1 2
2 3 4 1
1 4 3 2