<题目链接>

题目大意:
给定$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. [php代码审计] 哈希长度拓展攻击

    已知: 1. salt的长度. 2. message==“adminadmin”. 3. MD5(salt+message)的值. 求: MD5(salt+message+填充数据+任意字符串)的值. ...

  2. 《Webkit技术内幕》之页面渲染过程

    文章同步到github<Webkit技术内幕>之页面渲染过程 最近拜读了传说中的<Webkit技术内幕>一书,有很大收获,尤其是对页面渲染有了较深的认识.由于功力有限,而且书中 ...

  3. sublime text支持gbk编码

    sublime text支持gbk编码分两步完成 1.安装Package Control.打开Sublime Text,按Ctrl + ~打开控制台,在 https://packagecontrol. ...

  4. Python之批量读取文件【面试必学】

    python的os模块可以实现普遍的操作系统功能,并且和平台无关.以下为实现根目录下文件的批量读取. os.listdir(dirname)可以列出dirname下的目录和文件,依次读取相应的文件即可 ...

  5. 【leetcode】1031. Maximum Sum of Two Non-Overlapping Subarrays

    题目如下: Given an array A of non-negative integers, return the maximum sum of elements in two non-overl ...

  6. Jenkines邮件中添加图片

    1.在Jenkins的邮件插件 Email-ext中的Default Content内容编写html文件,简单模板如下: <html>  <head>  </head&g ...

  7. PB系统颜色值

    Colour Red Green Blue 值黑色 Black 0 0 0 0白色 White 255 255 255 16777215灰色 Gray 192 192 192 12632256深灰色 ...

  8. Leetcode_131. Palindrome Partitioning_[DFS]

    题目链接 Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  9. linux centos添加yum镜像

    下载配置文件,加入/etc/yum.repos.d/阿里镜像:http://mirrors.aliyun.com/repo/Centos-7.repo网易镜像:http://mirrors.163.c ...

  10. 【HDOJ6628】permutation 1(dfs)

    题意:求1到n的排列中使得其差分序列的字典序为第k大的原排列 n<=20,k<=1e4 思路:爆搜差分序列,dfs时候用上界和下界剪枝 #include<bits/stdc++.h& ...