본문 바로가기
Problem Solving/Baekjoon

[백준] 6188 Clear Cold Water - Graph Theory / Java

by graycode 2024. 1. 7.

 문제 링크

 

6188번: Clear Cold Water

The steamy, sweltering summers of Wisconsin's dairy district stimulate the cows to slake their thirst. Farmer John pipes clear cold water into a set of N (3 <= N <= 99999; N odd) branching pipes conveniently numbered 1..N from a pump located at the barn. A

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Main {

    static int[] arr;

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringBuilder sb = new StringBuilder();

        int n = read(), c = read();

        arr = new int[n + 1];
        while (c-- > 0) {
            int p = read();
            arr[read()] = p;
            arr[read()] = p;
        }

        for (int i = 1; i <= n; i++) sb.append(dfs(i, 1)).append("\n");

        bw.write(sb.toString());
        bw.flush();
    }

    private static int dfs(int p, int cnt) {
        if (p == 1) return cnt;
        return dfs(arr[p], cnt + 1);
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글