Skip to content

Commit b82ff8b

Browse files
authored
Merge pull request #66 from AlgorithmWithGod/khj20006
[20250210] BOJ / 플래3 / Game on Tree / 권혁준
2 parents b48d443 + af78cdd commit b82ff8b

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
class Main {
7+
8+
// IO field
9+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
11+
static StringTokenizer st;
12+
13+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
14+
static int nextInt() {return Integer.parseInt(st.nextToken());}
15+
static long nextLong() {return Long.parseLong(st.nextToken());}
16+
static void bwEnd() throws Exception {bw.flush();bw.close();}
17+
18+
// Additional field
19+
20+
static int[] d;
21+
static List<Integer>[] V;
22+
static int N, ans = -1;
23+
24+
public static void main(String[] args) throws Exception {
25+
26+
ready();
27+
solve();
28+
29+
bwEnd();
30+
}
31+
32+
static void ready() throws Exception{
33+
34+
N = Integer.parseInt(br.readLine());
35+
d = new int[N+1];
36+
V = new ArrayList[N+1];
37+
for(int i=1;i<=N;i++) V[i] = new ArrayList<>();
38+
nextLine();
39+
for(int i=2;i<=N;i++) {
40+
int p = nextInt();
41+
V[p].add(i);
42+
}
43+
44+
}
45+
46+
static void solve() throws Exception{
47+
48+
dfs(1);
49+
if(d[1] <= 1) {
50+
bw.write("FIRST\n");
51+
bw.write(findAns(1) + "\n");
52+
}
53+
else bw.write("SECOND");
54+
55+
}
56+
57+
static void dfs(int n) {
58+
if(V[n].isEmpty()) d[n] = 2;
59+
for(int i:V[n]) {
60+
dfs(i);
61+
d[n] += Math.max(0, d[i]-1);
62+
}
63+
}
64+
65+
static int findAns(int n) {
66+
if(V[n].isEmpty()) return n;
67+
int next = n, max = -1;
68+
for(int i:V[n]) {
69+
if(d[i] > max) {
70+
max = d[i];
71+
next = i;
72+
}
73+
}
74+
return findAns(next);
75+
}
76+
77+
}
78+
79+
```

0 commit comments

Comments
 (0)