• 문제 링크
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;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 26999 Satellite Photographs - Graph Theory / Java (0) | 2024.01.09 |
---|---|
[백준] 6018 Tea Time - Graph Theory / Java (0) | 2024.01.08 |
[백준] 9255 The Friend of My Enemy is My Enemy - Graph Theory / Java (0) | 2024.01.06 |
[백준] 6601 Knight Moves - Graph Theory / Java (0) | 2024.01.05 |
[백준] 8975 PJESMA - Data Structure / Java (0) | 2024.01.04 |
댓글