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 <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
vector<bool> v;
int find(vector<vector<int> > &g, queue<int> &q, int level, int n){
if(q.empty()) return 0;
int curr;
queue<int> next;
while(!q.empty()) {
curr = q.front();
q.pop();
for(vector<int>::iterator it = g[curr].begin(); it!=g[curr].end();it++) {
if(*it == n && v[*it] == false) {
printf("%d\n", level+1);
return level;
}
if(!v[*it] && *it != n) {
v[*it] = true;
next.push(*it);
}
}
}
return find(g, next, level+1, n);
}
int main() {
vector<vector<int> > g;
queue<int> q;
int n, m;
scanf("%d %d", &n, &m);
if(n==1) {
printf("0\n");
return 0;
}
g.resize(n+1);
v.resize(n+1);
// make graph
for (int i=m;i!=0;i--) {
int x,y;
scanf("%d %d", &x, &y);
g[x].push_back(y);
}
// find solution
q.push(1);
v[1] = true;
if(find(g, q, 0, n) == 0){
printf("-1\n");
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |