Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- vector<int> gr[10000];
- queue<int> q;
- int n,m;
- int BFS(int a, int b)
- {
- int d[n]={-1};
- d[a]=1;
- q.push(a);int ras=-1;
- while(!q.empty())
- {
- ras=q.front();
- q.pop();
- }
- for(int s: gr[ras])
- {
- if (d[s]==-1)
- {
- q.push(s);d[a]=d[ras]+1;
- }
- }
- return d[b];
- }
- int main()
- {
- cin>>n>>m;
- for(int i=0;i<m;i++){
- int v,v2;
- cin>>v>>v2;
- gr[v].push_back(v2);
- gr[v2].push_back(v);
- }
- int a,b;
- cin>>a>>b;
- cout<<BFS(a,b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement