algorithm

BOJ 1707

rkawk 2022. 9. 27. 22:20

 

๋”๋ณด๊ธฐ

์ด๋ถ„ ๊ทธ๋ž˜ํ”„ ์„ฑ๊ณต

 
2 ์ดˆ 256 MB 69417 18251 10951 23.507%

๋ฌธ์ œ

๊ทธ๋ž˜ํ”„์˜ ์ •์ ์˜ ์ง‘ํ•ฉ์„ ๋‘˜๋กœ ๋ถ„ํ• ํ•˜์—ฌ, ๊ฐ ์ง‘ํ•ฉ์— ์†ํ•œ ์ •์ ๋ผ๋ฆฌ๋Š” ์„œ๋กœ ์ธ์ ‘ํ•˜์ง€ ์•Š๋„๋ก ๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ์„ ๋•Œ, ๊ทธ๋Ÿฌํ•œ ๊ทธ๋ž˜ํ”„๋ฅผ ํŠน๋ณ„ํžˆ ์ด๋ถ„ ๊ทธ๋ž˜ํ”„ (Bipartite Graph) ๋ผ ๋ถ€๋ฅธ๋‹ค.

๊ทธ๋ž˜ํ”„๊ฐ€ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด ๊ทธ๋ž˜ํ”„๊ฐ€ ์ด๋ถ„ ๊ทธ๋ž˜ํ”„์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋ณ„ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š”๋ฐ, ์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ์ฒซ์งธ ์ค„์—๋Š” ๊ทธ๋ž˜ํ”„์˜ ์ •์ ์˜ ๊ฐœ์ˆ˜ V์™€ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E๊ฐ€ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ์ •์ ์—๋Š” 1๋ถ€ํ„ฐ V๊นŒ์ง€ ์ฐจ๋ก€๋กœ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ™์–ด ์žˆ๋‹ค. ์ด์–ด์„œ ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ E๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, ๊ฐ ์ค„์— ์ธ์ ‘ํ•œ ๋‘ ์ •์ ์˜ ๋ฒˆํ˜ธ u, v (u ≠ v)๊ฐ€ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค. 

์ถœ๋ ฅ

K๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ๊ทธ๋ž˜ํ”„๊ฐ€ ์ด๋ถ„ ๊ทธ๋ž˜ํ”„์ด๋ฉด YES, ์•„๋‹ˆ๋ฉด NO๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

์ œํ•œ

  • 2 ≤ K ≤ 5
  • 1 ≤ V ≤ 20,000
  • 1 ≤ E ≤ 200,000

์˜ˆ์ œ ์ž…๋ ฅ 1 ๋ณต์‚ฌ

2
3 2
1 3
2 3
4 4
1 2
2 3
3 4
4 2

์˜ˆ์ œ ์ถœ๋ ฅ 1 ๋ณต์‚ฌ

YES
NOโ€‹

BFS๋‚˜ DFS๋กœ brute-force๋ฅผ ํ•ด์„œ ๊ฐ ๋…ธ๋“œ์— ์ด์ „ ๋…ธ๋“œ์™€ ๋‹ค๋ฅธ ์ƒ‰๊น”(0 or 1)์„ ๋„ฃ์–ด์ค€ ๋‹ค์Œ

๋งŒ์•ฝ ๊ทธ๋ž˜ํ”„์˜ ๊ฐ„์„ ์ด ๊ฐ™์€ ์ƒ‰์˜ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฉด, ์ด๋ถ„ ๊ทธ๋ž˜ํ”„๊ฐ€ ๋  ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์„ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ์ œ๋Œ€๋กœ ๋ณด์ง€ ๋ชปํ•˜๊ณ  ๋‹น์—ฐํžˆ ๋…ธ๋“œ๋ผ๋ฆฌ ๋‹ค ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š”์ค„ ์•Œ์•„์„œ ์‹œ์ž‘์ ์„ 1๋กœ ๋„ฃ๊ณ  ๋Œ๋ ธ๋Š”๋ฐ, ๊ณ„์†ํ‹€๋ ค์„œ ๋ชจ๋“  ๋…ธ๋“œ์— ๋Œ€ํ•œ ํƒ์ƒ‰์„ ๋‹ค ๋Œ๋ฆฌ๋‹ˆ๊นŒ ๋งž์•˜๋‹ค. ์—ฐ๊ฒฐ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๋„ ์ƒ๊ฐํ•ด์•ผ ๋œ๋‹ค.

 

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int K, V, E;
int main(){
    cin>>K;
    while(K--){
        vector<vector<int>>graph(20001);
        bool color[20001]={0};
        int vis[20001] = {0};
        cin>>V>>E;
        for(int i=0; i<E; i++){
            int a, b;
            cin>>a>>b;
            graph[a].push_back(b);
            graph[b].push_back(a);
        }
        // int cur = 1, quit = 0;
        // color[cur] = 0;
        // vis[cur] = 1;
        // //0 -> black, 1 -> red
        // queue<pair<int,int>>q;
        // //first -> node, second -> color
        // q.push({cur, 0});
        int quit = 0;
        for(int i=1; i<=V; i++){
            if(vis[i]) continue;
            color[i] = 0;
            vis[i] = 1;
            queue<pair<int,int>>q;
            q.push({i,0});
            while(!q.empty()){
                auto cur_node = q.front().first;
                auto cur_color = q.front().second;
                q.pop();
                for(auto edge : graph[cur_node]){
                    if(vis[edge]){
                        if(color[edge] == cur_color){
                            //end
                            quit = 1;
                            break;
                        }
                        continue;
                    }
                    vis[edge] = 1;
                    color[edge] = !cur_color;
                    q.push({edge,!cur_color});
                }
                if(quit) break;
            }
            if(quit) break;
        }
        if(!quit){
            for(int i=1; i<=V; i++)
                for(auto edge : graph[i]){
                    if(color[i] == color[edge]) quit=1;
                }
        }
        if(!quit)
            cout<<"YES\n";
        else
            cout<<"NO\n";
            
    }
}