From af78cddcc6e906b8448eb75d6988b8fb6554c594 Mon Sep 17 00:00:00 2001 From: oncsr Date: Mon, 10 Feb 2025 11:50:24 +0900 Subject: [PATCH] =?UTF-8?q?[20250210]=20BOJ=20/=20=ED=94=8C=EB=9E=983=20/?= =?UTF-8?q?=20Game=20on=20Tree=20/=20=EA=B6=8C=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- khj20006/202502/10 BOJ P3 Game on Tree.md | 79 +++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 khj20006/202502/10 BOJ P3 Game on Tree.md diff --git a/khj20006/202502/10 BOJ P3 Game on Tree.md b/khj20006/202502/10 BOJ P3 Game on Tree.md new file mode 100644 index 00000000..77e54140 --- /dev/null +++ b/khj20006/202502/10 BOJ P3 Game on Tree.md @@ -0,0 +1,79 @@ +```java + +import java.util.*; +import java.io.*; + +class Main { + + // IO field + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + static long nextLong() {return Long.parseLong(st.nextToken());} + static void bwEnd() throws Exception {bw.flush();bw.close();} + + // Additional field + + static int[] d; + static List[] V; + static int N, ans = -1; + + public static void main(String[] args) throws Exception { + + ready(); + solve(); + + bwEnd(); + } + + static void ready() throws Exception{ + + N = Integer.parseInt(br.readLine()); + d = new int[N+1]; + V = new ArrayList[N+1]; + for(int i=1;i<=N;i++) V[i] = new ArrayList<>(); + nextLine(); + for(int i=2;i<=N;i++) { + int p = nextInt(); + V[p].add(i); + } + + } + + static void solve() throws Exception{ + + dfs(1); + if(d[1] <= 1) { + bw.write("FIRST\n"); + bw.write(findAns(1) + "\n"); + } + else bw.write("SECOND"); + + } + + static void dfs(int n) { + if(V[n].isEmpty()) d[n] = 2; + for(int i:V[n]) { + dfs(i); + d[n] += Math.max(0, d[i]-1); + } + } + + static int findAns(int n) { + if(V[n].isEmpty()) return n; + int next = n, max = -1; + for(int i:V[n]) { + if(d[i] > max) { + max = d[i]; + next = i; + } + } + return findAns(next); + } + +} + +```