In ancient times, when alchemists were searching for gold, the world was familiar with a total of
Joško is a modern alchemist and has
Input Specification
The first line of input contains two integers
The second line of input contains
The third line of input contains the integer
The following
- The first line contains the integers
and . - The second line contains
distinct integers . - The third line contains
distinct integers . - This describes the reaction with which the substance set
transforms into substance set .
The sum of all
The sum of all
Output Specification
The first line of output must contain the integer
The second line of output must contain
Scoring
In test cases worth
.- The sum of all
values and the sum of all values won't exceed .
Sample Input 1
4 2
1 2
2
2 1
1 2
3
2 1
1 3
4
Sample Output 1
4
1 2 3 4
Explanation for Sample Output 1
There are 2 reactions.
The first reaction transforms substance set
The second reaction transforms substance set
Joško initially has substances from the set
Using the first reaction, Joško can obtain substance 3, after which he has substances from the set
After that, using the second reaction, he can also obtain substance 4.
Sample Input 2
6 3
1 4 5
3
3 2
2 3 4
1 6
1 3
4
1 5 6
1 1
6
2
Sample Output 2
5
1 2 4 5 6
Explanation for Sample Output 2
Joško initially has substances from the set
Using the second reaction, it is possible to obtain substance 6, after which it is possible to apply the third reaction, giving substance 2.
The first reaction is impossible to apply because Joško doesn't have substance 3.
Comments
I don't see how the reactions are described in the input? sample #2
How can I interpret this? Thx
The first line you put(below the ...... line) represents L and R.
The next line after that contains L space-separated integers which would be the initial substance.
The line after would contain R space-separated integers which would be the result of the reaction of the initial substance.
You've copied two reactions from the sample input, so let's look at them both.
The first reaction basically tells us that one element, 4, will react to form three elements 1, 5, and 6.
The second reaction tells us that one element, 6, will react to form one element, 2.
Hope this helps :)
Thanks for replying, but I still confused about how the reactions are made?
I see how you're interpreting it. The numbers you put in the reactions are actually still part of L, R, X, Y, etc.
Since K = 3, there are three known reactions, so the next 3K lines, or 9 lines, would describe those reactions. The six lines of which you are unsure of still deals with the reactions, which I explained in the earlier comment.
Hope this helps :)