Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define maxn 100005
  3. using namespace std;
  4.  
  5. int n, m;
  6. vector<int> v[maxn];
  7.  
  8. bool used[maxn];
  9. priority_queue<int, vector<int>, greater<int>> pq;
  10. vector<int> ans;
  11.  
  12. int main()
  13. {
  14. cin >> n >> m;
  15. for (int i = 0; i < m; i++)
  16. {
  17. int x, y;
  18. scanf("%d%d", &x, &y);
  19. v[x].push_back(y);
  20. v[y].push_back(x);
  21. }
  22. pq.push(1);
  23. while (!pq.empty())
  24. {
  25. int now = pq.top();
  26. pq.pop();
  27. if (used[now])
  28. continue;
  29. used[now] = 1;
  30. ans.push_back(now);
  31. for (int i = 0; i < v[now].size(); i++)
  32. {
  33. if (used[v[now][i]])
  34. continue;
  35. pq.push(v[now][i]);
  36. }
  37. }
  38. for (int i = 0; i < n; i++)
  39. printf("%d ", ans[i]);
  40. return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement