CF164A.Variable, or There and Back Again
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
对一个名叫瓦西娅的再普通不过的变量而言,生活并不轻松。无论它走到哪里,总会被赋值、被忽略,或者被使用!
瓦西娅的生活发生在程序的状态中。在每个状态里,瓦西娅要么被使用(例如,用于计算另一个变量的值),要么被赋值,要么被忽略。某些状态之间存在有向(定向)转移。
一条路径是指一个状态序列 v1, v2, …, vx,使得对任意 1≤i<x,均存在从 vi 到 vi+1 的转移。
若存在一条路径 p1, p2, …, pk,满足以下条件,则称瓦西娅在状态 v 中的值“对世界而言是有趣的”:
- 存在某个 i(1≤i≤k),使得 pi=v;
- 在状态 p1 中,瓦西娅被赋值;
- 在状态 pk 中,瓦西娅被使用;
- 除 p1 外,路径中不存在任何其他状态 pi 使瓦西娅被赋值。
请帮助瓦西娅,找出所有瓦西娅的值“对世界而言是有趣的”的状态。
输入格式
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 _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.
第一行包含两个用空格分隔的整数 n 和 m(1≤n,m≤105),分别表示状态数和转移数。
第二行包含 n 个用空格分隔的整数 f1,f2,…,fn(0≤fi≤2),其中 fi 描述了在状态 i 下对 Vasya 执行的操作:0 表示忽略,1 表示赋值,2 表示使用。
接下来的 m 行每行包含一对用空格分隔的整数 ai,bi(1≤ai,bi≤n,且 ai=bi),每对表示从状态 ai 到状态 bi 的一条转移。任意两个状态之间可以存在任意数量的转移。
输出格式
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.
输出 n 个整数 _r_₁, _r_₂, ..., r__n,用空格或换行符分隔。若第 i 个状态中 Vasya 的值对世界而言是“有趣的”,则 r__i 应为 1;否则为 0。状态按输入中描述的顺序编号,编号从 1 到 n。
输入输出样例
输入#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.
在第一个样例中,程序状态可以构成唯一一条路径,该路径中瓦夏(Vasya)的值对世界而言是有趣的:1
2
3
4;该路径包含了所有状态,因此在所有这些状态中,瓦夏的值对世界而言都是有趣的。
在第二个样例中,瓦夏的值对世界而言是有趣的唯一路径为:1
3;状态 2 未包含于该路径中。
在第三个样例中,我们无法利用这些状态构造出任何一条路径,使得瓦夏的值对世界而言是有趣的,因此瓦夏的值永远不会对世界而言是有趣的。
输入解题思路,AI测评打分。不知道怎么写?