# [CodeForces 770C] Online Courses In BSU

cf 770C

## 思路

拓扑+dfs


## 题解

#include <algorithm>
#include <string>
#include <iostream>
#include <set>
#include <vector>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <stack>

int vis[100005];
std::vector<int> dpd[100005]; //保存依赖关系
std::vector<int> main_course; //
std::queue<int> ans;
bool dfs(int u)
{
vis[u] = -1;
for (auto x : dpd[u])
{

if (vis[x] == -1) return false;
if (!vis[x] && !dfs(x)) return false;
}
vis[u] = 1;
ans.push(u);
return true;
}
bool topu()
{
memset(vis, 0, sizeof(vis));
for (auto x : main_course)
if (!vis[x] && !dfs(x)) return false;
return true;

}
int main()
{
int n, k;
std::cin >> n >> k;
int temp;
for (int i = 0; i < k; i++)
{
scanf("%d", &temp);
main_course.push_back(temp);
}
for (int i = 1; i <= n; i++)
{
int t;
scanf("%d", &t);
if (t)
{
int tt;
for (int j = 1; j <= t; j++)
{
scanf("%d", &tt);
dpd[i].push_back(tt);
}
}
}
if (topu())
{
std::cout << ans.size() << std::endl;
while (!ans.empty())
{
printf("%d ", ans.front());
ans.pop();
}
}
else
printf("-1\n");
}

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试