#P4100. Backup Chef Robots Pairing
Backup Chef Robots Pairing
Backup Chef Robots Pairing
The Galaxy Team’s roster is out! As the specially appointed nutritionist, you are responsible for ensuring that the competing astronauts receive the right diet during the long journey to a distant planet. The journey may be long enough to affect body conditions, so the menu must be carefully arranged day by day.
To this end, you have prepared two sets of chef robots. Each set consists of n robots. Each robot can only cook one dish. Specifically, the i-th robot of the first set produces a dish such that one pound provides \(x_i\) micrograms of the i-th nutrient. Moreover, every robot is equipped with an antidote mechanism: by inputting a number, the robot can produce a dose of medicine that counteracts the nutritional content of that dish in the same proportion.
Since the journey is unpredictable, you assign a backup from the second set to each robot in the first set. If any first-set robot fails during the journey, its designated backup from the second set will replace it. With this replacement, the complete set of n robots must still be capable of producing any combination of nutrient requirements. Mathematically, if the nutritional capability of the functioning set is represented by \(a_1, a_2, \dots, a_n\) where normally \(a_i = x_i\) but when the i-th robot fails it is replaced by \(y_{f(i)}\), the system can meet any nutritional demand if and only if the matrix formed by these values is invertible. Since each robot provides only its specific nutrient, this condition is equivalent to requiring that every available nutrient value is nonzero, i.e., \[ \prod_{j \neq i} x_j \times y_{f(i)} \neq 0 \quad \text{for every } i=1,2,\dots,n. \]
Your task is to determine a valid backup pairing. In other words, given the two arrays \(x_1,x_2,\dots,x_n\) and \(y_1,y_2,\dots,y_n\), if all the first-set values and the chosen backup values are nonzero then any nutrient vector can be assembled. If any first-set value \(x_i\) or any backup value \(y_j\) is zero, then a valid pairing does not exist. When a valid pairing exists, output a permutation \(f\) of \(\{1,2,\dots,n\}\) as the backup assignment (the simplest valid pairing is the identity mapping).
inputFormat
The input consists of three lines:
- The first line contains an integer \(n\) (the number of nutrient types and robots in each set).
- The second line contains \(n\) space-separated integers \(x_1, x_2, \dots, x_n\), where \(x_i\) is the nutritional value provided (in micrograms per pound) by the i-th robot in the first set.
- The third line contains \(n\) space-separated integers \(y_1, y_2, \dots, y_n\), where \(y_i\) is the nutritional value provided by the i-th robot in the second set.
All values are guaranteed to be integers.
outputFormat
If a valid backup pairing exists, output a single line containing \(n\) integers. The \(i\)-th integer should be the index of the backup robot from the second set assigned to the i-th robot of the first set. If multiple valid pairings exist, any one is acceptable. If no valid pairing exists (i.e. if any \(x_i\) equals 0 or if any \(y_j\) equals 0), output a single line containing -1
.
sample
3
2 3 4
5 6 7
1 2 3