#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1684 KB |
Output is correct |
2 |
Correct |
0 ms |
1684 KB |
Output is correct |
3 |
Correct |
0 ms |
1684 KB |
Output is correct |
4 |
Correct |
0 ms |
1684 KB |
Output is correct |
5 |
Correct |
0 ms |
1684 KB |
Output is correct |
6 |
Correct |
0 ms |
1684 KB |
Output is correct |
7 |
Correct |
0 ms |
1684 KB |
Output is correct |
8 |
Correct |
0 ms |
1684 KB |
Output is correct |
9 |
Correct |
0 ms |
1684 KB |
Output is correct |
10 |
Incorrect |
20 ms |
2216 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |