• 문제 링크
15900번: 나무 탈출
평소에 사이가 좋지 않던 성원이와 형석이가 드디어 제대로 한 판 붙으려고 한다. 성원이와 형석이 둘과 모두 똑같이 친한 인섭이가 대결 종목을 정해 가져왔다. 바로 '나무 탈출' 이라는 보드게
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 int res = 0;
static List<Integer>[] graph;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
int n = Integer.parseInt(br.readLine());
graph = new ArrayList[n + 1];
for (int i = 1; i <= n; i++)
graph[i] = new ArrayList<>();
for (int i = 1; i < n; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
graph[a].add(b);
graph[b].add(a);
}
dfs(1, -1, 0);
bw.write(res % 2 == 0 ? "No" : "Yes");
bw.flush();
}
private static void dfs(int node, int parent, int depth) {
if (graph[node].size() == 1 && graph[node].get(0) == parent) {
res += depth;
return;
}
for (int next : graph[node]) {
if (next != parent)
dfs(next, node, depth + 1);
}
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 14940 쉬운 최단거리 - Graph Theory / Java (0) | 2023.01.27 |
---|---|
[백준] 14716 현수막 - Graph Theory / Java (0) | 2023.01.26 |
[백준] 1446 지름길 - Graph Theory / Java (0) | 2023.01.24 |
[백준] 13565 침투 - Graph Theory / Java (0) | 2023.01.23 |
[백준] 1388 바닥 장식 - Graph Theory / Java (0) | 2023.01.22 |
댓글