๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Data Structure๐Ÿงถ

๊ทธ๋ž˜ํ”„ ์ด๋ก  - ์ด๋ถ„ ๊ทธ๋ž˜ํ”„

by Jouureee 2021. 2. 25.

๐Ÿ”˜์ด๋ถ„ ๊ทธ๋ž˜ํ”„๋ž€ ?

 

์ธ์ ‘ํ•œ ์ •์ ๋ผ๋ฆฌ ์„œ๋กœ ๋‹ค๋ฅธ ์ƒ‰์œผ๋กœ ์น ํ•ด์„œ ๋ชจ๋“  ์ •์ ์„ ๋‘๊ฐ€์ง€ ์ƒ‰์œผ๋กœ๋งŒ ์น ํ•  ์ˆ˜ ์žˆ๋Š” ๊ทธ๋ž˜ํ”„

๊ทธ๋ž˜ํ”„์˜ ๋ชจ๋“  ์ •์ ์ด ๋‘ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆ ์ง€๊ณ  ์„œ๋กœ ๋‹ค๋ฅธ ๊ทธ๋ฃน์˜ ์ •์ ์ด ๊ฐ„์„ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด์ ธ ์žˆ๋Š” ๊ทธ๋ž˜ํ”„

 

โžฟ์ด๋ถ„ ๊ทธ๋ž˜ํ”„ ํŠน์ง•

  • ์ด๋ถ„ ๊ทธ๋ž˜ํ”„์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ BFS, DFS ํƒ์ƒ‰์„ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.
  • ์ด๋ถ„ ๊ทธ๋ž˜ํ”„๋Š” BFS๋ฅผ ํ•  ๋•Œ ๊ฐ™์€ ๋ ˆ๋ฒจ์˜ ์ •์ ๋ผ๋ฆฌ๋Š” ๋ฌด์กฐ๊ฑด ๊ฐ™์€ ์ƒ‰์œผ๋กœ ์น ํ•ด์ง„๋‹ค.
  • ์—ฐ๊ฒฐ ์„ฑ๋ถ„์˜ ๊ฐœ์ˆ˜(Connected Component)๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์œ ์‚ฌํ•˜๋‹ค.
  • ๋ชจ๋“  ์ •์ ์„ ๋ฐฉ๋ฌธํ•˜๋ฉฐ ๊ฐ„์„ ์„ ๊ฒ€์‚ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(V+E)๋กœ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๊ฐ™๋‹ค.

 

โ˜‘๏ธ์ด๋ถ„ ๊ทธ๋ž˜ํ”„ ํŒ๋ณ„ ์•Œ๊ณ ๋ฆฌ์ฆ˜

  • ์„œ๋กœ ์ธ์ ‘ํ•œ ์ •์ ์ด ๊ฐ™์€ ์ƒ‰์ด๋ฉด ์ด๋ถ„ ๊ทธ๋ž˜ํ”„๊ฐ€ ์•„๋‹ˆ๋‹ค.
  • BFS, DFS ๋กœ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์ •์  ๋ฐฉ๋ฌธ ํ•  ๋•Œ๋งˆ๋‹ค ๋‘๊ฐ€์ง€ ์ƒ‰ ์ค‘ ํ•˜๋‚˜๋ฅผ ์น ํ•œ๋‹ค.
  • ๋‹ค์Œ ์ •์  ๋ฐฉ๋ฌธํ•˜๋ฉด์„œ ์ž์‹ ๊ณผ ์ธ์ ‘ํ•œ ์ •์ ์€ ์ž์‹ ๊ณผ ๋‹ค๋ฅธ ์ƒ‰์œผ๋กœ ์น ํ•œ๋‹ค.
  • ํƒ์ƒ‰ ์ง„ํ–‰ ํ•  ๋•Œ ์ž์‹ ๊ณผ ์ธ์ ‘ํ•œ ์ •์ ์˜ ์ƒ‰์ด ์ž์‹ ๊ณผ ๋™์ผํ•˜๋ฉด ์ด๋ถ„ ๊ทธ๋ž˜ํ”„๊ฐ€ ์•„๋‹ˆ๋‹ค.
    • BFS์˜ ๊ฒฝ์šฐ ์ •์ ์„ ๋ฐฉ๋ฌธํ•˜๋‹ค๊ฐ€ ๋งŒ์•ฝ ๊ฐ™์€ ๋ ˆ๋ฒจ์—์„œ ์ •์ ์„ ๋‹ค๋ฅธ ์ƒ‰์œผ๋กœ ์น ํ•ด์•ผ ํ•œ๋‹ค๋ฉด ๋ฌด์กฐ๊ฑด ์ด๋ถ„ ๊ทธ๋ž˜ํ”„๊ฐ€ ์•„๋‹ˆ๋‹ค.
  • ๋ชจ๋“  ์ •์  ๋‹ค ๋ฐฉ๋ฌธํ–ˆ๋Š”๋ฐ ์œ„์™€ ๊ฐ™์€ ๊ฒฝ์šฐ ์—†๋‹ค๋ฉด ์ด๋ถ„ ๊ทธ๋ž˜ํ”„๋‹ค
  • ์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„์™€ ๋น„์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„ ๋‘˜ ๋‹ค ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค.
    • ๊ทธ๋ž˜ํ”„๊ฐ€ ๋น„์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„์ธ๊ฒฝ์šฐ ๋ชจ๋“  ์ •์ ์— ๋Œ€ํ•ด ํ™•์ธํ•˜๋Š” ์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค.

c++ ์ฝ”๋“œ 

//
//  1707_bgd.cpp
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2021/02/25.
//

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

enum color { RED, BLUE };
 
vector<int> RED_GROUP;
vector<int> BLUE_GROUP;
string res = "";
int visit[20001];
vector<int> map[20001];
int cnt = 0;
int v, e;

void dfs(int vertex, int color){
    visit[vertex] = color;
    
    if(color == RED){
        RED_GROUP.push_back(vertex);
    }else if(color == BLUE){
        BLUE_GROUP.push_back(vertex);
    }
    cnt++;
    //cout << "m : " << map[vertex].size() << " " << endl;
    
    if(cnt == v) return; // vertex ๋‹ค ๋Œ๋ฉด ์ข…๋ฃŒ
    
    for(int i = 0; i < (int)map[vertex].size(); i++){
        //cout << map[vertex][i]  << " " << endl;
        if(visit[map[vertex][i]] == -1){
            dfs(map[vertex][i], (color + 1) % 2);
        }
        if(visit[map[vertex][i]] == color){
            res = "NO";
            return;
        }
    }
    
}

int main(){
    cin >> v >> e; // vertex, edge
    memset(visit, -1, sizeof(visit));
    
    //fill(map.begin(), map.begin() + 20001, -1);
    res = "YES";
    for(int i = 0; i < e; i++){
        int v1, v2;
        cin >> v1 >> v2;
        map[v1].push_back(v2);
        map[v2].push_back(v1);
    }
    
    for(int j = 1; j <= v; j++){
        if(visit[j] == -1){ // ๋ฐฉ๋ฌธ ํ•˜์ง€ x์œผ๋ฏ€๋กœ
            dfs(j, RED);
        }
    }
    
    cout << "์ด๋ถ„ ๊ทธ๋ž˜ํ”„ ์—ฌ๋ถ€ "  << res << endl;
    
    if (!res.compare("YES")) {
       cout << "RED GROUP : ";
       for (int i = 0; i < (int)RED_GROUP.size(); i++) {
           cout << RED_GROUP[i] << " ";
       }
       cout << "\nBLUE GROUP : ";
       for (int i = 0; i < (int)BLUE_GROUP.size(); i++) {
           cout << BLUE_GROUP[i] << " ";
       }
       cout << endl;
    }
    
    return 0;
}

 

 

 

 

 

 

 

 

 

 

 

๋Œ“๊ธ€