- toc {:toc}
ํ์ด
DFS/BFS ์ ๋ํ ์ดํด๋ฅผ ๋ฌป๋ ๋ฌธ์ ์ด๋ค. ๊ทธ๋ํ๋ฅผ ํ์ฑํด์ฃผ๊ณ DFS/BFS๋ฅผ ๊ตฌํํ๋ค. ์ด ๋, ๋ฐฉ๋ฌธ ๊ฐ๋ฅํ ์ ์ ์ด ์ฌ๋ฌ ๊ฐ์ธ ๊ฒฝ์ฐ์๋ ์ ์ ๋ฒํธ๊ฐ ์์ ๊ฒ์ ๋จผ์ ๋ฐฉ๋ฌธํ๋ค๋ ์ ์ ์ฃผ์ํ๋ค. BFS์ DFS์ ์๋์๋ฆฌ๋ฅผ ์๊ฐํ๋ฉด์ ํ์ดํ์. ์๋ ์ฝ๋๋ DFS๋ฅผ ์ฌ๊ท์ ์ผ๋ก ํ์ดํ ๋ฐฉ์์ด๋ค.
#include <bits/stdc++.h>
using namespace std;
vector<int> graph[1005];
vector<pair<int, int>> vec;
queue<int> q;
stack<int> st;
int visit[1005];
void dfs(int v){
visit[v] = 1;
cout << v << ' ';
for(auto link : graph[v]){
if(visit[link] == 0)
dfs(link);
}
}
void bfs(){
while(!q.empty()){
int v = q.front(); q.pop();
cout << v << ' ';
for(auto link : graph[v]){
if(visit[link] == 0){
q.push(link);
visit[link] = 1;
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int N, M, V, val1, val2;
cin >> N >> M >> V;
for(int i=0; i < M; i++){
cin >> val1 >> val2;
if(val1 < val2)
vec.push_back({val1, val2});
else
vec.push_back({val2, val1});
}
sort(vec.begin(), vec.end());
for(int i=0; i < vec.size(); i++){
val1 = vec[i].first;
val2 = vec[i].second;
// ๋ฌด๋ฐฉํฅ ๊ทธ๋ํ
graph[val1].push_back(val2);
graph[val2].push_back(val1);
}
dfs(V);
cout << '\n';
fill(visit, visit+N+1, 0);
q.push(V);
visit[V] = 1;
bfs();
return 0;
}
๋ค์์ ๋ฐฉ์์ DFS๋ฅผ ์คํ์ ์ฌ์ฉํด์ ๊ตฌํํ ๋ฐฉ์์ด๋ค. ์ผ๋ฐ์ ์ธ ๋ฐ๋ํ ๋ฐฐ์ด์์๋ 4 ๋ฐฉํฅ์ผ๋ก ์ด๋ํ๋ฉด์ ์กฐ๊ฑด์ ๋ฐ๋ผ ํ ๋ฒ์ฉ ์คํ์ push ํด์ฃผ๋ฉด ๋์ง๋ง, ๋ฌด๋ฐฉํฅ ๊ทธ๋ํ์์๋ break์ push๋ฅผ ๋ ๋ฒ ํด์ค์ผ๋ก์จ DFS๋ฅผ ๊ตฌํํ๋ค.
์ push๋ฅผ ๋ ๋ฒ ํด์ค์ผ ํ ๊น?
4 5 1 1 2 1 3 1 4 2 4 3 4
๋ค์์ ์ ๋ ฅ์ ์๊ฐํด๋ณด์. 1์ ์์์ผ๋ก ๋ค์ด๊ฐ์ ๋ ๋ฐฉ๋ฌธ ๊ฐ๋ฅํ ์ ์ ์ด ์ฌ๋ฌ ๊ฐ์ด๊ธฐ ๋๋ฌธ์ ๋ฒํธ๊ฐ ์์ ๊ฒ์ ๋ ๋จผ์ ๋ฐฉ๋ฌธํด์ผ ํ๋ค.
- graph(1)์ ๋ค์ด๊ฐ์ ๋ 2, 3, 4 ์ ๋ฐฉ๋ฌธ ๊ฐ๋ฅํ๋ฏ๋ก 2๋ฅผ pushํ๊ณ breakํ๋ค.
- ์ด ํ 2๊ฐ ์คํ์์ pop()๋์ด ์ถ๋ ฅ๋๊ณ 2์ ์ฐ๊ฒฐ๋ ๋ ธ๋ 4๋ก ์ด๋ํ๊ฒ ๋๋ค.
- 4๊ฐ ์คํ์์ pop()๋์ด 3์ผ๋ก ์ด๋ํ๋ค. ๋ค์์ ๋ฐฉ์์ผ๋ก ์งํํ๋ฉด ๊น์ด ์ฐ์ ๋ฐฉ์์ผ๋ก ์งํ ๊ฐ๋ฅํ๋ค.
3 2 1 1 3 2 1 ํ์ง๋ง, ๋ค์์ ์ ๋ ฅ์ ์๊ฐํด๋ณด์.
- 1์ ๋ค์ด๊ฐ์ ๋ 2, 3์ ๋ฐฉ๋ฌธ ๊ฐ๋ฅํ๊ณ , 2๋ฅผ push ํ๊ณ breakํ๋ค.
- 2์ ๋ค์ด๊ฐ์ ๋ 1์ ๋ฐฉ๋ฌธ ๊ฐ๋ฅํ์ง๋ง ์ด๋ฏธ ๋ฐฉ๋ฌธ ํ๊ธฐ์ ๋์ด๊ฐ๋ค.
- DFS๊ฐ ๋๋๋ค. (1, 2 ๋ง ์ถ๋ ฅ)
์์ ๊ฒฝ์ฐ์ฒ๋ผ ํ ๋ฒ๋ง pushํ ๊ฒฝ์ฐ ๋ค์ ์ ์ ์ ๋ฐฉ๋ฌธ ๊ฐ๋ฅํ ์ ์ ์์ ๋์น๋ ์ ์ ์ด ์กด์ฌํ๊ฒ ๋๋ค. ๋ฐ๋ผ์, break ํ๋ ๋์ ๋ค์ ํ ๋ฒ ๋ค์ ์ ์ ์ ๊ฐ์ง ์ ์ ์ push ํ๋ฉด์ ๋์น๋ ์ ์ ์ด ์๋๋ก ๋ง๋ ๋ค.
#include <bits/stdc++.h>
using namespace std;
vector<int> graph[1005];
vector<pair<int, int>> vec;
queue<int> q;
stack<int> st;
int visit[1005];
void dfs(){
int v = st.top();
cout << v << ' ';
while(!st.empty()){
v = st.top(); st.pop();
for(auto link : graph[v]){
if(visit[link] == 0){
visit[link] = 1;
cout << link << ' ';
st.push(v);
st.push(link);
break;
}
}
}
}
void bfs(){
while(!q.empty()){
int v = q.front(); q.pop();
cout << v << ' ';
for(auto link : graph[v]){
if(visit[link] == 0){
q.push(link);
visit[link] = 1;
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int N, M, V, val1, val2;
cin >> N >> M >> V;
for(int i=0; i < M; i++){
cin >> val1 >> val2;
if(val1 < val2)
vec.push_back({val1, val2});
else
vec.push_back({val2, val1});
}
sort(vec.begin(), vec.end());
for(int i=0; i < vec.size(); i++){
val1 = vec[i].first;
val2 = vec[i].second;
// ๋ฌด๋ฐฉํฅ ๊ทธ๋ํ
graph[val1].push_back(val2);
graph[val2].push_back(val1);
}
st.push(V);
visit[V] = 1;
dfs();
cout << '\n';
fill(visit, visit+N+1, 0);
q.push(V);
visit[V] = 1;
bfs();
return 0;
}