728x90
๐๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/15900
๐ป์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int N, answer;
static ArrayList<Integer>[] board;
public static void main(String[] args) throws IOException {
N = Integer.parseInt(br.readLine());
board = new ArrayList[N + 1];
for (int i = 0; i <= N; i++) {
board[i] = 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());
board[a].add(b);
board[b].add(a);
}
startGame(1, -1, 0);
//์ด ์ด๋ ๊ฑฐ๋ฆฌ๊ฐ ํ์ -> ์ฑ์์ด๊ฐ ์น๋ฆฌ
System.out.print(answer % 2 == 0 ? "No" : "Yes");
}
private static void startGame(int cur, int parent, int sum) {
if (board[cur].size() == 1 && board[cur].get(0) == parent) {
// ์ฐ๊ฒฐ๋ ๊ฐ์ ์ด ํ๋๊ณ , ๊ทธ๊ฒ ๋ถ๋ชจ์ผ ๊ฒฝ์ฐ -> ๋ฆฌํ ๋
ธ๋
// ์ด ์ด๋ ๊ฑฐ๋ฆฌ ์
๋ฐ์ดํธ
answer += sum;
return;
}
for (int next : board[cur]) {
if (next == parent) continue;
startGame(next, cur, sum + 1);
}
}
}
728x90
'Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Algorithm/Baekjoon] ํ๋ถ ์ฐ๊ตฌ์ ๋ฏผ์ - 21922 (G5/JAVA) (2) | 2025.01.20 |
---|---|
[Algorithm/Baekjoon] ์ฌ๋ฌ๋ถ์ ๋ค๋ฆฌ๊ฐ ๋์ด ๋๋ฆฌ๊ฒ ์ต๋๋ค! - 17352 (G5/JAVA) (0) | 2025.01.06 |
[Algorithm/Baekjoon] ์ ์ ์ฌ์ - 1946 (S1/JAVA) (1) | 2024.12.23 |
[Algorithm/Baekjoon] ์ ๋ฐ ๋ช ๋จ - 3980(G5/JAVA) (0) | 2024.12.16 |
[Algorithm/Baekjoon] ๊ฐ๋ฏธ๊ตด - 14725(G3/JAVA) (3) | 2024.12.09 |