Codeforces 770C Online Courses In BSU (DFS)
<题目链接>
题目大意:
给定$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)的更多相关文章
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- CodeForces Gym 100500A A. Poetry Challenge DFS
Problem A. Poetry Challenge Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- codeforces 580C Kefa and Park(DFS)
题目链接:http://codeforces.com/contest/580/problem/C #include<cstdio> #include<vector> #incl ...
- Educational Codeforces Round 5 - C. The Labyrinth (dfs联通块操作)
题目链接:http://codeforces.com/contest/616/problem/C 题意就是 给你一个n行m列的图,让你求’*‘这个元素上下左右相连的连续的’.‘有多少(本身也算一个), ...
- 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 ...
- Codeforces 455C Civilization(并查集+dfs)
题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...
- Codeforces Codeforces Round #383 (Div. 2) E (DFS染色)
题目链接:http://codeforces.com/contest/742/problem/E 题意: 有一个环形的桌子,一共有n对情侣,2n个人,一共有两种菜. 现在让你输出一种方案,满足以下要求 ...
- Codeforces Round #290 (Div. 2) B (dfs)
题目链接:http://codeforces.com/problemset/problem/510/B 题意:判断图中是否有某个字母成环 思路:直接dfs就好了,注意判断条件:若下一个字母与当前字母相 ...
- Codeforces Beta Round #88 C. Cycle —— DFS(找环)
题目链接:http://codeforces.com/problemset/problem/117/C C. Cycle time limit per test 2.5 seconds memory ...
随机推荐
- vue 组件间传值方式
/* 父组件给子组件传值 1.父组件调用子组件的时候 绑定动态属性 <v-header :title="title"></v-header> 2.在子组件里 ...
- 搭建Keepalived+LNMP架构web动态博客 实现高可用与负载均衡
环境准备: 192.168.193.80 node1 192.168.193.81 node2 关闭防火墙 [root@node1 ~]# systemctl stop firewalld #两台都 ...
- LinuxC语言实现ATM取款机实验Socket
链接:https://pan.baidu.com/s/1sZt4qhYc6CDJVpoJHbtw-Q 提取码:53ot 复制这段内容后打开百度网盘手机App,操作更方便哦 本实验用的是Centos7t ...
- python常用模块学习3
# # dic='{"name":"tang"}' # # f=open("hello",'w') # # f.write(dic) # # ...
- 1085. PAT单位排行 (25)
每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜.本题就请你实现这个功能. 输入格式: 输入第一行给出一个正整数N(<=105),即考生人数.随后N行,每行按下列格式给出一个考生的信 ...
- [CF] E. Camels
CF 2000 的dp题目还是有点难qwq 题意: 一行有\(n\)个空位,每个空位可以填\([1,4]\)的整数,要求: 1.有\(t\)个位置满足 \(ai−1<ai>ai+1(1&l ...
- CSS3 Transform实例
移动translate <!doctype html> <html> <head> <meta charset="utf-8"> & ...
- centos上部署flask项目之环境配置-MySQL的安装
1.添加mysql 的yum源 wget 'https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm' rpm ...
- iOS----实现scrollView或者scrollView的子类下拉图片放大的效果
代码是通过Tableview来说明的,用在其他情况下同样适用 - (void)viewDidLoad { [super viewDidLoad]; _imageview = [[UIImageView ...
- TextView控件常用属性
常用属性 android:id——控件ID android:layout_width——控件宽度 android:layout_height——控件高度 android:text——文本内容 andr ...