QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#571216#6430. Monster HunterflyfreemrnWA 53ms67504kbC++142.3kb2024-09-17 21:14:302024-09-17 21:14:30

Judging History

你现在查看的是最新测评结果

  • [2024-09-17 21:14:30]
  • 评测
  • 测评结果:WA
  • 用时:53ms
  • 内存:67504kb
  • [2024-09-17 21:14:30]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define MAXN 2010
#define N 1000000000000
ll hd[MAXN],ed[MAXN*2],nxt[MAXN*2];
ll dp[MAXN][2][MAXN][2],val[MAXN],siz[MAXN],cnt[MAXN];
ll n,idx,T;
inline ll read(){
    ll x=0,f=1;
    char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-')f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
} 
void build(ll x,ll y){
    nxt[++idx]=hd[x];
    hd[x]=idx;
    ed[idx]=y;
}
void dfs(ll now){
//    cout<<now<<endl;
    for(int i=0;i<=n;i++)dp[now][1][i][0]=N,dp[now][0][i][0]=N;
    dp[now][1][1][0]=0,dp[now][0][0][0]=val[now];
    siz[now]=1;
    cnt[now]=0;
    for(int i=hd[now];i;i=nxt[i]){
        ll y=ed[i];
        dfs(y);
        cnt[now]++;
        siz[now]+=siz[y];
        for(int j=0;j<=siz[now];j++){
            dp[now][1][j][cnt[now]%2]=N,dp[now][0][j][cnt[now]%2]=N;
            for(int u=0;u<=min(siz[y],j);u++){
                dp[now][0][j][cnt[now]%2]=min(dp[now][0][j][cnt[now]%2],dp[now][0][j-u][(cnt[now]-1)%2]+min(dp[y][0][u][cnt[y]%2]+val[y],dp[y][1][u][cnt[y]%2]));
                if(j)dp[now][1][j][cnt[now]%2]=min(dp[now][1][j][cnt[now]%2],dp[now][1][j-u][(cnt[now]-1)%2]+min(dp[y][0][u][cnt[y]%2],dp[y][1][u][cnt[y]%2]));
//                cout<<now<<" "<<j<<" "<<u<<" "<<dp[now][0][j][cnt[now]%2]<<" "<<dp[now][1][j][cnt[now]%2]<<endl;
            }
//            cout<<now<<" "<<j<<" "<<dp[now][0][j][cnt[now]%2]<<" "<<dp[now][1][j][cnt[now]%2]<<endl;
        }
    }
}
void clear(){
//    memset(dp,125,sizeof(dp));
//    memset(siz,0,sizeof(siz));
//    memset(cnt,0,sizeof(cnt));
    memset(hd,0,sizeof(hd));
    memset(ed,0,sizeof(ed));
    memset(nxt,0,sizeof(nxt));
    idx=0;
}
signed main(){
    T=read();
    while(T--){
        n=read();
        clear();
        for(int i=1;i<n;i++){
            ll p=read();
            build(p,i+1);
        } 
        for(int i=1;i<=n;i++)val[i]=read();
        dfs(1);
        for(int i=0;i<=n;i++){
            if(!i)cout<<dp[1][0][0][cnt[1]%2]<<" ";
            else if(i==n)cout<<"0\n";
            else cout<<min(dp[1][1][i][cnt[1]%2],dp[1][0][i][cnt[1]%2])<<" "; 
        }
        cout<<endl;
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 5848kb

input:

3
5
1 2 3 4
1 2 3 4 5
9
1 2 3 4 3 4 6 6
8 4 9 4 4 5 2 4 1
12
1 2 2 4 5 3 4 3 8 10 11
9 1 3 5 10 10 7 3 7 9 4 9

output:

29 16 9 4 1 0

74 47 35 25 15 11 7 3 1 0

145 115 93 73 55 42 32 22 14 8 4 1 0


result:

ok 29 tokens

Test #2:

score: 0
Accepted
time: 1ms
memory: 3900kb

input:

179
20
1 1 1 4 5 5 7 7 9 9 11 12 13 14 5 16 17 16 19
3 9 3 2 7 7 2 8 5 7 5 4 7 4 2 4 9 2 7 9
19
1 1 3 4 3 6 7 6 6 10 10 12 13 13 12 16 16 18
8 8 3 6 10 1 1 1 2 2 3 3 3 10 5 5 7 10 5
2
1
10 4
2
1
2 7
14
1 1 3 4 4 6 4 8 9 10 11 8 13
4 4 6 6 10 8 9 5 7 1 4 7 9 8
6
1 2 3 3 5
2 7 5 6 1 6
11
1 2 3 3 5 6 6...

output:

209 182 159 137 117 99 81 65 56 47 39 32 25 19 15 11 8 6 4 2 0

178 151 129 108 89 74 64 54 44 36 29 23 18 13 10 7 5 2 1 0

18 4 0

16 2 0

172 137 111 93 78 63 49 39 31 23 16 10 5 1 0

52 33 21 9 3 1 0

109 72 53 39 29 22 16 10 5 2 1 0

105 69 47 35 25 17 12 7 3 0

156 133 113 97 82 68 56 44 33 26 ...

result:

ok 2178 tokens

Test #3:

score: 0
Accepted
time: 0ms
memory: 5948kb

input:

177
10
1 2 3 3 3 6 7 8 3
750920741 600457069 885939487 614833472 917972842 716271451 234536309 280049219 394290544 802674020
5
1 2 2 4
381361244 652246733 111336853 652733347 864548837
7
1 2 2 4 4 4
374076965 100213690 316923584 132783452 321143617 617096325 590521323
14
1 1 3 4 4 6 6 6 4 10 11 10 1...

output:

11644969567 6821338808 5469960998 4515572016 3564764256 2646791414 1844117394 1229283922 628826853 234536309 0

4943092784 2773077253 1145431444 492698097 111336853 0

4531440947 2737112778 2103265610 1466071235 823784001 365780594 100213690 0

15231373225 11922670663 9482266269 7732763282 605239618...

result:

ok 2176 tokens

Test #4:

score: 0
Accepted
time: 3ms
memory: 14388kb

input:

15
97
1 2 2 4 5 2 7 8 9 10 11 11 13 14 15 16 16 16 19 19 21 21 23 24 11 26 26 28 29 30 31 31 33 34 33 36 37 36 39 40 31 42 43 44 43 46 42 48 42 50 51 52 51 54 54 50 57 58 57 28 61 62 62 64 61 66 67 67 67 70 70 72 67 74 67 76 77 61 79 80 81 82 83 82 85 85 87 88 89 88 91 88 93 82 95 96
727261601 26107...

output:

107163950031 102732928896 98999901499 95286804744 91721257442 88258895738 85278119210 82530570741 79871508580 77382736169 74969945304 72561689905 70288550600 68157169720 66078603230 64055840678 62145527840 60274097298 58428791326 56711378906 54998171184 53305415604 51640777030 50049643668 4848686165...

result:

ok 2010 tokens

Test #5:

score: 0
Accepted
time: 3ms
memory: 16328kb

input:

13
171
1 1 3 3 5 6 6 6 9 10 11 12 13 14 15 16 12 6 19 20 20 22 22 24 25 22 27 28 29 27 27 27 33 33 33 36 37 38 39 40 40 42 39 44 33 46 33 48 48 48 51 51 53 53 55 56 53 58 59 48 61 61 63 64 65 66 67 68 69 70 69 72 73 73 72 69 77 69 68 80 81 82 83 84 85 82 87 88 80 90 91 92 93 93 95 93 97 98 99 91 101...

output:

187664021257 183101322620 178652629666 174903443687 171181863321 167508022266 164048979331 160694988110 157366569229 154144572418 150953357906 147898194220 145072963639 142252129359 139436947703 136631607337 133985709265 131363892815 128765304704 126168063880 123664992681 121166439175 118792530215 1...

result:

ok 1969 tokens

Test #6:

score: -100
Wrong Answer
time: 53ms
memory: 67504kb

input:

2
1000
1 2 3 4 5 6 6 8 8 10 11 12 13 14 12 16 17 16 19 20 19 22 22 24 25 26 24 28 29 19 31 32 33 34 35 34 34 33 32 40 41 42 43 42 4 46 47 48 49 49 48 52 53 53 55 56 56 58 58 60 61 62 60 64 65 65 65 68 68 68 71 64 73 64 75 76 77 77 64 80 81 82 81 81 85 86 85 88 88 90 88 92 92 94 95 96 97 97 99 99 101...

output:

1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 100000...

result:

wrong answer 1st words differ - expected: '1113237056588', found: '1000000000000'