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 ...
随机推荐
- oracle/node-oracledb 数据库驱动 与 Meteor 驱动包!
oracle/node-oracledb: https://github.com/oracle/node-oracledb Oracle 官方维护. metstrike/meteor-oracle ...
- Junit测试打印详细的log日志,可以看到sql
Junit测试打印详细的log日志,可以看到sql 在log4j.xml的日志配置文件中,把日志级别从info级别调整到debug级别: <?xml version="1.0" ...
- unity3d项目文件目录发布后,对应的ios/android应用目录[转]
Unity3d的Resource.AssetBundle与手游动态更新的报告,在这里分享一下,希望能够对各位用Unity的朋友有些许帮助.目录:1.Unity的资源数据加载2.Resource.Str ...
- ABAP 单位转换函数
CALL FUNCTION 'UNIT_CONVERSION_SIMPLE' EXPORTING input = wa_all-btg ...
- nyoj1000_快速幂_费马小定理
又见斐波那契数列 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 斐波那契数列大家应该很熟悉了吧.下面给大家引入一种新的斐波那契数列:M斐波那契数列. M斐波那契数列 ...
- IE的if条件Hack(兼容性)
1. 〈!--[if !IE]〉〈!--〉 除IE外都可识别 〈!--〈![endif]--〉 2. 〈!--[if IE]〉 所有的IE可识别〈![endif]--〉 3. 〈!--[if IE 5 ...
- MPAndroidChart 教程
以前没用过MPAndroidChart,为了方便学习查找,引用下别个大神的笔记. 其余文章索引: MPAndroidChart 教程:概述MPAndroidChart 教程:开始 Getting St ...
- Android Canvas绘图详解(图文)
编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! Andr ...
- 22中编程语言的HelloWorld
C:printf("HelloWorld"); C++ : cout<<"HelloWorld"; QBasic : Print "Hel ...
- [Android Pro] APK
svn updatesvn status ls -alsvn log --limit 8 > RELEASE_NOTE.txt cat RELEASE_NOTE.txt chmod a+x gr ...