Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- ์๋ฎฌ๋ ์ด์
- ์คํ
- CVE
- error
- ๋์ ํฉ
- ๋ฐ์ดํฌ์คํธ๋ผ
- JPA
- dynamic debugging
- GCP
- ์ฌ๊ท
- ๋ถํ ์ ๋ณต
- Reversing
- c++
- ๊ตฌํ
- DP
- BFS
- ๊ทธ๋ฆฌ๋
- web
- java
- ๋ฌธ์์ด
- OS
- ๋งต
- ์ต๋จ ๊ฒฝ๋ก
- ์ฐ์ ์์ ํ
- dfs
- ๋ฐฑํธ๋ํน
- Spring
- ์์ ์ ๋ ฌ
- ์ด๋ถ ํ์
- thymeleaf
Archives
- Today
- Total
hades
[Baekjoon] 1260๋ฒ: DFS์ BFS ๋ณธ๋ฌธ
๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/1260
๐ ์ค๊ณ
DFS์ BFS๋ฅผ ๊ตฌํํ๋ ๊ณผ์ ์์ ์ฌ๋ฐ๋ฅด๊ฒ ์ถ๋ ฅํ๋ฉด ๋๋ ๋ฌธ์ ์ด๋ค.
๋ฐฉ๋ฌธํ ์ ์๋ ์ ์ ์ด ์ฌ๋ฌ ๊ฐ์ธ ๊ฒฝ์ฐ์๋ ์ ์ ๋ฒํธ๊ฐ ์์ ๊ฒ์ ๋จผ์ ๋ฐฉ๋ฌธํ๋ค๋ ์กฐ๊ฑด์ด ์์ผ๋ฏ๋ก, ์ฐ๊ฒฐํ ์ ์ ์ ์ ๋ ฌํด์ผ ํ๋ค. ๊ฐ์ ์ ์๊ฐ ์ต๋ 10000๊ฐ์ด๋ฏ๋ก, ์๋ฐฉํฅ์ ๊ณ ๋ คํ๋ฉด ์ด ๊ฐ์ ์ ์๋ 20000๊ฐ์ด๋ค. ์ ๋ ฌ์ ํ๋ฉด, O(NlgN)์ด๋ฏ๋ก ์๊ฐ ์ ํ์ ์ด๊ณผํ์ง ์๋๋ค.
๐ ํ์ด
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;
int n, m, v, a, b;
vector<vector<int>> adj_list(1001);
vector<bool> visited(1001);
void dfs(int cur_point){
visited[cur_point] = true;
cout << cur_point << " ";
for (int i=0; i<adj_list[cur_point].size(); i++){
int new_point = adj_list[cur_point][i];
if (!visited[new_point]){
dfs(new_point);
}
}
}
void bfs(int start_point){
queue<int> q;
q.push(start_point);
visited[start_point] = true;
cout << start_point << " ";
while (!q.empty()){
int cur_point = q.front();
q.pop();
for (int i=0; i<adj_list[cur_point].size(); i++){
int new_point = adj_list[cur_point][i];
if (!visited[new_point]){
visited[new_point] = true;
q.push(new_point);
cout << new_point << " ";
}
}
}
}
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(false);
cin >> n >> m >> v;
for (int i=0; i<m; i++){
cin >> a >> b;
adj_list[a].push_back(b);
adj_list[b].push_back(a);
}
for (int i=1; i<=n; i++){
sort(adj_list[i].begin(), adj_list[i].end());
}
dfs(v);
visited = vector<bool>(1001);
cout << "\n";
bfs(v);
return 0;
}
๐ ๋ฉ๋ชจ
BFS๋ฅผ ๊ตฌํํ๋ ๊ณผ์ ์์ ์ ์ ๋ฒํธ๊ฐ ์์ ๊ฒ์ ๋จผ์ ๋ฐฉ๋ฌธํ๊ธฐ ์ํด ์ฐ์ ์์ ํ๋ฅผ ์ฌ์ฉํ๋ ค ํ์ผ๋ ๋ฐฉ๋ฌธ ์์๊ฐ ๋ง์ง ์๊ฒ ๋๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํ๋ค.
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 1620๋ฒ: ๋๋์ผ ํฌ์ผ๋ชฌ ๋ง์คํฐ ์ด๋ค์ (0) | 2024.07.07 |
---|---|
[Baekjoon] 1541๋ฒ: ์์ด๋ฒ๋ฆฐ ๊ดํธ (0) | 2024.07.05 |
[Baekjoon] 1074๋ฒ: Z (0) | 2024.07.02 |
[Baekjoon] 1012๋ฒ: ์ ๊ธฐ๋ ๋ฐฐ์ถ (0) | 2024.07.02 |
[Baekjoon] 1003๋ฒ: ํผ๋ณด๋์น ํฉ (0) | 2024.07.02 |