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 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 dfs(int p) {
- vis[p] = true;
- st[++top] = p;
- for (int j = 1; j <= n; j++) {
- if (adjm[p][j] && !vis[j]) {
- dfs(j);
- }
- }
- }
- void display() {
- for (int i = 1; i <= top; i++)
- cout << st[i] << ' ';
- }
- int main() {
- read();
- dfs(start);
- display();
- return 0;
- }
Add Comment
Please, Sign In to add comment