Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication211.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iomanip>
- #include <iostream>
- #include <functional>
- #include <algorithm>
- #include <math.h>
- #include <cmath>
- #include <string>
- #include <cstring>
- #include <vector>
- #include<set>
- #include<map>
- #include <time.h>
- #include <fstream>
- #include <queue>
- typedef long long ll;
- using namespace std;
- int n, m;
- vector<int> v[1000];
- ll vis[1000];
- int main()
- {
- cin >> n >> m;
- for (int i = 0; i < m; i++){
- int x, y;
- scanf("%d %d", &x, &y);
- // cin >> x >> y;
- v[x].push_back(y);
- v[y].push_back(x);
- }
- int s;
- cin >> s;
- memset(vis, -1, sizeof vis);
- queue <int> Q;
- Q.push(s);
- vis[s] = 0;
- int h1, h2;
- h1 = s;
- while (!Q.empty())
- {
- h1 = Q.front();
- Q.pop();
- for (int i = 0; i < v[h1].size(); i++){
- int nxt = v[h1][i];
- if (vis[nxt] == -1){
- vis[nxt] = vis[h1] + 1;
- Q.push(nxt);
- }
- }
- }
- for (int i = 0; i < n; i++){
- printf("%d %d\n", i, vis[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement