Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- void bfs(int r, vector<bool>& used, vector<vector<int>>& g, vector<int>& way) {
- queue<int> q;
- q.push(r);
- used[r] = true;
- while (!q.empty()) {
- int v = q.front();
- q.pop();
- way.push_back(v);
- for (int i = 0; i < g[v].size(); ++i) {
- int to = g[v][i];
- if (used[to] == false) {
- used[to] = true;
- q.push(to);
- }
- }
- }
- }
- int main() {
- int n, m, r;
- cin >> n >> m >> r;
- vector<vector<int>> g(n);
- vector<bool> used(n, false);
- vector<int> way;
- for (int i = 0; i < m; ++i) {
- int a, b;
- cin >> a >> b;
- --a, --b;
- g[a].push_back(b);
- g[b].push_back(a);
- }
- bfs(r - 1, used, g, way);
- cout << way.size() << endl;
- for (auto x : way) cout << x + 1 << " ";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement