CodeForces 770C 拓扑排序

http://codeforces.com/problemset/problem/770/C

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.

Input

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 ti (0 ≤ ti ≤ n - 1) — the number of courses on which the i-th depends. Then there follows the sequence of ti 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 ti doesn’t exceed 105.

Output

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.

Examples

Input

6 2
5 3
0
0
0
2 2 1
1 4
1 5

Output

5
1 2 3 4 5 

Input

9 3
3 9 5
0
0
3 9 4 5
0
0
1 8
1 6
1 2
2 1 2

Output

6
1 2 9 4 5 3 

Input

3 3
1 2 3
1 2
1 3
1 1

Output

-1

Note

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.

题目大意:有n门课程,其中有k门是必选课,然后部分课程有先选课,要学完先选课才能学这个课程。让你求学完k门必选课需要上的最少课程数。有环的时候就输出-1。

思路:借助DFS完成拓扑排序,这在刘汝佳的白书168页上面有。因为题目只考虑必选课,因此我们只对必选课调用dfs即可得到拓扑序列。注意一下如果u是v的先选课,那么我们令v指向u,这样才能得到正确的拓扑序列。不理解的可以自己画个图,看看反向的是什么情况。

#include<iostream>
#include<cstdio>
#include<stack>
#include<cmath>
#include<cstring>
#include<queue>
#include<set>
#include<algorithm>
#include<iterator>
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

const int maxn=100005;

struct edge
{
	int to,next;
};
edge E[maxn];
int head[maxn];
int vis[maxn];//-1表示正在访问子节点 0 表示还未访问 1表示已访问过
int n,m,k;
int a[maxn];
int order[maxn];
int num=0;

bool dfs(int u);
bool topo();

int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=k;i++)
		scanf("%d",&a[i]);
	int t1,t2;
	int cnt=0;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&t1);
		for(int j=1;j<=t1;j++)//想学i 要先学的课程
		{
			scanf("%d",&t2);
			E[++cnt].to=t2;
			E[cnt].next=head[i];
			head[i]=cnt;
		}
	}
	if(topo())
	{
		printf("%d\n",num);
		printf("%d",order[1]);
		for(int i=2;i<=num;i++)
			printf(" %d",order[i]);
		printf("\n");
	}
	else
		printf("-1\n");
	return 0;
}

bool dfs(int u)	//dfs完成拓扑排序
{
	vis[u]=-1;
	for(int i=head[u];i!=0;i=E[i].next)
	{
		int temp=E[i].to;
		if(vis[temp]==-1)	//两个结点互成子结点 有环	推出
			return false;
		else if(!vis[temp]&&!dfs(temp))	//子结点未访问过就访问子结点 若子结点有环 推出
			return false;
	}
	vis[u]=1;	//访问完毕
	order[++num]=u;	//拓扑序列
	return true;
}

bool topo()
{
	for(int i=1;i<=k;i++)
		if(!vis[a[i]]&&!dfs(a[i]))	//还没访问过就访问 如果访问结果是有环 就退出
			return false;
	return true;
}

 

    原文作者:拓扑排序
    原文地址: https://blog.csdn.net/xiji333/article/details/86660205
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞