You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
775 B
43 lines
775 B
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
const int N = 100010, M = N << 1;
|
|
int n, m;
|
|
|
|
int h[N], e[M], ne[M], idx;
|
|
void add(int a, int b) {
|
|
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
|
|
}
|
|
|
|
int d[N];
|
|
|
|
int bfs() {
|
|
queue<int> q;
|
|
q.push(1);
|
|
d[1] = 0;
|
|
while (q.size()) {
|
|
auto u = q.front();
|
|
q.pop();
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
int j = e[i];
|
|
if (d[j] == -1) {
|
|
d[j] = d[u] + 1;
|
|
q.push(j);
|
|
}
|
|
}
|
|
}
|
|
return d[n];
|
|
}
|
|
|
|
int main() {
|
|
memset(h, -1, sizeof h);
|
|
memset(d, -1, sizeof d);
|
|
cin >> n >> m;
|
|
for (int i = 1; i <= m; i++) {
|
|
int a, b;
|
|
cin >> a >> b;
|
|
add(a, b);
|
|
}
|
|
cout << bfs() << endl;
|
|
return 0;
|
|
} |