Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #define NMAX 30
- bool adjm[NMAX][NMAX], vis[NMAX];
- int n, m, start;
- int q[NMAX], first, last, st[NMAX], top;
- void read() {
- cin >> n >> m >> start;
- for (int i = 0; i < m; i++) {
- int a, b;
- cin >> a >> b;
- adjm[a][b] = adjm[b][a] = true;
- }
- }
- void bfs() {
- st[++top] = q[last++] = start;
- vis[start] = true;
- while (last != first) {
- int p = q[first++];
- for (int j = 1; j <= n; j++) {
- if (adjm[p][j] && !vis[j]) {
- vis[j] = true;
- st[++top] = q[last++] = j;
- }
- }
- }
- }
- void display() {
- for (int i = 1; i <= top; i++)
- cout << st[i] << ' ';
- }
- int main() {
- read();
- bfs();
- display();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement