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 ≤ nai ≠ 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

Input
4 3
1 0 0 2
1 2
2 3
3 4
Output
1
1
1
1
Input
3 1
1 0 2
1 3
Output
1
0
1
Input
3 1
2 0 1
1 3
Output
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的更多相关文章

随机推荐

  1. 第二次用map23333

    度熊所居住的 D 国,是一个完全尊重人权的国度.以至于这个国家的所有人命名自己的名字都非常奇怪.一个人的名字由若干个字符组成,同样的,这些字符的全排列的结果中的每一个字符串,也都是这个人的名字.例如, ...

  2. 如何使用Class和ClassLoader加载文件

    很多时候我们都需要在程序中加载各种文件,比如在加载配置文件,加载properties文件,或者只是加载一个文本文件,然后输出其中的内容,我在初学java的时候,就对加载文件非常头疼,今天又遇见了加载文 ...

  3. GRPC代替webapi Demo。

    gRPC 是一种与语言无关的高性能远程过程调用 (RPC) 框架. gRPC 的主要优点是: 现代高性能轻量级 RPC 框架. 协定优先 API 开发,默认使用协议缓冲区,允许与语言无关的实现. 可用 ...

  4. 2602978 - [How to] Content Synchronization between SLDs

    http://47.101.174.212:52000/sld http://47.101.176.136:56000/sld Symptom As described in Planning Gui ...

  5. Linux Centos7 网络设置UUID号的修改方法

    1.生成一个UUID [root@localhost ~]# uuidgen ens33 223bdb47-2fed-4773-b984-5f5733e61904 2.UUID号写入网络配置文件ifc ...

  6. Python使用xlrd、pandas包从Excel读取数据

    #coding=utf-8 # pip install xlrd import xlrd def read_from_xls(filepath,index_col_list): #filepath:读 ...

  7. javascript数据判断是否有指定元素

    jquery方法: $.inArray(dataKey, dataArray) == -1 javascript方法: indexOf() 功能:根据指定的数据,从左向右,查询在数组中出现的位置,如果 ...

  8. Codeforces 348 D - Turtles Lindström–Gessel–Viennot lemma

    #include<bits/stdc++.h> using namespace std; #define y1 y11 #define fi first #define se second ...

  9. orm多表的创建和基于对象的查询

    创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是一对一的关系( ...

  10. linux下环境管理anaconda3

    我之前在centos之安装单独python3.6,大家都知道centos自带python2.7,通过输入python,和python3来控制想要使用python2,或者python3,如今想要要在li ...