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>
#pragma GCC optimize("Ofast")
using namespace std;
bitset<30001> bs,bs2[30001];
vector<int> v[30001];
int n,m,tag;
int main(){
ios_base::sync_with_stdio(false);
cout.tie(NULL);
cin.tie(NULL);
cin >> n >> m;
for(int i = 0 ; i < m ; i += 1){
int a,b;
cin >> a >> b;
if(i == 1){
tag = a;
}
v[a].push_back(b);
}
queue<pair<int,int>> q;
for(auto i : v[0]){
q.push({0,i});
bs2[0][i] = 1;
}
bs[0] = 1;
int dist = 0;
while(q.size()){
queue<pair<int,int>> q2;
while(q.size()){
pair<int,int> a = q.front();
q.pop();
// cout << a.first << " " << a.second << " " << dist << endl;
if(!bs[a.first]){
bs[a.first] = 1;
for(auto i : v[a.first]){
q.push({a.first,i}) , bs2[a.first][i] = 1;
}
}
if(a.first+a.second < n && !bs2[a.first+a.second][a.second]){
q2.push({a.first+a.second,a.second}) , bs2[a.first+a.second][a.second] = 1;
}
if(a.first-a.second >= 0 && !bs2[a.first-a.second][a.second]){
q2.push({a.first-a.second,a.second}) , bs2[a.first-a.second][a.second] = 1;
}
}
q = q2;
if(bs[tag]){
cout << dist << '\n';
return 0;
}
dist += 1;
}
cout << -1 << endl;
}
# | 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... |