-
Notifications
You must be signed in to change notification settings - Fork 0
/
A.cpp
69 lines (55 loc) · 1.3 KB
/
A.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
int main(){
fast;
ll n,m,u,v;
cin>>n>>m;
map<pair<ll,ll>,ll> mp;
for(ll i=0;i<m;i++){
cin>>u>>v;
mp[{u,v}] = 1;
mp[{v,u}] = 1;
}
ll vis[n+1] = {0};
ll level[n+1] = {0};
queue<ll> q;
q.push(1);
vis[1] = 1;
while(!q.empty()){
ll val = q.front();
q.pop();
for(ll i=1;i<=n;i++){
if(!vis[i] && mp[{val,i}]){
q.push(i);
vis[i] = 1;
level[i] = level[val] + 1;
}
}
}
ll ans = level[n], flag=0;
if(level[n]==0)
flag = 1;
memset(vis,0,sizeof(vis));
memset(level,0,sizeof(level));
q.push(1);
vis[1] = 1;
while(!q.empty()){
ll val = q.front();
q.pop();
for(ll i=1;i<=n;i++){
if(!vis[i] && !mp[{val,i}]) {
q.push(i);
vis[i] = 1;
level[i] = level[val] + 1;
}
}
}
ans = max(ans,level[n]);
if(level[n]==0)
flag = 1;
cout<<(flag ? -1 : ans)<<endl;
return 0;
}