b.1707 ์ด๋ถ ๊ทธ๋ํ

https://www.acmicpc.net/problem/1707
๐ ์๋ฐ(Java)๋ก ํธ๋ ์ด๋ถ ๊ทธ๋ํ ํ๋ณ ๋ฌธ์ - ๋ฐฑ์ค 1707 ๐
๐ ๋ฌธ์ ๊ฐ์
๋ฐฑ์ค 1707๋ฒ - ์ด๋ถ ๊ทธ๋ํ ํ๋ณ ๋ฌธ์ ์ ๋๋ค.
์ฃผ์ด์ง ๋ฌด๋ฐฉํฅ ๊ทธ๋ํ๊ฐ ์ด๋ถ ๊ทธ๋ํ์ธ์ง ํ๋ณํ๋ ๊ฒ์ด ๋ชฉํ์ ๋๋ค.
์ด๋ถ ๊ทธ๋ํ๋?
โข ๋ชจ๋ ์ ์ ์ ๋ ๊ฐ์ ๊ทธ๋ฃน์ผ๋ก ๋๋ ์ ์๋ ๊ทธ๋ํ
โข ๊ฐ์ ๊ทธ๋ฃน ๋ด ์ ์ ๋ผ๋ฆฌ๋ ์๋ก ์ฐ๊ฒฐ๋์ง ์์์ผ ํจ
โข ์ฆ, ์ธ์ ํ ์ ์ ์ ํญ์ ๋ค๋ฅธ ๊ทธ๋ฃน์ ์ํด์ผ ํจ
๐ก ์์ ์ ๋ ฅ
2
3 2
1 3
2 3
4 4
1 2
2 3
3 4
4 1
๐ก ์์ ์ถ๋ ฅ
YES
NO
โก ์ฒซ ๋ฒ์งธ ๊ทธ๋ํ๋ ์ด๋ถ ๊ทธ๋ํ์ด๋ฉฐ, ๋ ๋ฒ์งธ ๊ทธ๋ํ๋ ์ด๋ถ ๊ทธ๋ํ๊ฐ ์๋
๐ ์๊ณ ๋ฆฌ์ฆ ์ ๊ทผ ๋ฐฉ์
BFS(๋๋น ์ฐ์ ํ์) ๋๋ DFS(๊น์ด ์ฐ์ ํ์)๋ฅผ ํ์ฉํ ๊ทธ๋ํ ์์น ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํฉ๋๋ค.
โ๏ธ ์ฃผ์ ๊ณ ๋ ค ์ฌํญ
โ BFS๋ฅผ ์ฌ์ฉํ์ฌ ์ ์ ์ ๋ ๊ฐ์ง ์์ผ๋ก ์น ํ๋ฉด์ ํ์
โ ์ธ์ ํ ์ ์ ์ด ๊ฐ์ ์์ด ๋๋ค๋ฉด ์ด๋ถ ๊ทธ๋ํ๊ฐ ์๋
โ ๋ชจ๋ ์ ์ ์ด ์ฐ๊ฒฐ๋์ด ์์ง ์์ ์ ์์ผ๋ฏ๋ก, ๋ชจ๋ ์ ์ ์ ๋ํด BFS ์คํ
โ ์ด๋ถ ๊ทธ๋ํ๋ฉด YES, ์๋๋ฉด NO ์ถ๋ ฅ
๐น Java ์ฝ๋ ํด์ค
import java.util.*;
import java.io.*;
public class Main {
static class Node {
int num;
boolean color;
public Node(int num, boolean color) {
this.num = num;
this.color = color;
}
}
static int T; // ํ
์คํธ ์ผ์ด์ค ๊ฐ์
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
T = stringToInteger(br.readLine());
for (int t = 0; t < T; t++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int N = stringToInteger(st.nextToken()); // ์ ์ ๊ฐ์
int E = stringToInteger(st.nextToken()); // ๊ฐ์ ๊ฐ์
sb.append(logic(N, E, br));
}
System.out.println(sb);
}
// ๊ทธ๋ํ ํ๋ณ ๋ก์ง
static String logic(int N, int E, BufferedReader br) throws IOException {
ArrayList<Integer>[] list = new ArrayList[N + 1];
for (int i = 1; i <= N; i++) {
list[i] = new ArrayList<>();
}
for (int i = 0; i < E; i++) {
StringTokenizer stLogic = new StringTokenizer(br.readLine());
int a = stringToInteger(stLogic.nextToken());
int b = stringToInteger(stLogic.nextToken());
list[a].add(b);
list[b].add(a);
}
int[] colors = new int[N + 1]; // 0: ๋ฏธ๋ฐฉ๋ฌธ, 1: ๋นจ๊ฐ, -1: ํ๋
for (int i = 1; i <= N; i++) {
if (colors[i] == 0) { // ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ์ ์
if (!BFS(i, list, colors)) return "NO\n";
}
}
return "YES\n";
}
// BFS๋ฅผ ํ์ฉํ ์ด๋ถ ๊ทธ๋ํ ํ๋ณ
static boolean BFS(int start, ArrayList<Integer>[] list, int[] colors) {
Queue<Node> q = new ArrayDeque<>();
q.add(new Node(start, true));
colors[start] = 1; // ์์ ์ ์ ์ 1 (๋นจ๊ฐ)
while (!q.isEmpty()) {
Node cur = q.poll();
int curColor = colors[cur.num];
for (int next : list[cur.num]) {
if (colors[next] == 0) { // ๋ฐฉ๋ฌธํ์ง ์์ ์ ์ ์ด๋ฉด
colors[next] = -curColor; // ํ์ฌ ์ ์ ๊ณผ ๋ฐ๋ ์์์ผ๋ก ์น ํจ
q.add(new Node(next, !cur.color));
} else if (colors[next] == curColor) { // ์ธ์ ์ ์ ์ด ๊ฐ์ ์์ด๋ฉด ์ด๋ถ ๊ทธ๋ํ X
return false;
}
}
}
return true;
}
// ๋ฌธ์์ด์ ์ ์๋ก ๋ณํํ๋ ํจ์
static int stringToInteger(String s) {
return Integer.parseInt(s);
}
}
๐ ์ฝ๋ ์ค๋ช
๐ 1. ๊ทธ๋ํ ์ ๋ ฅ ๋ฐ ์ด๊ธฐํ
ArrayList<Integer>[] list = new ArrayList[N + 1];
for (int i = 1; i <= N; i++) {
list[i] = new ArrayList<>();
}
for (int i = 0; i < E; i++) {
StringTokenizer stLogic = new StringTokenizer(br.readLine());
int a = stringToInteger(stLogic.nextToken());
int b = stringToInteger(stLogic.nextToken());
list[a].add(b);
list[b].add(a);
}
โข ์ธ์ ๋ฆฌ์คํธ(list[])๋ฅผ ์ฌ์ฉํ์ฌ ๊ทธ๋ํ๋ฅผ ์ ์ฅ
โข ๋ฌด๋ฐฉํฅ ๊ทธ๋ํ์ด๋ฏ๋ก ์์ชฝ(list[a].add(b) & list[b].add(a))์ ์ถ๊ฐ
๐ 2. ๋ชจ๋ ์ ์ ์ ๋ํด BFS ์คํ
int[] colors = new int[N + 1]; // 0: ๋ฏธ๋ฐฉ๋ฌธ, 1: ๋นจ๊ฐ, -1: ํ๋
for (int i = 1; i <= N; i++) {
if (colors[i] == 0) { // ์์ง ๋ฐฉ๋ฌธํ์ง ์์ ์ ์
if (!BFS(i, list, colors)) return "NO\n";
}
}
โข ๋ชจ๋ ์ ์ (1 ~ N)์ ํ์ธํ๋ฉฐ ๋ฐฉ๋ฌธํ์ง ์์ ์ ์ ์์ BFS ์คํ
โข BFS ์คํ ๊ฒฐ๊ณผ๊ฐ false์ด๋ฉด ์ด๋ถ ๊ทธ๋ํ๊ฐ ์๋๋ฏ๋ก โNOโ ์ถ๋ ฅ
๐ 3. BFS๋ฅผ ์ฌ์ฉํ ์ด๋ถ ๊ทธ๋ํ ํ๋ณ
Queue<Node> q = new ArrayDeque<>();
q.add(new Node(start, true));
colors[start] = 1; // ์์ ์ ์ ์ 1 (๋นจ๊ฐ)์ผ๋ก ์ค์
while (!q.isEmpty()) {
Node cur = q.poll();
int curColor = colors[cur.num];
for (int next : list[cur.num]) {
if (colors[next] == 0) { // ๋ฐฉ๋ฌธํ์ง ์์ ์ ์ ์ด๋ฉด
colors[next] = -curColor; // ๋ฐ๋ ์์์ผ๋ก ์น ํจ
q.add(new Node(next, !cur.color));
} else if (colors[next] == curColor) { // ์ธ์ ์ ์ ์ด ๊ฐ์ ์์ด๋ฉด ์ด๋ถ ๊ทธ๋ํ X
return false;
}
}
}
โข ํ(Queue)๋ฅผ ์ฌ์ฉํ์ฌ BFS ํ์
โข ๊ฐ ์ ์ ์ ๋ ๊ฐ์ ์(1, -1)์ผ๋ก ์น ํ๋ฉด์ ์งํ
โข ์ด๋ฏธ ์น ํด์ง ์ธ์ ์ ์ ์ด ๊ฐ์ ์์ด๋ฉด ์ด๋ถ ๊ทธ๋ํ๊ฐ ์๋
๐ ์ ๋ฆฌ
โ ํต์ฌ ํฌ์ธํธ
โ BFS๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ถ ๊ทธ๋ํ ํ๋ณ
โ ์ ์ ๋ฐฉ๋ฌธ ์ ๋ ๊ฐ์ ์์ผ๋ก ์น ํ๋ฉฐ ํ์
โ O(N + E)์ ์๊ฐ ๋ณต์ก๋๋ก ๋น ๋ฅด๊ฒ ํด๊ฒฐ ๊ฐ๋ฅ
์ด ๊ธ์ด ๋์์ด ๋์ จ๋ค๋ฉด ์ข์์์ ๊ณต์ ๋ถํ๋๋ฆฝ๋๋ค!