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的更多相关文章
随机推荐
- Redis—.Net中的使用
StackExchange.Redis使用以及封装 来源:http://www.cnblogs.com/qtqq/p/5951201.html,https://www.cnblogs.com/xsj1 ...
- Gogs + Drone 实现CI/CD(CD)
前文已经实现CI部分,本文继续以Asp.Net Core实现CD部分. 创建gogs仓库 首先在gogs创建一个空项目drone-ci-demo,本地新建一个asp.net core项目,并且在与.c ...
- C++开源库大全
标准库 C++ Standard Library:是一系列类和函数的集合,使用核心语言编写,也是C++ISO自身标准的一部分. Standard Template Library:标准模板库 ...
- POJ3368(Frequent values)--线段树
题目在这里 3368 Accepted 7312K 1829MS C++ 6936B 题意为给你一组数据,再给定一组区间,问你这个区间内出现次数最多的元素的次数是多少. 我还记得这题是学校校赛基础的题 ...
- iOS自动布局学习(UIView+AutoLayout)
自动布局虽然在iOS6的时候已经推出,不过由于各个原因并没有被开发组广泛使用.一方面是大家的app支持版本都是低于iOS6的,另一方面来说是Xcode支持木有现在这么好.以前由于iPhone设备相对固 ...
- shell 三剑客之 sed pattern 详解
sed 基础介绍 语法格式 sed 处理过程 sed 选项 cat sed.txt '-p' 打印输出 ,默认输出两次,流输出一次,源文件输出一次 sed 'p' sed.txt -n 只显示处理的 ...
- Linux误删python导致yum不可用,删除重装方法。
Linux 系统为 CentOS Linux release 7.4.1708 手贱.手贱.手贱 删了python 导致yum不可用.百度一大圈,重装yum和python后,老是报各种各样的错.历经磨 ...
- 备份一下我的88bugs的application文档
# 服务端口 server.port=8083 #spring.mvc.favicon.enabled=false server.servlet.context-path=/bug/ dateform ...
- Notepad++ 连接远程 NppFTP
远程连接 1.自动安装: 插件——> Plugin Manager——>ShowPlugin Manager——>勾选NppFTP插件——>Install 2. 手动安装 ...
- linux网络编程之socket编程(十五)
今天继续学习socket编程,这次主要是学习UNIX域协议相关的知识,下面开始: [有个大概的认识,它是来干嘛的] ①.UNIX域套接字与TCP套接字相比较,在同一台主机的传输速度前者是后者的两倍. ...