#include <iostream>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
int visited[1001];
int map[1001][1001];
queue<int>q;
int N, M, V;
void dfs(int n) {
visited[n] = 1; //ํ์ฌ ๋
ธ๋๋ฅผ ๋ฐฉ๋ฌธ์ฒ๋ฆฌ
cout << n << ' ';
for (int i = 1; i <= N; i++) {
if (!visited[i] && map[n][i]) {
dfs(i);
}
}
}
void bfs(int n) {
q.push(n);
visited[n] = 1;
while (!q.empty()) {
int x = q.front(); //ํ์์ ํ๋์ ์์๋ฅผ ๋ฝ์ ์ถ๋ ฅ
q.pop();
cout << x << ' ';
for (int i = 1; i <=N; i++) {
if (!visited[i] && map[x][i]) {
q.push(i);
visited[i] = 1;
}
}
}
}
int main() {
cin >> N >> M >> V;
for (int i = 0; i < M; i++) { // ๊ฐ์ ์ฐ๊ฒฐ
int a, b;
cin >> a >> b;
map[a][b] = 1;
map[b][a] = 1;
}
dfs(V);
cout << "\n";
memset(visited, 0, sizeof(visited));
bfs(V);
}
'Algorithm ๐ง๐ปโ๐ป > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค,c++] 1264๋ฒ - ๋ชจ์์ ๊ฐ์ (0) | 2021.11.02 |
---|---|
[๋ฐฑ์ค,c++] 1261๋ฒ - ์๊ณ ์คํ (0) | 2021.11.02 |
[๋ฐฑ์ค,c++] 12605๋ฒ - ๋จ์ด ์์ ๋ค์ง๊ธฐ (0) | 2021.11.02 |
[๋ฐฑ์ค,c++] 1259๋ฒ - ํฐ๋ฆฐ๋๋กฌ์ (0) | 2021.11.02 |
[๋ฐฑ์ค,c++] 1253๋ฒ - ์ข๋ค (0) | 2021.11.02 |
[๋ฐฑ์ค,c++] 1238๋ฒ - ํํฐ (0) | 2021.11.02 |
๋๊ธ