8000 Update 2533.cpp · dongju97/basic-algo-lecture@7645c3d · GitHub
[go: up one dir, main page]

Skip to content

Commit 7645c3d

Browse files
Update 2533.cpp
1 parent 226c755 commit 7645c3d

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

0x19/solutions/2533.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@ void mktree(int curr, int prev) {
1414
mktree(nxt, curr);
1515
}
1616
}
17+
// curr번 정점을 얼리어답터로 둘 때 / 두지 않을때에 대한 dp
1718
int dp(int curr, bool isEarlyAdaptor) {
1819
if (state[curr][isEarlyAdaptor] != -1)
1920
return state[curr][isEarlyAdaptor];
2021

2122
int val = 0;
22-
if (isEarlyAdaptor)
23+
if (isEarlyAdaptor) // curr번 정점이 얼리어답터이면 자식은 얼리어답터이든 아니든 상관이 없으니 dp(nxt, false), dp(nxt, true) 중 최솟값을 더하면 됨
2324
for (int nxt : tree[curr])
2425
val += min(dp(nxt, false), dp(nxt, true));
2526
else
26-
for (int nxt : tree[curr])
27+
for (int nxt : tree[curr]) // curr번 정점이 얼리어답터가 아니면 자식은 얼리어답터여야 하니 dp(nxt, true)를 더하면 됨
2728
val += dp(nxt, true);
2829

29-
return state[curr][isEarlyAdaptor] = val + isEarlyAdaptor;
30+
return state[curr][isEarlyAdaptor] = val + isEarlyAdaptor; // isEarlyAdaptor가 true인 경우 얼리어답터의 수를 1 증가시켜야 하기 때문
3031
}
3132
int main(void) {
3233
ios::sync_with_stdio(0);
@@ -46,6 +47,8 @@ int main(void) {
4647
cout << min(dp(1, false), dp(1, true));
4748
}
4849
/*
50+
먼저 임의로 1번 정점을 루트로 정한 후 트리를 구성
4951
state[node][isEarlyAdapter]
50-
: 해당 node가 isEarlyAdapter인 경우에 필요한 최소 얼리 어답터의 수
51-
*/
52+
: 해당 node를 루트로 하는 서브트리에 대해, isEarlyAdapter=true일 경우 node가 얼리어답터일 때 필요한 최소 얼리 어답터의 수
53+
isEarlyAdapter=false일 경우 node가 얼리어답터가 아닐 때 필요한 최소 얼리 어답터의 수
54+
*/

0 commit comments

Comments
 (0)
0