Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <map>
- #include <vector>
- #include <algorithm>
- #include <unordered_set>
- #include <set>
- #include <deque>
- using namespace std;
- int main() {
- int n, m;
- cin >> n >> m;
- vector<vector<int>> g(n);
- for (int i = 0; i < m; i++) {
- int x, y;
- cin >> x >> y;
- x--, y--;
- g[x].push_back(y);
- g[y].push_back(x);
- }
- int start;
- cin >> start;
- start--;
- vector<int> rst(n, -1);
- rst[start] = 0;
- deque<int> q;
- q.push_back(start);
- while (!q.empty()) {
- int h = q.front();
- q.pop_front();
- for (auto e : g[h]) {
- if (rst[e] == -1) {
- rst[e] = rst[h] + 1;
- q.push_back(e);
- }
- }
- }
- for (auto e : rst) cout << e << ' ';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment