Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<algorithm>
- #include<queue>
- #include<vector>
- #include<map>
- using namespace std;
- map<int, bool> isFree;
- vector<vector<int>> adjMatrix;
- void BFS(int start) {
- queue<int> que;
- isFree[start] = false;
- que.push(start);
- for (size_t i = 0; i < adjMatrix.size(); i++)
- {
- for (size_t j = 0; j < adjMatrix[i].size(); j++)
- {
- if (isFree[adjMatrix[i][j]]) {
- que.push(adjMatrix[i][j]);
- isFree[adjMatrix[i][j]] = false;
- }
- }
- }
- while (!que.empty()) {
- start = que.front();
- cout << start+1 << " ";
- que.pop();
- }
- }
- int main() {
- int N, V;
- cin >> N >> V;
- for (size_t i = 0; i < N; i++)
- {
- isFree.insert(make_pair(i,true));
- }
- adjMatrix.resize(N);
- int a, b;
- for (size_t i = 0; i < V; i++)
- {
- cin >> a >> b;
- adjMatrix[a-1].push_back(b-1);
- adjMatrix[b-1].push_back(a-1);
- }
- for (size_t i = 0; i < adjMatrix.size(); i++)
- {
- for (size_t j = 0; j < adjMatrix[i].size(); j++)
- {
- cout << adjMatrix[i][j]+1 << " ";
- }
- cout << endl;
- }
- BFS(2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement