• 문제 링크
6125번: Treasure Cave
Bessie's grandfather was a pirate who accumulated a great treasure chest of golden plunder. He hid the treasure chest in a cave that Bessie has recently discovered right on Farmer John's land! Just inside the cave's entrance she found a map that told her h
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
public class Main {
static int t;
static Node[] tree;
static boolean[] visit;
static Deque<Integer> dq = new ArrayDeque<>();
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int p = read(), ns = read();
t = read();
tree = new Node[++p];
visit = new boolean[p];
while (p-- > 1) tree[p] = new Node(p);
while (ns-- > 0) {
int src = read();
tree[src].addEdge(read());
tree[src].addEdge(read());
}
dfs(1);
sb.append(dq.size()).append("\n");
while (!dq.isEmpty()) sb.append(dq.poll()).append("\n");
bw.write(sb.toString());
bw.flush();
}
private static boolean dfs(int src) {
visit[src] = true;
dq.offer(src);
if (src == t) return true;
for (int i : tree[src].adj) if (!visit[i] && dfs(i)) return true;
visit[src] = false;
dq.pollLast();
return false;
}
private static class Node {
int src;
List<Integer> adj;
Node(int src) {
this.src = src;
this.adj = new ArrayList<>();
}
public void addEdge(int tgt) {
adj.add(tgt);
}
}
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' 카테고리의 다른 글
[백준] 14471 포인트 카드 - Greedy / Java (0) | 2023.11.19 |
---|---|
[백준] 21313 문어 - Greedy / Java (0) | 2023.11.19 |
[백준] 11322 Numbers are Easy - Graph Theory / Java (0) | 2023.11.16 |
[백준] 11448 Ga - Graph Theory / Java (0) | 2023.11.15 |
[백준] 6004 The Chivalrous Cow - Graph Theory / Java (0) | 2023.11.14 |
댓글