COCI '17 Contest 6 #2 Alkemija

View as PDF

Submit solution

Points: 10 (partial)
Time limit: 1.0s
Memory limit: 64M

Problem types

In ancient times, when alchemists were searching for gold, the world was familiar with a total of N distinct substances, denoted with 1 to N. During many years of hard work, searching for the secret formula, alchemists discovered a series of interesting regularities - alchemical reactions. In one reaction, it's possible to transform substance set \{X_1, X_2, \dots, X_L\} to another substance set \{Y_1, Y_2, \dots, Y_R\}. For example, the substance set \{1, 4, 5\} might react once and create the new substance set \{2, 6\}.

Joško is a modern alchemist and has M distinct substances denoted with A_1, A_2, \dots, A_M. He has an unlimited quantity of each substance from that set. Joško wants to know which substances he can create using a list of reactions of ancient alchemists, so he's asking you to help him solve this problem.

Input Specification

The first line of input contains two integers N and M (1 \le M \le N \le 100\,000), the numbers from the task.

The second line of input contains M integers A_i (1 \le A_i \le N), labels of the substances Joško has in the beginning.

The third line of input contains the integer K (1 \le K \le 100\,000), the number of known reactions.

The following 3K lines contain a list of reactions. Each reaction is described with 3 lines in the following way:

  • The first line contains the integers L and R (1 \le L, R \le N).
  • The second line contains L distinct integers X_i (1 \le X_i \le N).
  • The third line contains R distinct integers Y_i (1 \le Y_i \le N).
  • This describes the reaction with which the substance set \{X_1, X_2, \dots, X_L\} transforms into substance set \{Y_1, Y_2, \dots, Y_R\}.

The sum of all L values won't exceed 100\,000.

The sum of all R values won't exceed 100\,000.

Output Specification

The first line of output must contain the integer X, the number of obtainable substances.

The second line of output must contain X distinct integers B_i, sorted ascendingly, that represent the labels of the obtainable substances.

Scoring

In test cases worth 60\% of total points, it will hold:

  • N, K \le 500.
  • The sum of all L values and the sum of all R values won't exceed 500.

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 \{1, 2\} into substance set \{3\}.

The second reaction transforms substance set \{1, 3\} into substance set \{4\}.

Joško initially has substances from the set \{1, 2\}.

Using the first reaction, Joško can obtain substance 3, after which he has substances from the set \{1, 2, 3\}.

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 \{1, 4, 5\}.

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


  • 0
    MouradNouaili  commented on Jan. 2, 2022, 8:42 p.m. edit 3

    I don't see how the reactions are described in the input? sample #2

    ......
    1 3
    4
    1 5 6
    1 1
    6
    2

    How can I interpret this? Thx


    • 0
      Spitfire720  commented on Jan. 2, 2022, 10:21 p.m.

      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 :)


      • 0
        MouradNouaili  commented on Jan. 9, 2022, 5:15 p.m.

        Thanks for replying, but I still confused about how the reactions are made?


        • 0
          Spitfire720  commented on Jan. 9, 2022, 7:48 p.m.

          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 :)