CodeForces 164A Variable, or There and Back Again(搜索)
2016-04-13 16:11
573 查看
题意:
给你n和m,表示有n个状态和m条单向边
快乐路径表示从1开始,2结束的路径,这个路径中间没有1就可以
问你这些状态哪些是快乐路径上的,哪些不是
思路:两次BFS
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 p1Vasya 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: 0represents 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
Input
Output
Input
Output
Input
Output
Hint
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
![](http://7xjob4.com1.z0.glb.clouddn.com/7dba3e435f3fa0126d9a286022c47850)
2
![](http://7xjob4.com1.z0.glb.clouddn.com/7dba3e435f3fa0126d9a286022c47850)
3
![](http://7xjob4.com1.z0.glb.clouddn.com/7dba3e435f3fa0126d9a286022c47850)
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
![](http://7xjob4.com1.z0.glb.clouddn.com/7dba3e435f3fa0126d9a286022c47850)
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.
给你n和m,表示有n个状态和m条单向边
快乐路径表示从1开始,2结束的路径,这个路径中间没有1就可以
问你这些状态哪些是快乐路径上的,哪些不是
思路:两次BFS
#include<bits/stdc++.h> using namespace std; const int maxn = 1e6+10; 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 u,v; scanf("%d%d",&u,&v); e1[u].push_back(v); e2[v].push_back(u); } 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 v = e1[now][i]; if (a[v]==1) continue; if (vis1[v]) continue; vis1[v]=1; q.push(v); } } 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 v = e2[now][i]; if (a[v]==1) vis2[v]=1;; if (vis2[v]) continue; vis2[v]=1; q.push(v); } } for (int i = 1;i<=n;i++) if (vis1[i]&&vis2[i]) printf("1\n"); else printf("0\n"); }
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 p1Vasya 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: 0represents 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
Input
4 3 1 0 0 2 1 2 2 3 3 4
Output
1 1 1 1
Input
3 1 1 0 2 1 3
Output
1 0 1
Input
3 1 2 0 1 1 3
Output
0 0 0
Hint
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.
相关文章推荐
- 算法杂货铺——分类算法之朴素贝叶斯分类(Naive Bayesian classification)
- LeetCode Algorithms #172 <Factorial Trailing Zeroes>
- 【原创】k8s源码分析-----kubelet(7)containerRuntime
- IPC(五)——浅谈AIDL的架构原理
- LightOJ 1138 Trailing Zeroes (III) 打表
- CDISC SDTM SE domain 学习笔记
- CPaintDC、CClientDC、CWindowDC和CMetaFileDC类的主要特点及区别
- leetcode-11-Container With Most Water
- wait和waitpid详解
- 重启ambari-agent服务器报错: Failed to start ping port listener of
- U盘安装Ubuntu——关于UltraISO打开Ubuntu只有EFI文件夹的解决方法
- No Data There was no data available.Possible reasons include inaccessible
- ambari安装报错Registration with the server failed.
- ganglia no data there was no data available possible reasons
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATI
- 关于At least one JAR was scanned for TLDs yet contained no TLDs.问题的解决
- updateViewConstraints和updateConstraints
- Container exited with a non-zero exit code 127
- 人工智能的妙用:谷歌公布图像字幕技术
- bzoj 4320 ShangHai2006 Homework