computer science students are having a snowball fight!
Each student has a list of targets, initially having one known target.
For every round in the fight, each student in the order will throw a snowball to the first student in their target list, then remove the student from the front of their list. If their list is empty, they will do nothing.
There may be multiple occurrences of the same student in a student's list of targets. If the target student is on the list multiple times, only the first occurrence should be removed.
When all people have finished throwing or doing nothing for the round, students that had snowballs thrown at them this round will add whoever threw a snowball at them to the end of their list, in increasing order. For example, if students and threw a snowball to student , student would add students and to the end of their list, in that order.
This will be repeated for rounds, after which all the students will stop.
Determine the last student each student threw a snowball to.
Constraints
For this problem, you will be required to pass all the samples in order to receive points. In addition, you must pass all previous subtasks to earn points for a specific subtask.
Subtask 1 [4/15]
Subtask 2 [11/15]
No additional constraints.
Input Specification
The first line will contain and , space-separated.
The second and last line will contain space-separated integers between and inclusive, the known initial target of the -th student. No student will target themselves.
Output Specification
Output space-separated integers between and inclusive, with the -th integer denoting the student that the -th student threw their last snowball to.
Sample Input
4 3
4 1 1 2
Sample Output
3 1 1 2
Explanation
In the first round, student throws a snowball to student , students and throw a snowball to student , and student throws a snowball to student .
The new target list looks like
In the second round, student throws a snowball to student , student throws a snowball to student , student does nothing, and student throws a snowball to student .
The new target list looks like
In the third round, student throws a snowball to student , student throws a snowball to student , student does nothing, and student throws a snowball to student .
Comments