春季拓扑补题

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.
第一排输入总共课程数目和必修课数目,第二排是必修课,然后剩下n排分别表示每一门课的前置课程,
不过并没有用到拓扑。。。
dfs遍历前置课程,用一个visit数组来判断是否成环,并且最后输出的时候不用遍历已经访问过的必修课程。

#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
vector<int> edge[100005];
int n,k,key[100005],vis[100005],cout,flag,ans[100005];
void dfs(int x)
{
	if(flag==0)
	return;
    if(vis[x]==0)
	{
		vis[x]=1;
		for(int i=0;i<edge[x].size();i++)
	    {
		    dfs(edge[x][i]);
	    }	
	    vis[x]=2;
	    ans[cout++]=x;
	}  
	else if(vis[x]==1)
	flag=0;
	return;
}
int main()
{
	cout=0,flag=1;
	scanf("%d%d",&n,&k);
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=k;i++)
	scanf("%d",key+i);

	for(int i=1;i<=n;i++)
	{
		int num;
		scanf("%d",&num);
		for(int j=0;j<num;j++)
		{
			int temp;
			scanf("%d",&temp);
			edge[i].push_back(temp);
		}
	}
	for(int i=1;i<=k;i++)
	{
		if(flag==0)
		break;
		if(vis[key[i]]>0)
		continue;
		dfs(key[i]);
	}
	if(flag)
	{
		printf("%d\n",cout);
		for(int i=0;i<cout-1;i++)
		printf("%d ",ans[i]);
		printf("%d\n",ans[cout-1]);
	}
	else
	printf("-1\n");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值