题库练习 [COCI-2017_2018-contest6]#1 Alkemija
← 上一题 下一题 →

A1367 | [COCI-2017_2018-contest6]#1 Alkemija

来源COCI
时间限制1s
内存限制128MB
通过 / 提交0/0

题目描述

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 {X1, X2, …, XL} to another substance set {Y1, Y2, …, YR}. 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 A1, A2, …, AM. 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.

输入格式

The first line of input contains two integers N and M (1 ≤ M ≤ N ≤ 100 000), the numbers from the task.
The second line of input contains M integers Ai (1 ≤ Ai ≤ N), labels of the substances Joško has in the beginning.
The third line of input contains the integer K (1 ≤ K ≤ 100 000), the number of known reactions.
The following 3·K 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 ≤ L, R ≤ N).
● The second line contains L distinct integers Xi (1 ≤ Xi ≤ N).
● The third line contains R distinct integers Yi (1 ≤ Yi ≤ N).
● This describes the reaction with which the substance set {X1, X2, …, XL} transformsinto substance set {Y1, Y2, … YR}.
The sum of all L values won’t exceed 100 00
0.
The sum of all R values won’t exceed 100 00
0.

输出格式

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 Bi , sorted ascendingly, that represent the labels of the obtainable substances.

输入输出样例

输入 #1
4 2
1 2
2
2 1
1 2
3
2 1
1 3
4
输出 #1
4
1 2 3 4
输入 #2
6 3
1 4 5
3
3 2
2 3 4
1 6
1 3
4
1 5 6
1 1
6
2
输出 #2
5
1 2 4 5 6
C++ 编辑器
输入
输出