Codeforces VK Cup 2012 Round 3 A. Variable, or There and Back Again(dfs)
题目链接:http://codeforces.com/problemset/problem/164/A
思路:用vector分别保留原图和发图,然后分别从val值为1的点正向遍历,va值为2的点反向遍历,如果某个点这两种方式都可以遍历到,则输出1,否则输出0.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std; const int MAX_N = (100000 + 100);
int N, M, val[MAX_N], vis1[MAX_N], vis2[MAX_N];
int path[MAX_N];
vector<int > g1[MAX_N], g2[MAX_N]; void dfs(int u, int fa)
{
vis1[u] = 1;
REP(i, 0, (int)g1[u].size()) {
int v = g1[u][i];
if (!vis1[v] && v != fa && val[v] != 1) dfs(v, u);
}
} void rdfs(int u, int fa)
{
vis2[u] = 1;
if (val[u] == 1) return;
REP(i, 0, (int)g2[u].size()) {
int v = g2[u][i];
if (!vis2[v] && v != fa) rdfs(v, u);
}
} int main()
{
cin >> N >> M;
FOR(i, 1, N) cin >> val[i];
FOR(i, 1, M) {
int u, v; cin >> u >> v;
g1[u].push_back(v);
g2[v].push_back(u);
}
FOR(i, 1, N) {
if (val[i] == 1) dfs(i, -1);
else if (val[i] == 2) rdfs(i, -1);
}
FOR(i, 1, N) printf("%d\n", (vis1[i] & vis2[i]));
return 0;
}
Codeforces VK Cup 2012 Round 3 A. Variable, or There and Back Again(dfs)的更多相关文章
- VK Cup 2012 Round 3 (Unofficial Div. 2 Edition)
VK Cup 2012 Round 3 (Unofficial Div. 2 Edition) 代码 VK Cup 2012 Round 3 (Unofficial Div. 2 Edition) A ...
- Codeforces 161.D. Distance in Tree-树分治(点分治,不容斥版)-树上距离为K的点对数量-蜜汁TLE (VK Cup 2012 Round 1)
D. Distance in Tree time limit per test 3 seconds memory limit per test 512 megabytes input standard ...
- VK Cup 2012 Round 1 D. Distance in Tree (树形dp)
题目:http://codeforces.com/problemset/problem/161/D 题意:给你一棵树,问你两点之间的距离正好等于k的有多少个 思路:这个题目的内存限制首先大一倍,他有5 ...
- 【树形dp】VK Cup 2012 Round 1 D. Distance in Tree
统计树中长度为K的路径条数. 用f[u][k]表示从u结点的子树中出发,终止于u结点的长度为k的路径条数. 边dp边统计答案.为了防止重复统计,在枚举子节点的时候,先将该子节点和当前u结点(和前面已经 ...
- DP VK Cup 2012 Qualification Round D. Palindrome pairs
题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...
- Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!
VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM Time Lim ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!
Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 全场题解 菜鸡只会A+B+C,呈上题解: A. Bear and ...
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 1 Edition) C. Little Artem and Random Variable 数学
C. Little Artem and Random Variable 题目连接: http://www.codeforces.com/contest/668/problem/C Descriptio ...
- Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3)(A.B.C,3道暴力题,C可二分求解)
A. Is it rated? time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...
随机推荐
- 【leetcode】Wildcard Matching
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- poj 1276
一道DP的题目,还是一道多重背包的题目,第一次接触. 题意:有现今cash,和n种钱币,每种钱币有ni个,价值为di,求各种钱币组成的不超过cash的最大钱数 思路:可以转换为0/1背包和完全背包来做 ...
- Delphi中限制文本框(TEdit)只能输入数字
procedure Tform1.Edit1KeyPress(Sender: TObject; var Key: Char);var edt: TEdit; str, strL, strR: stri ...
- ACM/ICPC 之 BFS-广搜+队列入门-抓牛(POJ3278)
这一题是练习广度优先搜索很好的例题,在很多广搜教学中经常用到,放在这里供学习搜索算法的孩纸们看看= = 题目大意:一维数轴上,农夫在N点,牛在K点,假定牛不会移动,农夫要找到这头牛只能够进行以下三种移 ...
- Python2.7<-------->Python3.x
版本差异 from __future__ Python2.7 Python3.x 除法 / // Unicode u'' ...
- [Error] Error parsing XML: unbound prefix
发生该错误的代码: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- code vs1517 求一次函数解析式(数论 纯数学知识)
1517 求一次函数解析式 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 查看运行结果 题目描述 Description 相信大家都做过练 ...
- Spring面向切面编程(AOP)方式二
使用注解进行实现:减少xml文件的配置. 1 建立切面类 不需要实现任何特定接口,按照需要自己定义通知. package org.guangsoft.utils; import java.util.D ...
- eclipse快捷键Alt + / 失效
最近电脑上的Eclipse没有了自动提示功能,也不是全部不提示,大多数情况下按下"alt+/"键还会产生提示,但是当我在java项目中邪main方法和syso的时候,"a ...
- [Android Pro] Android studio jni中调用Log输出调试信息
reference to : http://www.linuxidc.com/Linux/2014-02/96341.htm Android 开发中,java 可以方便的使用调试信息Log.i, Lo ...