Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <vector>
- using namespace std;
- const int maxn = 1e5 + 10;
- vector<int> graph[maxn];
- int main()
- {
- int n;
- cin >> n;
- int m;
- cin >> m;
- for(int i = 0; i < m; i++) {
- int a, b;
- cin >> a >> b;
- a--; b--;
- graph[a].push_back(b);
- graph[b].push_back(a);
- }
- int S, E;
- cin >> S >> E;
- S--;
- E--;
- queue<int> q;
- q.push(S);
- q.push(0);
- vector<bool> visited(n, false);
- while(!q.empty()) {
- int c = q.front();
- q.pop();
- int dist = q.front();
- q.pop();
- if(c == E) {
- cout << dist << endl;
- return 0;
- }
- for(int i = 0; i < (int) graph[c].size(); i++) {
- int neighbour = graph[c][i];
- if(!visited[neighbour]) {
- q.push(neighbour);
- q.push(dist + 1);
- visited[neighbour] = true;
- }
- }
- }
- cout << 0 << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement