A10816 | Online Courses In BSU
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
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.
输入格式
The first line contains $n$ and $k$ ( $1<=k<=n<=10^{5}$ ) — 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 $10^{5}$ .
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 $10^{5}$ .
输出格式
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.
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
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted