Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- const int maxn = 100005;
- vector<int> graph[maxn];
- int main() {
- ios_base::sync_with_stdio(false);
- int num_of_vertices;
- cin >> num_of_vertices;
- int num_of_edges;
- cin >> num_of_edges;
- for(int i = 0; i < num_of_edges; i++) {
- int a, b;
- cin >> a >> b;
- graph[a].push_back(b);
- graph[b].push_back(a);
- }
- int S, E;
- cin >> S >> E;
- queue<int> q;
- q.push(S);
- q.push(0);
- vector<bool> visited(num_of_vertices, false);
- while(!q.empty()) {
- int c_node = q.front();
- q.pop();
- int dist = q.front();
- q.pop();
- if(c_node == E) {
- cout << dist << endl;
- return 0;
- }
- for(int i = 0; i < graph[c_node].size(); i++) {
- int neighbour = graph[c_node][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