ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 1260번 C++ 풀이
    백준 2018. 9. 6. 22:45
    #include <iostream>
    #include <queue>
    using namespace std;
    bool buffer[1001][1001];
    bool isVisited[1001];
    bool isBfsVisited[1001];
    queue<int> q;
    int n, m, v;

    void dfs(int start){
    isVisited[start] = true;
    cout<<start<<' ';
    for (int i = 1 ; i<=n; i++){
    if (!isVisited[i] && buffer[start][i]){
    dfs(i);
    }
    }
    }
    void bfs(int start){
    q.push(start);
    isBfsVisited[start] = true;
    cout << start << ' ';
    while(!q.empty()){
    int top = q.front();
    q.pop();
    for (int i = 1 ; i <= n ; i++){
    if (!isBfsVisited[i] && buffer[top][i]){
    q.push(i);
    isBfsVisited[i] = true;
    cout << i << ' ';
    }
    }
    }
    }

    int main(int argc, const char * argv[]) {
    cin.tie(NULL);
    ios::sync_with_stdio(false);
    cin >> n >> m >> v;
    for(int i = 0; i <m; i++){
    int a, b;
    cin >> a >> b;
    buffer[a][b] = true;
    buffer[b][a] = true;
    }
    dfs(v);
    cout << '\n';
    bfs(v);
    return 0;

    }


    '백준' 카테고리의 다른 글

    백준 2667 C++ 풀이  (0) 2018.10.03
    백준 2178번 C++ 풀이  (0) 2018.10.03
    백준 1547번 C++ 풀이  (0) 2018.09.06
    백준 2455번 C++ 풀이  (0) 2018.08.29
    백준 2448번 C++ 풀이  (0) 2018.08.29
Designed by Tistory.