image.png

처음 문제 보자마자 푼 풀이(틀림, 연결 리스트 이용함)

/**
 *  2025-05-01
 *  문제016_백준 13023
 * */
package day5.B12023;

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("입력 대기 중");
        String[] firstLine = br.readLine().split(" ");
        System.out.println("firstLine = " + Arrays.toString(firstLine));
        int N = Integer.parseInt(firstLine[0]);
        int M = Integer.parseInt(firstLine[1]);

        int D1, D2;
        Node root = null;

        for (int i = 0; i < M; i++) {
            String[] line = br.readLine().split(" ");
            D1 = Integer.parseInt(line[0]);
            D2 = Integer.parseInt(line[1]);

            Node first = new Node(D1);
            Node second = new Node(D2);
            first.next = second;

            if (root == null) {
                root = first;
            } else {
                Node curr = root;
                while (curr.next != null) {
                    curr = curr.next;
                }
                curr.next = first;
            }
        }

        // 연결 길이 계산
        int count = 0;
        Node curr = root;
        while (curr != null) {
            count++;
            curr = curr.next;
        }

        System.out.println(count >= 5 ? 1 : 0);
    }
}

image.png

왜 DFS 쓰지?

→ DFS는 한 경로를 따라 최대한 깊게 들어가서 갈 곳이 없을 때까지 탐색하니까.

🌷기본 코드 구현 시 필요한 것

DFS 쓴 풀이

package day5.B12023;

import java.io.*;
import java.util.*;

public class Main {
    static class State {
        int node, depth;
        public State(int node, int depth) {
            this.node = node;
            this.depth = depth;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(br.readLine()); // 사람 수
        int M = Integer.parseInt(br.readLine()); // 친구 관계 수

        List<List<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < N; i++) {
		       graph.add(new ArrayList<>());
       }

        // 양방향 그래프 구성
        for (int i = 0; i < M; i++) {
            String[] line = br.readLine().split(" ");
            int a = Integer.parseInt(line[0]);
            int b = Integer.parseInt(line[1]);

            graph.get(a).add(b);
            graph.get(b).add(a);
        }

        boolean found = false;

        for (int start = 0; start < N; start++) {
            boolean[] isVisited = new boolean[N];
            Stack<State> stack = new Stack<>();
            stack.push(new State(start, 1));

            while (!stack.isEmpty()) {
                State current = stack.pop();
                int state = current.node;
                int depth = current.depth;

                if (depth == 5) {
                    found = true;
                    break;
                }

                if (isVisited[state]) continue;
                isVisited[state] = true;

                for (int next : graph.get(state)) {
                    if (!isVisited[next]) {
                        stack.push(new State(next, depth + 1));
                    }
                }
            }

            if (found) break;
        }

        System.out.println(found ? 1 : 0);
    }
}