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的更多相关文章
随机推荐
- 使用代码浏览WPF控件模版
纯代码创建,不需要创建界面,创建WPF工程后,直接复制代码就可以使用. 当你手头没有Blend,又不记得以下这段代码,但是又想浏览控件模版的时候,就可以直接复制拿来用了. public partial ...
- ESlint 格式化代码 备忘
vscode 代码格式化配置 vscode 菜单 文件->首选项->设置 --->进入扩展查找到ESlint,点击任一选项中的[在setting.json中配置],复制以下代码 { ...
- 1+x证书学习日志——css 基本选择符
##css选择符 1:类型选择符 直接用标签名称当作选择符 特点:选中所有同类元素 2:id名称 ...
- 前端相关UED团队和个人博客整理
平时收集的UED的团队和个人博客一些有关/*********************************这次真的是搬运工,原文转载自蓝色理想********************/ 前端团队推荐 ...
- java - day019 - 数据库
https://www.cnblogs.com/myxq666/p/7787744.html Mac 安装MySQL步骤 什么是数据库 数据库: 英文名称Database ,简称 DB 数据库是按照数 ...
- C++——流类库和输入/输出
前言 数据是怎么写道磁盘的? 代码里面我们一个读写调用似乎就能将数据从磁盘读写.仿佛代码是直接和磁盘打交道.以我们最常见的笔记本.或台式机的磁盘SATA盘为例,其受南桥上ATA控制器支配,这里面传输的 ...
- C++——Big Three(copy ctor、copy op=、dtor)
Big Three C++ 中Big Three指的是copy ctor 和 copy op= 和 dtor m_data是个字符串指针.一般而言,处理字符串,都是使用指针,在需要存储字符的时候再 ...
- SUSE Ceph Cephfs - Storage6
(1)Policy 配置文件,添加MDS角色定义 # vim /srv/pillar/ceph/proposals/policy.cfg # MDS role-mds/cluster/mds*.sls ...
- 更改Ubuntu下默认Python版本
更改Ubuntu下默认Python版本 首先查看系统内有哪些版本的Python ls /usr/bin/python* 查看当前python版本 python --version 基于用户修改默认版本 ...
- 无法访问此网站 ERR_CONTENT_DECODING_FAILED
这个错误挺少见的. 百度了下: 1,说是文件编码格式不正确: 2,说是Nginx的压缩和tomcat的压缩冲突了,关闭gzip压缩: 3,……………… 不管是哪一种情况,都是编码问题,所以逐一排查,根 ...