From 401bd3eb2ed7b64863575ed4bda90119a2c8b4ee Mon Sep 17 00:00:00 2001 From: oncsr Date: Fri, 7 Mar 2025 20:58:49 +0900 Subject: [PATCH] =?UTF-8?q?[20250307]=20BOJ=20/=20P4=20/=20Denouncing=20Ma?= =?UTF-8?q?fia=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/202503/07 BOJ P4 Denouncing Mafia.md | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 khj20006/202503/07 BOJ P4 Denouncing Mafia.md diff --git a/khj20006/202503/07 BOJ P4 Denouncing Mafia.md b/khj20006/202503/07 BOJ P4 Denouncing Mafia.md new file mode 100644 index 00000000..341bb1f8 --- /dev/null +++ b/khj20006/202503/07 BOJ P4 Denouncing Mafia.md @@ -0,0 +1,75 @@ +```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 N, K; + static List[] V; + static int[] C; + + public static void main(String[] args) throws Exception { + + ready(); + solve(); + + bwEnd(); + + } + + static void ready() throws Exception{ + + nextLine(); + N = nextInt(); + K = nextInt(); + + V = new List[N+1]; + C = new int[N+1]; + for(int i=1;i<=N;i++) V[i] = new ArrayList<>(); + + nextLine(); + for(int i=2;i<=N;i++) V[nextInt()].add(i); + + } + + static void solve() throws Exception{ + + int ans = 0; + for(int x = dfs(1);K > 0 && x >= 1;) { + if(C[x] > 0) { + C[x]--; + ans += x; + K--; + } + else x--; + } + + bw.write(ans + "\n"); + + } + + static int dfs(int n) { + int mx = 0; + for(int i:V[n]) mx = Math.max(mx, dfs(i)); + C[mx++]--; + C[mx]++; + return mx; + } + +} + +```