728x90
알고리즘 스터디
오늘의 문제는 트리 문제
https://www.acmicpc.net/problem/15900
간단했지만.. 출력을 잘못한 탓에... 몇분 시간을 썼다.. 문제를 잘 볼 것..!!
// https://www.acmicpc.net/problem/15900
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
static int N;
static List<List<Integer>> lists = new ArrayList<>();
static boolean[] visited;
static int res = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
input(br);
pro();
}
static void pro() {
dfs(0, 1);
System.out.println(res % 2 == 0 ? "No" : "Yes");
}
static void dfs(int dep, int cur){
visited[cur] = true;
int count = 0;
for(int i : lists.get(cur)){
if(visited[i]) continue;
count++;
dfs(dep+1, i);
}
if(count == 0) res+= dep;
}
static void input(BufferedReader br) throws IOException {
N = Integer.parseInt(br.readLine());
StringTokenizer st;
visited = new boolean[N+1];
for (int i = 0; i <= N; i++) {
lists.add(new ArrayList<>());
}
for (int i = 0; i < N-1; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
lists.get(a).add(b);
lists.get(b).add(a);
}
}
}
728x90
'TIL' 카테고리의 다른 글
| [TIL] 2025.05.23(금) 진법 변환 (0) | 2025.05.23 |
|---|---|
| [TIL] 2025.05.22(목) (0) | 2025.05.23 |
| [TIL] 2025.05.21(수) 내 코드는 되는 코드일까?, 이력서 수정 (0) | 2025.05.22 |
| [TIL] 2025.05.20(화) (0) | 2025.05.21 |
| [TIL]2025.05.17(토) (1) | 2025.05.18 |