본문 바로가기
CodingTest/Baekjoon

[Baekjoon] 1260번 DFS와 BFS

by 김 원 2025. 5. 11.

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

const int MaxCount = 10001;
vector<int> Graph[MaxCount];

bool VisitedDFS[MaxCount] = { false };
void DFS(int V)
{
    VisitedDFS[V] = true;
    cout << V << ' ';
    for (auto iter : Graph[V])
    {
        if (VisitedDFS[iter] == false)
        {
            VisitedDFS[iter] = true;
            DFS(iter);
        }
    }
}

void BFS(int V)
{
    queue<int> Queue;
    bool VisitedBFS[MaxCount] = { false };

    Queue.push(V);
    VisitedBFS[V] = true;

    while (Queue.empty() == false)
    {
        int front = Queue.front();
        Queue.pop();
        cout << front << ' ';

        for (auto iter : Graph[front])
        {
            if (VisitedBFS[iter] == false)
            {
                VisitedBFS[iter] = true;
                Queue.push(iter);
            }
        }
    }
}

int main() 
{
    int N, M, V;
    cin >> N >> M >> V;

    for (size_t i = 0; i < M; i++)
    {
        int A, B;
        cin >> A >> B;

        Graph[A].push_back(B);
        Graph[B].push_back(A);
    }

    for (size_t i = 1; i <= N; i++)
        sort(Graph[i].begin(), Graph[i].end());

    DFS(V);
    cout << "\n";
    BFS(V);

    return 0;
}

'CodingTest > Baekjoon' 카테고리의 다른 글

[Baekjoon] 2667번 단지번호붙이기  (0) 2025.05.11
[Baekjoon] 2178번 미로 탐색  (1) 2025.05.11
[Baekjoon] 1920번 수 찾기  (0) 2025.05.11
[Baekjoon] 2751번 수 정렬하기 2  (0) 2025.05.10
[Baekjoon] 9012번 괄호  (1) 2025.05.10