CF770C.Online Courses In BSU
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Now you can take online courses in the Berland State University! Polycarp needs to pass k main online courses of his specialty to get a diploma. In total n courses are availiable for the passage.
The situation is complicated by the dependence of online courses, for each course there is a list of those that must be passed before starting this online course (the list can be empty, it means that there is no limitation).
Help Polycarp to pass the least number of courses in total to get the specialty (it means to pass all main and necessary courses). Write a program which prints the order of courses.
Polycarp passes courses consistently, he starts the next course when he finishes the previous one. Each course can't be passed more than once.
现在,你可以在贝尔兰国立大学参加在线课程!波利卡普需要通过他专业的 k 门核心在线课程才能获得毕业证书。总共有 n 门课程可供选择。
由于在线课程之间存在依赖关系,情况变得复杂:对每门课程,都列出了在开始学习该课程之前必须先通过的课程(该列表可以为空,表示没有前置限制)。
请帮助波利卡普以最少的总课程数完成专业要求(即通过所有核心课程及必要的前置课程)。编写一个程序,输出课程的学习顺序。
波利卡普按顺序学习课程:只有在完成前一门课程后,才开始学习下一门课程。每门课程最多只能通过一次。
输入格式
The first line contains n and k (1 ≤ k ≤ n ≤ 105) — the number of online-courses and the number of main courses of Polycarp's specialty.
The second line contains k distinct integers from 1 to n — numbers of main online-courses of Polycarp's specialty.
Then n lines follow, each of them describes the next course: the i-th of them corresponds to the course i. Each line starts from the integer t__i (0 ≤ t__i ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of t__i distinct integers from 1 to n — numbers of courses in random order, on which the i-th depends. It is guaranteed that no course can depend on itself.
It is guaranteed that the sum of all values t__i doesn't exceed 105.
第一行包含两个整数 n 和 k(1≤k≤n≤105)—— 分别表示在线课程的总数以及 Polycarp 专业主修课程的数量。
第二行包含 k 个互不相同的整数,取值范围为 1 到 n —— 表示 Polycarp 专业的主修在线课程的编号。
接下来是 n 行,每行描述一门课程:其中第 i 行对应课程 i。每行以整数 ti(0≤ti≤n−1)开头 —— 表示课程 i 所依赖的课程数量;随后是 ti 个互不相同的整数(取值范围为 1 到 n),以任意顺序给出课程 i 所依赖的课程编号。保证没有任何课程依赖于自身。
保证所有 ti 的总和不超过 105。
输出格式
Print -1, if there is no the way to get a specialty.
Otherwise, in the first line print the integer m — the minimum number of online-courses which it is necessary to pass to get a specialty. In the second line print m distinct integers — numbers of courses which it is necessary to pass in the chronological order of their passage. If there are several answers it is allowed to print any of them.
如果无法获得该专业,则输出 -1。
否则,第一行输出整数 m —— 获得该专业所需通过的在线课程的最少门数;第二行输出 m 个互不相同的整数 —— 所需通过的课程编号,按学习顺序排列。若存在多种答案,输出任意一种即可。
输入输出样例
输入#1
6 2 5 3 0 0 0 2 2 1 1 4 1 5
输出#1
5 1 2 3 4 5
输入#2
9 3 3 9 5 0 0 3 9 4 5 0 0 1 8 1 6 1 2 2 1 2
输出#2
6 1 2 9 4 5 3
输入#3
3 3 1 2 3 1 2 1 3 1 1
输出#3
-1
说明/提示
In the first test firstly you can take courses number 1 and 2, after that you can take the course number 4, then you can take the course number 5, which is the main. After that you have to take only the course number 3, which is the last not passed main course.
在第一次测试中,首先你可以选择课程 1 和课程 2;之后你可以选择课程 4;接着你可以选择课程 5(它是主修课程);此后,你只需再选择课程 3,这是最后一门尚未通过的主修课程。
输入解题思路,AI测评打分。不知道怎么写?