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的更多相关文章
随机推荐
- php json_encode()函数返回对象和数组问题
php json_encode() 函数格式化数据时会根据不同的数组类型格式化不同类型的json数据 索引数组时 <?php $arr = [1,2,3,4,5]; print_r(json_e ...
- 【转载】Asp.Net中Cookie对象的作用以及常见属性
Cookie对象是服务器为用户访问存储的特定信息,这些信息一般存储在浏览器中,服务器可以从提交的数据中获取到相应的Cookie信息,Cookie的最大用途在于服务器对用户身份的确认,即票据认证,用户会 ...
- vue组件中的data与methods
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> ...
- 流程控制 while for
循环执行 计算机最擅长的功能之一就是按照规定的条件,重复执行某些操作,这是程序设计中最能发挥计算机特长的程序结构. 1.while语句 while(表达式){ 各种语句.... } 当表达式的值为tr ...
- 爬虫之 BeautifulSoup与Xpath
知识预览 BeautifulSoup xpath BeautifulSoup 一 简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: '' ...
- C++——友元 friend
人类社会的friend VS C++世界的friend 现实世界中,我们自己很多物品,朋友是可以使用的,但是陌生人就不行.那么money,朋友可以随便拿吗?这要是你和friend的关系深浅而定.人类社 ...
- 【OF框架】定义框架标准WebApi,按照规范返回状态信息及数据信息
准备 了解框架基本应用,已经完成Controller创建. 一.定义框架标准WebApi 一个标准的WebApi,包含预定义的入参和回参类型 入参为CallParams,需要增加FromBody声明, ...
- 数据库中的Schema到底是什么
参考:http://database.guide/what-is-a-database-schema/ 在数据库中,schema(发音 “skee-muh” 或者“skee-mah”,中文叫模式)是数 ...
- 别人家的java语言编写的自动化测试系统
https://gitee.com/testdevops/easyrest 但是我还不会用 ~~~~(>_<)~~~~ 目前的状况是:大概知道流程了,可是第一个用例就跑步起来 自己想写一些 ...
- ArrayList之foreach循环删除倒数第二个元素,不触发fail-fast机制
今天一朋友问了个问题,对于如下一段代码,运行后会有怎样的结果? public class ArrayListTest { public static void main(String[] args) ...