์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[๋ฐฑ์ค€(BOJ)] 1707๋ฒˆ: ์ด๋ถ„ ๊ทธ๋ž˜ํ”„ (DFS/BFS, C++)

PROGRAMMING/Algorithm

by koharin 2021. 2. 25. 16:19

๋ณธ๋ฌธ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“ ๋ฌธ์ œํ’€์ด ๊ณผ์ •


BOJ:11724(์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜) ๋ฌธ์ œ์™€ ์œ ์‚ฌํ•˜๋‹ค.

์ด ๋ฌธ์ œ์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์กฐ๊ฑด์„ ํ™•์ธํ•˜๋Š” ๊ณผ์ •์ด ์ถ”๊ฐ€๋œ๋‹ค.

 

์ด๋ถ„ ๊ทธ๋ž˜ํ”„ ํ™•์ธ ๋ฐฉ๋ฒ•

  1. ์„œ๋กœ ์ธ์ ‘ํ•œ ์ •์ ์€ ์„œ๋กœ ๋‹ค๋ฅธ ์ƒ‰์ด๋‹ค.
    • ํ˜„์žฌ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•œ ์ •์ ๊ณผ ์ธ์ ‘ํ•œ ์ •์ ์€ +, -๋กœ ๋‹ค๋ฅธ ๋ถ€ํ˜ธ๋ฅผ ์ค€๋‹ค.
    • ํ˜„์žฌ ํƒ์ƒ‰ํ•œ ์ •์ ์˜ ๊ฐ’์ด current์ด๋ฉด, ํ•ด๋‹น ์ •์ ์—๋Š” current ๊ฐ’์„ ์ฃผ๊ณ , ํ•ด๋‹น ์ •์ ๊ณผ ์ธ์ ‘ํ•œ ์ •์ ์€ -current๋ฅผ ์ค˜์„œ ์„œ๋กœ ๋‹ค๋ฅธ ๊ทธ๋ฃน์— ์†ํ•˜๋„๋ก ํ•œ๋‹ค.
  2. ์ •์ ๊ณผ ์ธ์ ‘ํ•œ ์ •์ ์„ ๋ฐฉ๋ฌธํ•  ๋•Œ ๋งŒ์•ฝ ๊ฐ™์€ ์ƒ‰์ด๋ผ๋ฉด ์ด๋ถ„ ๊ทธ๋ž˜ํ”„๊ฐ€ ์•„๋‹ˆ๋‹ค.
    • ํ˜„์žฌ ํƒ์ƒ‰ ์ •์ ๊ณผ ์ธ์ ‘ํ•œ ์ •์  ์ค‘์—์„œ ์ด๋ฏธ ๋ฐฉ๋ฌธ์„ ํ–ˆ๊ณ (color[num] != 0), color[num+ color[current!= 0์ด ์•„๋‹ˆ๋ผ๋ฉด ๊ฐ™์€ ๊ทธ๋ฃน์— ์†ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ด๋ถ„ ๊ทธ๋ž˜ํ”„๊ฐ€ ์•„๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ CheckBipartite = false๋กœ ์ฃผ๊ณ  ๋ฆฌํ„ดํ•œ๋‹ค.

 

๋ชจ๋“  ์ •์ ์ด ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š๊ณ , ์•„์˜ˆ ์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„๊ฐ€ ์•„๋‹ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ดˆ๊ธฐ ํ•˜๋‚˜๋งŒ ๊ฐ’์„ ์ค˜์„œ ํƒ์ƒ‰์„ ํ•˜์ง€ ์•Š๊ณ  ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๋ชจ๋“  ์ •์ ์— ๋Œ€ํ•ด BFS ํƒ์ƒ‰์„ ํ–ˆ๋‹ค.

 

 

๐Ÿ’ป C++ ์ œ์ถœ์ฝ”๋“œ


#include <bits/stdc++.h> //<stdio.h><stack><queue><deque>
using namespace std;

void DFS(int start, deque<int> *graph, int *color, bool *CheckBipartite){
    stack<int> s;
    color[start] = start;
    s.push(start);

    while(!s.empty()){
        int current = s.top();
        s.pop();
        for(int next: graph[current]){
            if(color[next] == 0){
                s.push(current);
                s.push(next);
                color[next] = color[current]*-1;
            }
            else if(color[next] + color[current] != 0){
                *CheckBipartite = false;
                return;
            }
        }
    }
}

void BFS(int start, deque<int> *graph, int *color, bool *CheckBipartite){
    queue<int> q;
    color[start] = start; // +start
    q.push(start);
    //printf("[+] %d visited. color[%d] = %d\n", start, start, color[start]);

    while(!q.empty()){
        int current = q.front();
        q.pop();
        for(int num: graph[current]){
            if(color[num] == 0){ // if not visited
                q.push(num);
                color[num] = color[current]*-1; // -start 
                //printf("[+] %d visited. color[%d] = %d\n", num, num, color[num]);
            }
            else if(color[num] + color[current] != 0) {
                *CheckBipartite = false;
                return; 
            }
        }
    }
}

int main(){
    int K; // K: ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜
    scanf("%d", &K);

    for(int i=0; i<K; i++){
        int V, E; // V: ์ •์ ์˜ ๊ฐœ์ˆ˜, E: ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜
        scanf("%d %d", &V, &E);
        
        deque<int> graph[V+1];
        int color[V+1]; // check not visitied by 0
        bool CheckBipartite = true;
        fill(color, color+V+1, 0); // initialize

        for(int j=0; j<E; j++){
            int u, v;
            scanf("%d %d", &u, &v);
            graph[u].push_back(v);
            graph[v].push_back(u);
        }
        for(int j=1; j<=V; j++){
            if(CheckBipartite == false) break;
            if(color[j] == 0) BFS(j, graph, color, &CheckBipartite);
            //if(color[j] == 0) DFS(j, graph, color, &CheckBipartite);
        }
        printf("%s\n", CheckBipartite == true ? "YES" : "NO");
    }
}

DFS์˜ ๊ฒฝ์šฐ, stack์œผ๋กœ ๊ตฌํ˜„ํ•ด์„œ ์ธ์ ‘ํ•œ ๋…ธ๋“œ push ์ „ current๋„ ๋„ฃ์–ด์„œ current์˜ ์ž์‹๋…ธ๋“œ๊นŒ์ง€ ๋จผ์ € ํƒ์ƒ‰ ํ›„ ์ธ์ ‘๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜๋„๋ก ํ–ˆ๋‹ค.

 

 

๐Ÿ‘ ๊ฒฐ๊ณผ


728x90
๋ฐ˜์‘ํ˜•

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ