Algorithm/๊ทธ๋ž˜ํ”„(Graph)

[๋ฐฑ์ค€, Java] 1707๋ฒˆ : ์ด๋ถ„ ๊ทธ๋ž˜ํ”„

Zayson 2022. 4. 27. 23:45

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/1707

๐Ÿค” ๋ฌธ์ œ

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

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

๐Ÿ˜ฎ ๋ฌธ์ œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

์ด๋ถ„ ๊ทธ๋ž˜ํ”„๋Š” ๋ฌธ์ œ์— ๋‚˜์™€ ์žˆ๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ ์ •์ ๋ผ๋ฆฌ ์ด์–ด์ง„ ๊ทธ๋ž˜ํ”„๋ฅผ ํ•˜๋‚˜์˜ ์ง‘ํ•ฉ์œผ๋กœ ๋ณด๊ณ  ์„œ๋กœ ์—ฐ๊ฒฐ๋œ ์ •์ ๋ผ๋ฆฌ๋Š” ๋‹ค๋ฅธ ๋‘ ๊ทธ๋ฃน์œผ๋กœ ์†ํ•˜๊ฒŒ ํ•˜๋Š” ๊ทธ๋ž˜ํ”„๋ฅผ ๋งํ•œ๋‹ค.

image

๋”ฐ๋ผ์„œ, ์ด๋ถ„ ๊ทธ๋ž˜ํ”„๋ฅผ ํŒ๋ณ„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. ์ƒ‰์น ๋˜์ง€ ์•Š์€ ๋ชจ๋“  ์ •์ ์— ๋Œ€ํ•ด BFS๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.
  2. BFS์˜ ์‹œ์ž‘ ์ •์ ์„ ์ž„์˜์˜ ์ƒ‰์ƒ์œผ๋กœ ์ƒ‰์น ํ•œ๋‹ค. → colors[start] = RED(1);
  3. ์—ฐ๊ฒฐ๋œ ์ •์ ์„ ๋‹ค๋ฅธ ์ƒ‰์ƒ์œผ๋กœ ์ƒ‰์น ํ•œ๋‹ค. → colors[next] = colors[start] * -1;
  4. ์—ฐ๊ฒฐ๋œ ์ •์ ์ด ์„œ๋กœ ๋™์ผํ•œ ์ƒ‰์ƒ์„ ๊ฐ€์ง€๋Š” ๊ฒฝ์šฐ ์ด๋ถ„ ๊ทธ๋ž˜ํ”„๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ false ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  5. ๋ชจ๋“  ์ •์ ์ด ๊ฐ ๋…๋ฆฝ๋œ ๊ทธ๋ฃน์„ ๊ตฌ์„ฑํ•˜๋ฉด ์ด๋ถ„ ๊ทธ๋ž˜ํ”„์ด๋‹ค.

๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ๋ฅผ ์ƒ‰์ƒ ๋ฐฐ์—ด๋กœ ์ฒดํ‚น์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

๐Ÿšฉ Java ์ฝ”๋“œ

package com.algorithm.Baekjoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main_1707_์ด๋ถ„_๊ทธ๋ž˜ํ”„ {

    private static List<List<Integer>> graph;
    private static int[] colors;
    private static final int RED = 1;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int K = Integer.parseInt(br.readLine());
        for(int testCase = 0; testCase < K; testCase++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int V = Integer.parseInt(st.nextToken());
            int E = Integer.parseInt(st.nextToken());
            graph = new ArrayList<>();
            colors = new int[V+1];
            // ๊ทธ๋ž˜ํ”„ ์ดˆ๊ธฐํ™”
            for(int vertex =0 ; vertex <= V; vertex++) {
                graph.add(new ArrayList<>());
            }

            // ๊ทธ๋ž˜ํ”„ ์—ฐ๊ฒฐ
            for(int edge = 0; edge < E; edge++) {
                st = new StringTokenizer(br.readLine());
                int from = Integer.parseInt(st.nextToken());
                int to = Integer.parseInt(st.nextToken());

                graph.get(from).add(to);
                graph.get(to).add(from);
            }

            boolean rst = false;
            // 1. ์ƒ‰์น  ๋˜์ง€ ์•Š์€ ๋ชจ๋“  ์ •์ ์— ๋Œ€ํ•ด์„œ
            for(int vertex = 1; vertex <= V; vertex++) {
                if(colors[vertex] == 0) {
                    rst = isBipartiteGraph(vertex, RED);
                }
                if(!rst) break;
            }
            if(rst) System.out.println("YES");
            else System.out.println("NO");
        }
        br.close();
    }

    private static boolean isBipartiteGraph(int start, int color) {
        Queue<Integer> queue = new LinkedList<>();
        queue.add(start);

                // 2. ์‹œ์ž‘ ์ •์  ์ž„์˜์˜ ์ƒ‰์ƒ์œผ๋กœ ์ƒ‰์น 
        colors[start] = color;

        while(!queue.isEmpty()) {
            int cur = queue.poll();
            for(int next : graph.get(cur)) {
                // 4. ์ธ์ ‘ ์ •์  ์ƒ‰์ด ๋™์ผํ•˜๋ฉด ์ด๋ถ„ ๊ทธ๋ž˜ํ”„๊ฐ€ ์•„๋‹˜
                if(colors[cur] == colors[next]) return false;

                // 3. ์ธ์ ‘ ์ •์  ์ƒ‰์น  ์•ˆ๋œ ๊ฒฝ์šฐ ํ˜„์žฌ ์ •์  ๋ฐ˜๋Œ€ ์ƒ‰๊น”๋กœ ์ƒ‰์น 
                                // ์ƒ‰์ƒ ๋ฐฐ์—ด์„ ํ†ตํ•ด ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ ํ™•์ธ ๊ฐ€๋Šฅ
                if(colors[next] == 0) {
                    colors[next] = colors[cur] * -1;
                    queue.add(next);
                }
            }
        }
        return true;
    }
}
๋ฐ˜์‘ํ˜•