Skip to content

Commit 401bd3e

Browse files
authored
[20250307] BOJ / P4 / Denouncing Mafia / 권혁준
1 parent 0ec52fa commit 401bd3e

File tree

1 file changed

+75
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)