题目链接:https://atcoder.jp/contests/abc132/tasks/abc132_e

题目大意

  给定一张 N 个点 M 条边无自环无重边的一张有向图,求从起点 S 能否三步三步跳到终点 T,输出跳的次数。

分析

  是一个步长为 3 的 BFS。

代码如下

 #include <bits/stdc++.h>
using namespace std; #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
#define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
#define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
#define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper); #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} template<class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
} inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
} //min <= aim <= max
template<typename T>
inline bool BETWEEN(const T aim, const T min, const T max) {
return min <= aim && aim <= max;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef pair< int, int > PII;
typedef pair< int, PII > PIPII;
typedef pair< string, int > PSI;
typedef pair< int, PSI > PIPSI;
typedef set< int > SI;
typedef set< PII > SPII;
typedef vector< int > VI;
typedef vector< double > VD;
typedef vector< VI > VVI;
typedef vector< SI > VSI;
typedef vector< PII > VPII;
typedef map< int, int > MII;
typedef map< int, string > MIS;
typedef map< int, PII > MIPII;
typedef map< PII, int > MPIII;
typedef map< string, int > MSI;
typedef map< string, string > MSS;
typedef map< PII, string > MPIIS;
typedef map< PII, PII > MPIIPII;
typedef multimap< int, int > MMII;
typedef multimap< string, int > MMSI;
//typedef unordered_map< int, int > uMII;
typedef pair< LL, LL > PLL;
typedef vector< LL > VL;
typedef vector< VL > VVL;
typedef priority_queue< int > PQIMax;
typedef priority_queue< int, VI, greater< int > > PQIMin;
const double EPS = 1e-;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ;
const LL evenBits = 0xaaaaaaaaaaaaaaaa;
const LL oddBits = 0x5555555555555555; int N, M, S, T, ans = -;
VI v[maxN];
int vis[maxN][]; void bfs() {
int cnt = , times = ;
queue< int > Q;
Q.push(S);
vis[S][] = ; while(!Q.empty()) {
int tmp = Q.front(); Q.pop();
--cnt; foreach(i, v[tmp]) {
if(vis[*i][]) continue; // 如果从 *i 向下走两步已经执行过,就跳过
vis[*i][] = ; foreach(j, v[*i]) {
if(vis[*j][]) continue; // 如果从 *j 向下走一步已经执行过,就跳过
vis[*j][] = ; foreach(k, v[*j]) {
if(vis[*k][]) continue; // 如果从 *k 向下走零步已经执行过,就跳过
vis[*k][] = ; if(*k == T) {
ans = times + ;
return;
}
Q.push(*k);
}
}
} if(cnt == ) {
cnt = Q.size();
++times;
}
}
} int main(){
//freopen("MyOutput.txt","w",stdout);
//freopen("input.txt","r",stdin);
INIT();
cin >> N >> M;
Rep(i, M) {
int x, y;
cin >> x >> y;
v[x].PB(y);
}
cin >> S >> T;
bfs();
cout << ans << endl;
return ;
}

最新文章

  1. 为什么operator&gt;&gt;(istream&amp;, string&amp;)能够安全地读入长度未知的字符串?
  2. 用DirectX实现魔方(二)
  3. 2016年10月12日 星期三 --出埃及记 Exodus 18:23
  4. Lucene教程--转载
  5. Buffer Sort
  6. I have a dream
  7. Android状态栏颜色修改
  8. boost vc编译
  9. 转:jQuery事件绑定.on()简要概述及应用
  10. Android 文件共享服务器
  11. 一天搞定HTML----列表标签03
  12. 谈谈JAVA中的安全发布
  13. [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)
  14. JS获取屏幕,浏览器窗口大小,网页高度宽度(实现代码)_javascript技巧_
  15. udt通信java
  16. tp框架中的一些疑点知识-5
  17. C# 利用反射将枚举绑定到下拉框
  18. Elasticsearch究竟要设置多少分片数?
  19. Ubuntu 源码方式安装Subversion
  20. uboot启动内核的实现

热门文章

  1. 牛客 某练习赛 Data Structure
  2. stl+数论——1247D
  3. js-原生js触发器使用
  4. [SDOI2015]排序 题解 (搜索)
  5. 牛客Another Distinct Values
  6. 使用wordpress搭建的网站如何去掉域名中的wordpess
  7. postgresql修改自增序列
  8. Vim用法AAAAA
  9. db2,用户名密码不对导致无法连接数据库: Reason: User ID or Password invalid. ERRORCODE=-4214, SQLSTATE=28000
  10. RocktMq安装和简单使用以及报错收集