# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
4877 | Qwaz | 파일 삭제 (GA3_delete) | C++98 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cstdio>
#include <vector>
using namespace std;
const int MAX=3020, MAX_N=10020, INF=MAX_N;
int n, m, target, p[MAX], cnt[MAX];
vector < int > children[MAX];
void input(){
scanf("%d%d%d", &n, &m, &target);
int i;
for(i=0; i<n; i++){
int t;
scanf("%d", &t);
cnt[t]++;
}
for(i=0; i<m; i++){
scanf("%d", &p[i]);
if(i > 0) children[p[i]].push_back(i);
}
}
int s[MAX], e[MAX], endNum[MAX], index=1;
void numbering(int now){
s[now] = index;
int i;
for(i=0; i<children[now].size(); i++){
numbering(children[now][i]);
cnt[now] += cnt[children[now][i]];
}
e[now] = index;
endNum[index] = now;
index++;
}
int dp[MAX][MAX_N];
void solve(){
numbering(0);
int i, j;
for(i=1; i<=target; i++)
dp[0][i] = INF;
for(i=1; i<index; i++){
int now = endNum[i];
for(j=0; j<=target; j++){
dp[i][j] = dp[i-1][j];
if(j >= cnt[now] && dp[i][j] > dp[s[now]-1][j-cnt[now]]+1)
dp[i][j] = dp[s[now]-1][j-cnt[now]]+1;
}
}
int res=INF;
for(i=0; i<=target; i++)
res = min(res, dp[index-1][i]+(target-i));
printf("%d\n", res);
}
int main(){
input();
solve();
return 0;
}