A8387 | Variable, or There and Back Again
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $v_{1},v_{2},...,v_{x}$ , where for any $1<=i<x$ exists a transition from $v_{i}$ to $v_{i+1}$ .
Vasya's value in state $v$ is interesting to the world, if exists path $p_{1},p_{2},...,p_{k}$ such, that $p_{i}=v$ for some $i$ $(1<=i<=k)$ , in state $p_{1}$ Vasya gets assigned a value, in state $p_{k}$ Vasya is used and there is no state $p_{i}$ (except for $p_{1}$ ) where Vasya gets assigned a value.
Help Vasya, find the states in which Vasya's value is interesting to the world.
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 $v_{1},v_{2},...,v_{x}$ , where for any $1<=i<x$ exists a transition from $v_{i}$ to $v_{i+1}$ .
Vasya's value in state $v$ is interesting to the world, if exists path $p_{1},p_{2},...,p_{k}$ such, that $p_{i}=v$ for some $i$ $(1<=i<=k)$ , in state $p_{1}$ Vasya gets assigned a value, in state $p_{k}$ Vasya is used and there is no state $p_{i}$ (except for $p_{1}$ ) where Vasya gets assigned a value.
Help Vasya, find the states in which Vasya's value is interesting to the world.
输入格式
The first line contains two space-separated integers $n$ and $m$ ( $1<=n,m<=10^{5}$ ) — the numbers of states and transitions, correspondingly.
The second line contains space-separated $n$ integers $f_{1},f_{2},...,f_{n}$ ( $0<=f_{i}<=2$ ), $f_{i}$ described 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 $a_{i},b_{i}$ ( $1<=a_{i},b_{i}<=n$ , $a_{i}≠b_{i}$ ), each pair represents the transition from the state number $a_{i}$ to the state number $b_{i}$ . Between two states can be any number of transitions.
The second line contains space-separated $n$ integers $f_{1},f_{2},...,f_{n}$ ( $0<=f_{i}<=2$ ), $f_{i}$ described 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 $a_{i},b_{i}$ ( $1<=a_{i},b_{i}<=n$ , $a_{i}≠b_{i}$ ), each pair represents the transition from the state number $a_{i}$ to the state number $b_{i}$ . Between two states can be any number of transitions.
输出格式
Print $n$ integers $r_{1},r_{2},...,r_{n}$ , separated by spaces or new lines. Number $r_{i}$ 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.
输入输出样例
输入 #1
4 3 1 0 0 2 1 2 2 3 3 4
输出 #1
1 1 1 1
输入 #2
3 1 1 0 2 1 3
输出 #2
1 0 1
输入 #3
3 1 2 0 1 1 3
输出 #3
0 0 0
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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted