Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- #include <vector>
- #include <queue>
- using namespace std;
- vector<int> g[30005];
- int res[30005];
- int main()
- {
- ifstream fin("pathbge1.in");
- ofstream fout("pathbge1.out");
- int n, s = 1, m, u, v;
- fin >> n >> m;
- for (int i = 0; i < m; i++)
- {
- fin >> u >> v;
- g[v].push_back(u);
- g[u].push_back(v);
- }
- queue<int> q;
- q.push(s);
- while (!q.empty())
- {
- u = q.front();
- q.pop();
- for(int i = 0; i < g[u].size(); i++)
- {
- if (!res[v] && v!=s)
- {
- res[v] = res[u] + 1;
- q.push(v);
- }
- }
- }
- for (int i = 1; i <= n; i++)
- {
- if (i == s) fout << 0 <<" ";
- else
- {
- if (res[i] != 0) fout << res[i]<<" ";
- else fout << -1 <<" ";
- }
- }
- fout << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement