@@ -14,19 +14,20 @@ void mktree(int curr, int prev) {
14
14
mktree (nxt, curr);
15
15
}
16
16
}
17
+ // curr번 정점을 얼리어답터로 둘 때 / 두지 않을때에 대한 dp
17
18
int dp (int curr, bool isEarlyAdaptor) {
18
19
if (state[curr][isEarlyAdaptor] != -1 )
19
20
return state[curr][isEarlyAdaptor];
20
21
21
22
int val = 0 ;
22
- if (isEarlyAdaptor)
23
+ if (isEarlyAdaptor) // curr번 정점이 얼리어답터이면 자식은 얼리어답터이든 아니든 상관이 없으니 dp(nxt, false), dp(nxt, true) 중 최솟값을 더하면 됨
23
24
for (int nxt : tree[curr])
24
25
val += min (dp (nxt, false ), dp (nxt, true ));
25
26
else
26
- for (int nxt : tree[curr])
27
+ for (int nxt : tree[curr]) // curr번 정점이 얼리어답터가 아니면 자식은 얼리어답터여야 하니 dp(nxt, true)를 더하면 됨
27
28
val += dp (nxt, true );
28
29
29
- return state[curr][isEarlyAdaptor] = val + isEarlyAdaptor;
30
+ return state[curr][isEarlyAdaptor] = val + isEarlyAdaptor; // isEarlyAdaptor가 true인 경우 얼리어답터의 수를 1 증가시켜야 하기 때문
30
31
}
31
32
int main (void ) {
32
33
ios::sync_with_stdio (0 );
@@ -46,6 +47,8 @@ int main(void) {
46
47
cout << min (dp (1 , false ), dp (1 , true ));
47
48
}
48
49
/*
50
+ 먼저 임의로 1번 정점을 루트로 정한 후 트리를 구성
49
51
state[node][isEarlyAdapter]
50
- : 해당 node가 isEarlyAdapter인 경우에 필요한 최소 얼리 어답터의 수
51
- */
52
+ : 해당 node를 루트로 하는 서브트리에 대해, isEarlyAdapter=true일 경우 node가 얼리어답터일 때 필요한 최소 얼리 어답터의 수
53
+ isEarlyAdapter=false일 경우 node가 얼리어답터가 아닐 때 필요한 최소 얼리 어답터의 수
54
+ */
0 commit comments