Skip to content

Commit 32ff104

Browse files
authored
[20250404] BOJ / P3 / 정원 정리 / 권혁준
1 parent 6a3b637 commit 32ff104

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 = new StringTokenizer("");
12+
13+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
14+
static String nextToken() throws Exception {
15+
while(!st.hasMoreTokens()) nextLine();
16+
return st.nextToken();
17+
}
18+
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
19+
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
20+
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
21+
static void bwEnd() throws Exception {bw.flush();bw.close();}
22+
23+
// Additional field
24+
25+
static int N, M;
26+
static boolean[][] edge;
27+
static int[] sub;
28+
static int[][] dp;
29+
static final int INF = 1234;
30+
31+
public static void main(String[] args) throws Exception {
32+
33+
ready();
34+
solve();
35+
36+
bwEnd();
37+
38+
}
39+
40+
static void ready() throws Exception{
41+
42+
N = nextInt();
43+
M = nextInt();
44+
edge = new boolean[N+1][N+1];
45+
sub = new int[N+1];
46+
for(int i=1;i<N;i++) {
47+
int a = nextInt(), b = nextInt();
48+
edge[a][b] = edge[b][a] = true;
49+
}
50+
dp = new int[N+1][];
51+
52+
}
53+
54+
static void solve() throws Exception{
55+
56+
pre(1,0);
57+
dfs(1,0);
58+
int ans = dp[1][M];
59+
for(int i=2;i<=N;i++) if(sub[i] >= M) ans = Math.min(ans, dp[i][M] + 1);
60+
bw.write(ans + "\n");
61+
62+
}
63+
64+
static void pre(int n, int p) {
65+
sub[n] = 1;
66+
for(int i=1;i<=N;i++) if(edge[n][i] && i != p) {
67+
pre(i,n);
68+
sub[n] += sub[i];
69+
}
70+
}
71+
72+
static void dfs(int n, int p) {
73+
74+
dp[n] = new int[sub[n]+1];
75+
Arrays.fill(dp[n], INF);
76+
dp[n][1] = 0;
77+
dp[n][0] = 1;
78+
for(int i=1;i<=N;i++) if(edge[n][i] && i != p) {
79+
dfs(i,n);
80+
int[] ndp = new int[sub[n]+1];
81+
Arrays.fill(ndp, INF);
82+
for(int k=0;k<=sub[i];k++) {
83+
for(int g=sub[n];g>=k;g--) {
84+
ndp[g] = Math.min(ndp[g], dp[i][k] + dp[n][g-k]);
85+
}
86+
}
87+
dp[n] = ndp;
88+
}
89+
if(n != 1) dp[n][0] = 1;
90+
}
91+
92+
}
93+
94+
```

0 commit comments

Comments
 (0)