• 문제 링크
17199번: Milk Factory
The milk business is booming! Farmer John's milk processing factory consists of $N$ processing stations, conveniently numbered $1 \ldots N$ ($1 \leq N \leq 100$), and $N-1$ walkways, each connecting some pair of stations. (Walkways are expensive, so Farmer
www.acmicpc.net
• 풀이 과정
• 풀이 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
static Node[] graph;
static boolean[] visit;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
graph = new Node[n];
for (int i = 0; i < n; i++)
graph[i] = new Node(i);
for (int i = 1; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int tgt = Integer.parseInt(st.nextToken()) - 1;
graph[Integer.parseInt(st.nextToken()) - 1].addEdge(tgt);
}
int res = -1;
for (int i = 0; i < n; i++) {
visit = new boolean[n];
dfs(i);
if (isCentral()) {
res = i + 1;
break;
}
}
bw.write(String.valueOf(res));
bw.flush();
}
private static void dfs(int src) {
visit[src] = true;
for (int tgt : graph[src].adj)
if (!visit[tgt]) dfs(tgt);
}
private static boolean isCentral() {
for (boolean flag : visit) if (!flag) return false;
return true;
}
private static class Node {
int src;
List<Integer> adj;
public Node(int src) {
this.src = src;
this.adj = new ArrayList<>();
}
public void addEdge(int tgt) {
adj.add(tgt);
}
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 5848 Message Relay - Graph Theory / Java (0) | 2023.07.05 |
---|---|
[백준] 5938 Daisy Chains in the Field - Graph Theory / Java (0) | 2023.07.04 |
[백준] 5966 Cow Cotillion - Data Structure / Java (0) | 2023.07.02 |
[백준] 23568 Find the House - Data Structure / Java (0) | 2023.07.01 |
[백준] 10654 Cow Jog - Data Structure / Java (0) | 2023.06.30 |
댓글