codeforces164A
Variable, or There and Back Again
CodeForces - 164A
Life is not easy for the perfectly common variable named Vasya. Wherever it goes, it is either assigned a value, or simply ignored, or is being used!
Vasya's life goes in states of a program. In each state, Vasya can either be used (for example, to calculate the value of another variable), or be assigned a value, or ignored. Between some states are directed (oriented) transitions.
A path is a sequence of states v1, v2, ..., vx, where for any 1 ≤ i < x exists a transition from vi to vi + 1.
Vasya's value in state v is interesting to the world, if exists path p1, p2, ..., pksuch, that pi = v for some i (1 ≤ i ≤ k), in state p1 Vasya gets assigned a value, in state pk Vasya is used and there is no state pi (except for p1) where Vasya gets assigned a value.
Help Vasya, find the states in which Vasya's value is interesting to the world.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the numbers of states and transitions, correspondingly.
The second line contains space-separated n integers f1, f2, ..., fn (0 ≤ fi ≤ 2), fidescribed actions performed upon Vasya in state i: 0 represents ignoring, 1 — assigning a value, 2 — using.
Next m lines contain space-separated pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi), each pair represents the transition from the state number ai to the state number bi. Between two states can be any number of transitions.
Output
Print n integers r1, r2, ..., rn, separated by spaces or new lines. Number ri should equal 1, if Vasya's value in state i is interesting to the world and otherwise, it should equal 0. The states are numbered from 1 to n in the order, in which they are described in the input.
Examples
4 3
1 0 0 2
1 2
2 3
3 4
1
1
1
1
3 1
1 0 2
1 3
1
0
1
3 1
2 0 1
1 3
0
0
0
Note
In the first sample the program states can be used to make the only path in which the value of Vasya interests the world, 1 2
3
4; it includes all the states, so in all of them Vasya's value is interesting to the world.
The second sample the only path in which Vasya's value is interesting to the world is , — 1 3; state 2 is not included there.
In the third sample we cannot make from the states any path in which the value of Vasya would be interesting to the world, so the value of Vasya is never interesting to the world.
题意:好像就是从所有1走到2,的路径覆盖的点答案是1,否则答案是0
sol:很容易发现就是减一下反图,分别从1和2开始bfs
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=; bool f=; char ch=' ';
while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();}
while(isdigit(ch)) {s=(s<<)+(s<<)+(ch^); ch=getchar();}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<) {putchar('-'); x=-x;}
if(x<) {putchar(x+''); return;}
write(x/); putchar((x%)+'');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=,M=;
int n,m,a[N];
int tot=,Next[M],to[M],head[N];
bool arr1[N],arr2[N];
inline void Link(int x,int y)
{
Next[++tot]=head[x]; to[tot]=y; head[x]=tot;
}
#define PB push_back
vector<int>E[N];
int main()
{
queue<int>Que;
int i,e,x,y;
R(n); R(m);
for(i=;i<=n;i++) R(a[i]);
for(i=;i<=m;i++)
{
R(x); R(y); Link(x,y); E[y].PB(x);
}
memset(arr1,,sizeof arr1);
for(i=;i<=n;i++) if(a[i]==) {Que.push(i); arr1[i]=;}
while(!Que.empty())
{
x=Que.front(); Que.pop();
for(e=head[x];e;e=Next[e])
{
if(arr1[to[e]]) continue;
arr1[to[e]]=; Que.push(to[e]);
}
}
while(!Que.empty()) Que.pop();
memset(arr2,,sizeof arr2);
for(i=;i<=n;i++) if(a[i]==) {Que.push(i); arr2[i]=;}
while(!Que.empty())
{
x=Que.front(); Que.pop();
for(i=;i<E[x].size();i++)
{
if(a[E[x][i]]==) {arr2[E[x][i]]=; continue;}
if(arr2[E[x][i]]) continue;
arr2[E[x][i]]=; Que.push(E[x][i]);
}
}
for(i=;i<=n;i++) {if(arr1[i]&&arr2[i]) puts(""); else puts("");}
return ;
}
codeforces164A的更多相关文章
随机推荐
- docker registry-v2 搭建私有仓库
参考官方文档:https://docs.docker.com/registry/deploying/ 参考 :http://www.tuicool.com/articles/6jEJZj 本例子使用两 ...
- SVN_02安裝
1.下载 TortoiseSVN https://tortoisesvn.net/downloads.html 2.下载 VIsualSVN https://www.visualsvn.com ...
- React 脚手架支持Typescript和Sass
首先,创建React工程目录,以及选择Typescript版本 进入在my-app目录,安装node-sass 然后再安装webpack的sass-loader 接下来进入node_modules ...
- 3_PHP表达式_2_变量
以下为学习孔祥盛主编的<PHP编程基础与实例教程>(第二版)所做的笔记. PHP变量可分为自定义变量和预定义变量. 以下所谈到的变量均为自定义变量. 1.变量的基本概念 PHP的变量名遵循 ...
- 理解Java序列化中的SerialVersionUid
一.前言 SerialVersionUid,简言之,其目的是序列化对象版本控制,有关各版本反序列化时是否兼容.如果在新版本中这个值修改了,新版本就不兼容旧版本,反序列化时会抛出InvalidClass ...
- @app.route源码流程分析
@app.route(), 是调用了flask.app.py文件里面的Flask类的route方法,route方法所做的事情和add_url_rule类似,是用来为一个URL注册一个视图函数,但是我们 ...
- 【MySQL】数据库(分库分表)中间件对比
分区:对业务透明,分区只不过把存放数据的文件分成了许多小块,例如mysql中的一张表对应三个文件.MYD,MYI,frm. 根据一定的规则把数据文件(MYD)和索引文件(MYI)进行了分割,分区后的表 ...
- .gitignore文件的写法
有些时候,你必须把某些文件放到Git工作目录中,但又不能提交它们,比如保存了数据库密码的配置文件啦,等等,每次git status都会显示Untracked files .... 解决的方法就是在gi ...
- Cron 定时任务表达式
Cron Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth M ...
- 服务发现之consul理论整理_结合Docker+nginx+Tomcat简单部署案例
目录 一.理论概述 服务发现的概念简述 consul简述 二.部署docker+consul+Nginx案例 环境 部署 三.测试 四.总结 一.理论概述 服务发现的概念简述 在以前使用的是,N台机器 ...