#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int main() {
vector<vector<int> > g;
vector<bool> v;
queue<int> q;
int n, m;
int count = 0;
scanf("%d %d", &n, &m);
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);
}
// cout << "queue init start\n";
// find solution
q.push(1);
v[1] = true;
// cout << "queue init\n";
while(!q.empty()) {
int curr = q.front();
// cout << "curr "<< curr << endl;
q.pop();
for(vector<int>::iterator it = g[curr].begin(); it!=g[curr].end();it++) {
if(*it == n) {
printf("%d\n", count+1);
return 0;
}
if(!v[*it] && *it != n) {
v[*it] = true;
q.push(*it);
}
}
count++;
}
if(!v[n])
printf("-1\n");
if(count == 0)
printf("0\n");
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1684 KB |
Output is correct |
2 |
Correct |
0 ms |
1684 KB |
Output is correct |
3 |
Incorrect |
0 ms |
1684 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |