๐ ๋ฌธ์
https://www.acmicpc.net/problem/16947

๐ ์์ด๋์ด
1. Cycle์ ์ฐพ๋๋ค. -> DFS
2. Cycle์ ์ํ ๊ฒ๋ค๋ง ์ถ๋ ค ๊ฑฐ๊ธฐ์ ๋ถํฐ ์์๋๋ ๊ฒ๋ค์ Depth(distance)์ ์ฐพ๋๋ค. -> BFS
์ค์ ํ๋์ :
casting ์ค๋ฅ (Integer ๊ณผ Integer์ ๋น๊ตํ๋ฉด reference๋ง ๋น๊ตํ๋ค. ์ฃผ์ ํ์)
Cycle์ ์ฐพ๋๋ฒ : (ํ๊ธฐ ๋ ๊ฐ์ ๋ฐฉ๋ฒ์ ์ธ์ดํด์ด ํ๋์ผ๋๋ง ์๊ฐํ ๊ฒ์ ๋๋ค)
1. union find ( findParents(a) == findParents(b) ์ธ ๊ฒฝ์ฐ๋ฉด ์ด๋ฏธ ๋๊ฐ๊ฐ ๊ฐ์ ๋ถ๋ชจ๋ฅผ ํฅํ๋ ๊ฒ์ผ๋ก ์ธ์ดํด์ ์กด์ฌํจ์ ์๋ฏธ)
2. DFS (์ด๋ฏธ ๋ฐฉ๋ฌธ๋ ๋ ธ๋๋ผ๋ฉด ์ธ์ดํด์ด ์กด์ฌํจ์ ์๋ฏธํ๋ค)
๐ ํ์ด
package ๋ฐฑ์ค.๊ตฌํ;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Boj_16947_์์ธ์งํ์ฒ 2ํธ์ {
private static List<List<Integer>> graphs = new ArrayList<>();
private static boolean[] visited;
private static boolean[] isInCycle;
private static int[] distance;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
graphs.add(new ArrayList<>());
}
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken()) - 1;
int b = Integer.parseInt(st.nextToken()) - 1;
graphs.get(a).add(b);
graphs.get(b).add(a);
}
visited = new boolean[N];
isInCycle = new boolean[N];
findCycle(0, -1, new ArrayList<>());
distance = new int[N];
foundDepth();
for (int d : distance) {
sb.append(d).append(" ");
}
System.out.println(sb);
}
//bfs
private static void foundDepth() {
Queue<Integer> queue = new LinkedList<>();
visited = new boolean[graphs.size()];
for (int i = 0; i < isInCycle.length; i++) {
if (isInCycle[i]) {
queue.add(i);
visited[i] = true;
distance[i] = 0;
}
}
int tempDepth = 1;
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
int current = queue.poll();
for (Integer child : graphs.get(current)) {
if (!visited[child]) {
distance[child] = tempDepth;
queue.add(child);
visited[child] = true;
}
}
}
tempDepth++;
}
}
//dfs
private static boolean findCycle(int current, int parent, ArrayList<Integer> path) {
visited[current] = true;
path.add(current);
for (Integer next : graphs.get(current)) {
//๋ฐฉํฅ ๊ฐ์ ์ผ๋ก ๋ถ๋ชจ๋ ๋ฌด์ํ๋ค.
if (next == parent) continue;
if (visited[next]) {
//path๋ด์ ํ์ฌ ๋
ธ๋๊ฐ ์์ํ ์์น๋ถํฐ ํ์ฌ๊น์ง๋ฅผ ์ธ์ดํด๋ก ์ทจ๊ธํ๋ค.
coloredCycle(next, path);
return true;
}
if(findCycle(next, current, path)) return true;
}
path.remove(path.size() - 1);
return false;
}
private static void coloredCycle(int cycleStart, ArrayList<Integer> containsPath) {
boolean marking = false;
//Integer์ ํ๋ณํ ํ์ฌ ๋ณํํด์ผ ๋น๊ต ๋๋ค.
for (int node : containsPath) {
if (cycleStart == node) marking = true;
if (marking) isInCycle[node] = true;
}
}
}
'๋ฐฑ์ค(boj)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [JAVA]1111 IQ ํ ์คํธ (0) | 2025.04.14 |
|---|---|
| [JAVA]1153 ๋ค ๊ฐ์ ์์ (1) | 2025.04.13 |
| [JAVA]1377 ๋ฒ๋ธ ์ํธ, ์ ๋ ฌ (0) | 2025.04.12 |
| [JAVA]11437 LCA ๊ณตํต ๋ถ๋ชจ ๋ ธ๋ ์ฐพ๊ธฐ(๊ณจ๋ 3) (0) | 2023.09.20 |
| [JAVA]13418 ํ๊ต ํ๋ฐฉํ๊ธฐ, ๊ณจ๋ 3 (0) | 2023.08.21 |