[백준,c++] 1260번 - DFS와 BFS

[백준,c++] 1260번 - DFS와 BFS

#include #include #include #include using namespace std; int visited[1001]; int map[1001][1001]; queueq; 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 << "

"; memset(visited, 0, sizeof(visited)); bfs(V); }

공유하기 글 요소 저작자표시

from http://dkswnkk.tistory.com/209 by ccl(A) rewrite - 2021-11-02 23:00:35