Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define maxn 100005
- using namespace std;
- int n, m;
- vector<int> v[maxn];
- bool used[maxn];
- priority_queue<int, vector<int>, greater<int>> pq;
- vector<int> ans;
- int main()
- {
- cin >> n >> m;
- for (int i = 0; i < m; i++)
- {
- int x, y;
- scanf("%d%d", &x, &y);
- v[x].push_back(y);
- v[y].push_back(x);
- }
- pq.push(1);
- while (!pq.empty())
- {
- int now = pq.top();
- pq.pop();
- if (used[now])
- continue;
- used[now] = 1;
- ans.push_back(now);
- for (int i = 0; i < v[now].size(); i++)
- {
- if (used[v[now][i]])
- continue;
- pq.push(v[now][i]);
- }
- }
- for (int i = 0; i < n; i++)
- printf("%d ", ans[i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement