之前学长让我们练习最短路径时我摸鱼了,刚刚补了spf那部分知识点,训练就遇到类似的题,但是非常不好想,以后不摸鱼了。
You are given an array a consisting of n integers. In one move, you can jump from the position i to the position i−ai (if 1≤i−ai) or to the position i+ai (if i+ai≤n).
For each position i from 1 to n you want to know the minimum the number of moves required to reach any position j such that aj has the opposite parity from ai (i.e. if ai is odd then aj has to be even and vice versa).
Input
The first line of the input contains one integer n (1≤n≤2⋅105) — the number of elements in a.
The second line of the input contains n integers a1,a2,…,an (1≤ai≤n), where ai is the i-th element of a.
Output
Print n integers d1,d2,…,dn, where di is the minimum the number of moves required to reach any position j such that aj has the opposite parity from ai (i.e. if ai is odd then aj has to be even and vice versa) or -1 if it is impossible to reach such a position.
#include<iostream>
#include<cstring>
#include<queue>
#include<deque>
#include<vector>
using namespace std;
const int maxn=2e6+5;
int a[maxn],dis[maxn],vis[maxn],ans[maxn];
vector<int>son[maxn];
void spf(int u)
{
deque<int>in;
memset(dis,0x3f3f3f3f,sizeof(dis));