CodeForces 164A Variable, or There and Back Again 搜索
Variable, or There and Back Again
题目连接:
http://codeforces.com/problemset/problem/164/A
Description
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, ..., pk such, 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), fi 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 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.
Sample Input
4 3
1 0 0 2
1 2
2 3
3 4
Sample Output
1
1
1
1
Hint
题意
给你n和m,表示有n个状态和m条单向边
快乐路径表示从1开始,2结束的路径,这个路径中间没有1就可以
问你这些状态哪些是快乐路径上的,哪些不是
题解:
两次bfs/dfs就好呢
第一次从1开始搜,第2次2开始搜
如果这个点在两次搜索的时候,都经过的话,就是快乐路径上的
否则就不是
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+6;
vector<int> E1[maxn];
vector<int> E2[maxn];
int vis1[maxn];
int vis2[maxn];
int a[maxn];
queue<int> Q;
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
E1[x].push_back(y);
E2[y].push_back(x);
}
for(int i=1;i<=n;i++)
if(a[i]==1)
{
vis1[i]=1;
Q.push(i);
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
for(int i=0;i<E1[now].size();i++)
{
int x = E1[now][i];
if(a[x]==1)continue;
if(vis1[x])continue;
vis1[x]=1;
Q.push(x);
}
}
while(!Q.empty())Q.pop();
for(int i=1;i<=n;i++)
if(a[i]==2)
{
vis2[i]=1;
Q.push(i);
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
for(int i=0;i<E2[now].size();i++)
{
int x = E2[now][i];
if(a[x]==1)
vis2[x]=1;
if(vis2[x])continue;
vis2[x]=1;
Q.push(x);
}
}
for(int i=1;i<=n;i++)
if(vis1[i]&&vis2[i])
printf("1\n");
else
printf("0\n");
}
CodeForces 164A Variable, or There and Back Again 搜索的更多相关文章
- Codeforces Gym 101142C:CodeCoder vs TopForces(搜索)
http://codeforces.com/gym/101142/attachments 题意:每个人在TC和CF上分别有两个排名,如果有一个人在任意一个网站上大于另一个人的排名,那么这个人可以打败另 ...
- Educational Codeforces Round 1 E. Chocolate Bar 记忆化搜索
E. Chocolate Bar Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/598/prob ...
- Codeforces Round #359 (Div. 2) C. Robbers' watch 搜索
题目链接:http://codeforces.com/contest/686/problem/C题目大意:给你两个十进制的数n和m,选一个范围在[0,n)的整数a,选一个范围在[0,m)的整数b,要求 ...
- codeforces 284 D. Cow Program(记忆化搜索)
题目链接:http://codeforces.com/contest/284/problem/D 题意:给出n个数,奇数次操作x,y都加上a[x],偶数次操作y加上a[x],x减去a[x],走出了范围 ...
- Codeforces 294B Shaass and Bookshelf(记忆化搜索)
题目 记忆化搜索(深搜+记录状态) 感谢JLGG //记忆话搜索 //一本书2中状态,竖着放或者横着放 //初始先都竖着放,然后从左边往右边扫 #include<stdio.h> #inc ...
- Codeforces 540D Bad Luck Island - 概率+记忆化搜索
[题意] 一个岛上有三种生物A,B,C,各有多少只在输入中会告诉你,每种最多100只 A与B碰面,A会吃掉B, B与C碰面,B会吃掉C, C与A碰面,C会吃掉A...忍不住想吐槽这种环形食物链 碰面是 ...
- codeforces E - Anya and Cubes 分块处理 暴力搜索
说的是给了n个立方体,立方体从1标号到n,每个立方体上有一个数字, 你有 k 个机会 使得其中 k个数位他们自己的阶乘,(自然使用可以少于k次机会,每个立方体最多被使用1次) ,那么求出你从这n个立方 ...
- Codeforces Round #256 (Div. 2) C. Painting Fence (搜索 or DP)
[题目链接]:click here~~ [题目大意]:题意:你面前有宽度为1,高度给定的连续木板,每次能够刷一横排或一竖列,问你至少须要刷几次. Sample Input Input 5 2 2 1 ...
- Codeforces 354B 博弈, DP,记忆化搜索
题意:现在有一个字符矩阵,从左上角出发,每个人交替选择一个字符.如果最后字符a数目大于字符b,那么第一个人获胜,否则b获胜,否则平均.现在双方都不放水,问最后结果是什么? 思路:这题需要注意,选择的字 ...
随机推荐
- go 应用程序性能测试
runtime/pprof 我们要加入对pprof包里的方法调用,程序才能将运行时候程序的堆内存分配状态记录到文件(也可以是写到其他地方,例如网络等)中,以便进一步的分析. 如果你的go程序只是一个应 ...
- LR之脚本调试
1.概述 2.Animated run和Non-animated run 3.调试小技巧 4.日志设置
- 笔记《Java程序性能优化 让你的Java程序更快、更稳定》 第二章 设计调优
2.1 善用设计模式 23 (1) 1. 设计模式好处: 2.1.1 单例模式 23 (6) 1. 单例模式是一种对象创建模式,用于产生一个对象的具体实例,它可以确保系统中一个类只产生一个实例: 2. ...
- 使用LabVIEW如何生成应用程序(exe)和安装程序(installer)
主要软件: LabVIEW Development Systems>>LabVIEW Professional Development System主要软件版本: 2012主要软件 ...
- grails中报Cannot create a session after the response has been committed异常的解决办法
在grails中,我们在layouts\main.gsp中使用类似如下的代码来判断当前用户处于登录状态时显示相关的登录信息: <g:if test="${session.users}& ...
- 【加解密】关于DES加密算法的JAVA加密代码及C#解密代码
JAVA加密: package webdomain; import java.security.Key; import java.security.spec.AlgorithmParameterSpe ...
- Raspberry Pi上手
2013-05-21 买的树莓派终于到手了,嘿嘿.我在官方代理ICKEY买的,是英国版,B型. 上手教程可以根据Getting Started with Raspberry Pi(网上有电子版免费下载 ...
- 手势冲突UIPanGestureRecognizer 和UIPinchGestureRecognizer
当同时使用pan和pin手势时假如冲突,需要加入下面方法 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shou ...
- js_sl 延迟菜单
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- F2063 Could not compile used unit 'tt.pas'
install packge error F2063 Could not compile used unit 'tt.pas' 有可能是工程的pas文件相对路径不对.在工程管理看是否能打开文件,如果打 ...