<题目链接>

题目大意:
给定$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. srs-librtmp pusher(push h264 raw)

    Simple Live System Using SRS https://www.cnblogs.com/dong1/p/5100792.html 1.上面是推送文件,改成推送缓存 封装了三个函数 i ...

  2. CentOS6.5系统解决中文乱码问题

      一.    问题详情 Windows的默认编码为GBK,Linux的默认编码为UTF-8.在Windows下编辑的中文,在Linux下显示为乱码.为了解决此问题,修改Linux的默认编码为GBK. ...

  3. maven_上传到私服,以及从私服下载

    公司由于没有maven,自己又想用,于是乎,就自己搭了一个nexus 1.苦逼不多说,将本地jar文件上传到maven 需要在本机(客户端windows)中的maven中的setting.xml添加这 ...

  4. linux系统安装telnet服务

    linux安装telnet 安装前准备工作 1.安装telnet服务需要三个软件包:telnet.telnet-server和xinetd包. telnet,telnet-sever,xinetd软件 ...

  5. maven 提取jar包 依赖及打包排除

    <properties> <project.targetDir>D:\jar</project.targetDir> <project.targetServe ...

  6. Flutter-stack層疊樣式

    alignment調整佈局 var stack = new Stack( alignment: Alignment.center,//元素居中 //alignment: Alignment (1,1) ...

  7. django之路由的理解

    一:路由 简单的路由过程图: 1. 路由的定义位置 路由定义方式一:主路由和子路由分开定义 主路由的定义 urls.py from django.conf.urls import url from d ...

  8. prototype的用法

    定义: prototype 属性使您有能力向对象添加属性和方法. 语法: object.prototype.name=value 实例: function prot(){ this.name = 'J ...

  9. Minor GC、Major GC、Full GC 区别

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11488036.html Minor GC 清理年轻代 Minor GC指新生代GC,即发生在新生代(包 ...

  10. poj 3468 : A Simple Problem with Integers 【线段树 区间修改】

    题目链接 题目是对一个数组,支持两种操作 操作C:对下标从a到b的每个元素,值增加c: 操作Q:对求下标从a到b的元素值之和. #include<cstdio> #include<c ...