This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
/*typedef __uint128_t L;
struct FastMod {
ull b, m;
FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
ull reduce(ull a) {
ull q = (ull)((L(m) * a) >> 64);
ull r = a - q * b; // can be proven that 0 <= r < 2*b
return r >= b ? r - b : r;
}
};
FastMod FM(2);*/
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << H;
debug_out(T...);
}
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
#define F first
#define S second
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 3e4 + 10;
int n, m, a[maxn], b[maxn];
map<pii,bool> mark;
map<pii,int> h;
vector<int> val[maxn];
void bfs(pii st){
h[st] = 0;
mark[st] = true;
deque<pii> q;
q.push_back(st);
while(!q.empty()){
int v = q.front().F, k = q.front().S;
q.pop_front();
for (auto x: val[v]){
if (!mark[{v, x}] || h[{v, x}] > h[{v, k}]){
h[{v, x}] = h[{v, k}];
mark[{v, x}] = true;
q.push_front({v, x});
}
}
val[v].clear();
if (v - k >= 0 && !mark[{v-k, k}]){
h[{v-k, k}] = h[{v, k}] + 1;
mark[{v-k, k}] = true;
q.push_back({v-k, k});
}
if (v + k < n && !mark[{v+k, k}]){
h[{v+k, k}] = h[{v, k}] + 1;
mark[{v+k, k}] = true;
q.push_back({v+k, k});
}
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++){
cin >> a[i] >> b[i];
val[a[i]].push_back(b[i]);
}
bfs({a[1], b[1]});
cout << (!mark[{a[2], b[2]}]? -1: h[{a[2], b[2]}]) << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |