<题目链接>

题目大意:
给定$n$个任务,其中有$m$个主线任务,然后$n$个任务中,每个任务都有可能有一个list,表示完成这个任务之前必须要完成的任务,然后现在让你找出,完成这$m$个主线任务至少要完成的任务,输出这些任务。

解题分析:

建图的时候注意一下,所有任务向必须要在它之前完成的任务连一条有向边。遍历所有的主线任务,然后用DFS判一下环,注意$vis$状态要设3个,这样才能起到判断冲突的作用。

#include <bits/stdc++.h>
using namespace std; #define pb push_back
const int N = 1e5+;
#define REP(i,s,t) for(int i=s;i<=t;i++)
vector<int>vec,ans,G[N];
int vis[N];
void dfs(int u){
if(vis[u]==)return;
vis[u]=; //vis[u]=1表示当前完成u这个主线任务需要涉及到的点
for(auto v:G[u]){
if(vis[v]==)
puts("-1"),exit();
dfs(v);
}
vis[u]=; //表示这个点已经选完了
ans.pb(u);
}
int main(){
ios::sync_with_stdio(false);cin.tie();cout.tie();
int n,m;cin>>n>>m;
REP(i,,m){
int now;cin>>now;
vec.pb(now);
}
REP(u,,n){
int k;cin>>k;
while(k--){
int v;cin>>v;
G[u].pb(v);
}
}
for(auto v:vec)dfs(v);
cout<<ans.size()<<endl;
for(auto v:ans)cout<<v<<' ';
}

Codeforces 770C Online Courses In BSU (DFS)的更多相关文章

  1. codeforces Gym 100187J J. Deck Shuffling dfs

    J. Deck Shuffling Time Limit: 2   Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  2. CodeForces Gym 100500A A. Poetry Challenge DFS

    Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  3. codeforces 580C Kefa and Park(DFS)

    题目链接:http://codeforces.com/contest/580/problem/C #include<cstdio> #include<vector> #incl ...

  4. Educational Codeforces Round 5 - C. The Labyrinth (dfs联通块操作)

    题目链接:http://codeforces.com/contest/616/problem/C 题意就是 给你一个n行m列的图,让你求’*‘这个元素上下左右相连的连续的’.‘有多少(本身也算一个), ...

  5. Codeforces 791B Bear and Friendship Condition(DFS,有向图)

    B. Bear and Friendship Condition time limit per test:1 second memory limit per test:256 megabytes in ...

  6. Codeforces 455C Civilization(并查集+dfs)

    题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...

  7. Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)

    题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...

  8. Codeforces Round #290 (Div. 2) B (dfs)

    题目链接:http://codeforces.com/problemset/problem/510/B 题意:判断图中是否有某个字母成环 思路:直接dfs就好了,注意判断条件:若下一个字母与当前字母相 ...

  9. Codeforces Beta Round #88 C. Cycle —— DFS(找环)

    题目链接:http://codeforces.com/problemset/problem/117/C C. Cycle time limit per test 2.5 seconds memory ...

随机推荐

  1. 2018-9-1-win10-17025-触摸bug

    title author date CreateTime categories win10 17025 触摸bug lindexi 2018-09-01 09:50:18 +0800 2018-2-1 ...

  2. R语言——ifelse函数

    在数据处理中,经常会遇到要对判断结果做处理的情况.if函数是经常遇到的. ifelse(cond,statment1,statment2) 如果cond成立,执行statment1,否则执行statm ...

  3. Linux性能优化从入门到实战:01 Linux性能优化学习路线

      我通过阅读各种相关书籍,从操作系统原理.到 Linux内核,再到硬件驱动程序等等.   把观察到的性能问题跟系统原理关联起来,特别是把系统从应用程序.库函数.系统调用.再到内核和硬件等不同的层级贯 ...

  4. DateUtil-工具类

    /** * 类描述:时间操作定义类 */public class DateUtils{ private static final Logger logger = Logger.getLogger(Da ...

  5. 搭建vue项目并启动vue项目

    链接地址:https://blog.csdn.net/aa792978017/article/details/82939483 Vue.js是现在比较优秀的Web前端框架,下面开始从零开始搭建一个Vu ...

  6. TCP TIME_WAIT和CLOSE_WAIT

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11484451.html 使用如下指令查看当前Server的TCP状态 netstat -n | awk ...

  7. 利用xcode Build生成模拟器运行包

    真机只能运行.ipa包 模拟器上只能运行.app包 xcode中生成.app包步骤: 启动xcode IDE,打开gigold源码工程 [project]——[gigold]——[Basic]:修改V ...

  8. simulate 中的一些操作

    1. neutralize: position based 的alpha int neutralize(int di, int ti) { ; ; ; ; ii < nsyms; ++ii) { ...

  9. 英语单词omitting

    omitting 来源——报错 [root@centos7 ~]# cp /etc/ /bin cp: omitting directory ‘/etc/’ [root@centos7 ~]# cp ...

  10. Dubbo学习-4-dubbo简单案例-1

    模拟一个需求,通过dubbo实现RPC调用: 这里用户服务模块的查询用户地址的功能,就是一个服务提供者,而订单服务模块的创建订单模块就是一个服务消费者: 1. 创建服务提供者的maven工程:user ...