QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#571123 | #6430. Monster Hunter | flyfreemrn | TL | 342ms | 118244kb | C++14 | 2.3kb | 2024-09-17 20:38:50 | 2024-09-17 20:38:54 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define MAXN 2010
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;
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 p=0;p<=1;p++){
for(int j=p;j<=siz[now]-1+p;j++){
dp[now][p][j][cnt[now]%2]=10000000000000;
for(int q=0;q<=1;q++){
for(int u=q;u<=min(siz[y]+q-1,j-p);u++){
// cout<<now<<" "<<p<<" "<<j<<" "<<q<<" "<<u<<endl;
dp[now][p][j][cnt[now]%2]=min(dp[now][p][j][cnt[now]%2],dp[y][q][u][cnt[y]%2]+dp[now][p][j-u][(cnt[now]-1)%2]+((q|p)?0:val[y]));
// cout<<now<<" "<<p<<" "<<j<<" "<<q<<" "<<u<<" "<<dp[now][p][j][cnt[now]%2]<<" "<<dp[y][q][u][cnt[y]%2]<<" "<<dp[now][p][j-u][(cnt[now]-1)%2]<<endl;
}
}
// cout<<now<<" "<<p<<" "<<j<<" "<<dp[now][p][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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3900kb
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: 2ms
memory: 5860kb
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: 1ms
memory: 3852kb
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: 16208kb
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: 16384kb
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: 0
Accepted
time: 83ms
memory: 67268kb
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:
1113237056588 1108226059331 1103452926208 1098937464290 1094424834414 1090101107734 1085979381309 1081924835645 1077939941478 1073955392448 1070060014619 1066167943999 1062419631658 1058704177859 1055039647440 1051385794327 1047741820417 1044141128405 1040544084361 1036983950782 1033443541341 102993...
result:
ok 2002 tokens
Test #7:
score: 0
Accepted
time: 152ms
memory: 112796kb
input:
1 2000 1 1 3 4 1 6 7 7 6 10 11 12 12 14 15 11 17 18 18 20 21 22 23 24 22 26 27 28 27 27 31 32 33 34 35 32 37 37 39 39 41 42 43 42 45 10 47 47 49 49 51 49 53 54 55 56 57 57 59 60 61 62 61 64 65 66 67 68 65 70 71 71 73 73 75 75 77 78 75 80 81 82 82 82 85 85 87 88 88 90 88 92 93 92 95 96 97 97 97 100 1...
output:
2218881294575 2213744695667 2209258961365 2204812368487 2200449406724 2196203533720 2192056603090 2187933211280 2183833968547 2179737242326 2175719186934 2171741657318 2167810748823 2163879903171 2159962385119 2156074431587 2152191794989 2148338662389 2144513166298 2140724085981 2136958464736 213322...
result:
ok 2001 tokens
Test #8:
score: 0
Accepted
time: 117ms
memory: 118244kb
input:
1 2000 1 1 3 4 4 4 7 8 9 8 11 12 7 14 15 16 17 17 3 20 21 20 23 24 25 25 27 25 29 29 29 23 33 23 35 35 37 38 39 39 39 42 42 39 45 35 47 47 49 49 51 52 47 54 55 55 57 58 55 60 61 60 60 60 65 66 66 68 69 70 71 69 73 73 75 76 77 76 76 80 81 81 83 83 85 86 87 87 89 90 91 91 93 89 95 80 97 98 99 100 101 ...
output:
2251617273778 2247003039440 2242430225953 2237883166436 2233355730054 2228961190864 2224718903682 2220482730939 2216287724882 2212119854387 2207975346387 2203853324653 2199734356726 2195721269018 2191711933780 2187703738249 2183801657775 2179907800916 2176038498164 2172181973574 2168329662833 216450...
result:
ok 2001 tokens
Test #9:
score: 0
Accepted
time: 114ms
memory: 114968kb
input:
1 2000 1 2 2 1 5 1 1 8 9 9 11 11 13 14 15 16 16 18 16 20 16 22 13 24 25 25 27 28 29 28 31 32 32 28 35 27 37 37 39 40 39 42 43 44 44 46 46 48 49 44 51 52 53 52 55 52 51 58 59 58 61 61 63 63 65 66 67 67 65 70 70 72 73 74 70 76 77 77 79 80 77 65 83 83 85 86 86 88 89 89 91 91 93 93 93 93 97 97 99 100 10...
output:
2206881062084 2202092706920 2197564054211 2193078757615 2188740187606 2184470790404 2180330836648 2176299098130 2172293912044 2168303008199 2164315464017 2160345496700 2156417698598 2152491863648 2148575952119 2144668178439 2140768739007 2136894877679 2133031560312 2129188280081 2125408396292 212163...
result:
ok 2001 tokens
Test #10:
score: 0
Accepted
time: 15ms
memory: 114728kb
input:
1 2000 1 2 3 4 5 6 7 8 9 10 11 10 13 14 14 16 17 16 19 16 13 22 22 13 10 26 27 26 29 26 26 10 33 34 33 9 37 38 39 40 38 42 43 38 45 38 47 47 37 50 51 52 53 54 53 56 53 58 59 52 61 61 63 61 65 52 67 52 69 51 71 50 73 74 74 76 74 78 37 80 81 37 83 83 37 37 37 37 37 9 91 91 93 91 95 9 8 98 99 100 101 1...
output:
2239908981975 2232517414071 2225894512841 2219372844163 2213085150127 2207519039932 2202083361540 2196758986281 2191444012176 2186229941641 2181154413896 2176085010679 2171029257148 2166029667863 2161088940966 2156306922261 2151535754361 2146791775272 2142105755058 2137465141703 2132880168320 212830...
result:
ok 2001 tokens
Test #11:
score: 0
Accepted
time: 32ms
memory: 91740kb
input:
1 2000 1 2 3 4 5 6 7 8 8 7 11 11 13 14 15 16 17 18 19 20 21 22 23 24 25 25 23 28 22 30 31 30 22 34 35 36 37 38 22 22 41 22 43 43 21 20 47 48 49 49 51 51 49 54 48 56 48 58 47 47 20 62 63 63 65 66 67 66 69 66 63 72 73 18 17 16 77 78 79 79 16 82 83 84 84 86 83 82 89 90 91 92 92 94 91 96 89 98 99 100 10...
output:
2187566746517 2182187929991 2176874744181 2172157735355 2167581858172 2163014604257 2158449355514 2153903271574 2149394732089 2144890155305 2140409630299 2135948820472 2131496356723 2127063843406 2122704497177 2118350861665 2114007171456 2109743502902 2105496707054 2101256560940 2097108029529 209296...
result:
ok 2001 tokens
Test #12:
score: 0
Accepted
time: 48ms
memory: 116320kb
input:
1 2000 1 2 3 4 4 6 7 8 9 10 11 12 12 14 15 7 6 18 19 20 21 22 23 24 23 26 27 26 26 30 31 32 33 30 30 36 26 38 39 22 41 41 43 44 45 46 47 48 49 50 50 52 52 47 55 55 45 58 45 44 61 62 61 64 64 64 43 68 69 70 71 68 73 74 74 73 77 78 77 68 68 82 82 22 85 86 87 88 87 90 91 90 93 94 94 94 97 86 99 100 101...
output:
2189791993272 2184503440761 2179558617878 2174909008068 2170291963818 2165729773866 2161186103446 2156743150210 2152327655076 2147979515403 2143644696051 2139346682030 2135121171622 2130925682410 2126769405351 2122695229538 2118629846463 2114651244627 2110684023924 2106730754655 2102782338387 209884...
result:
ok 2001 tokens
Test #13:
score: 0
Accepted
time: 86ms
memory: 90052kb
input:
1 2000 1 2 3 4 5 6 7 8 9 10 10 8 13 14 15 15 17 18 19 20 21 21 23 23 25 25 27 28 29 30 31 31 33 34 35 36 37 37 36 40 41 40 43 43 43 35 47 47 49 50 51 52 53 54 55 53 53 58 59 60 61 61 63 64 63 60 52 49 69 47 71 72 35 74 75 76 77 78 79 79 81 82 83 81 85 85 81 77 89 89 89 92 93 94 94 94 76 98 98 100 10...
output:
2220518092263 2215732319500 2210984793056 2206238533572 2201597531031 2197054470311 2192520546799 2188081300705 2183715317378 2179387838156 2175170120514 2171146207161 2167177667676 2163227064523 2159290434728 2155398316333 2151507487160 2147619095455 2143732109465 2139854894565 2136039327617 213224...
result:
ok 2001 tokens
Test #14:
score: 0
Accepted
time: 0ms
memory: 5824kb
input:
134 19 1 2 2 4 5 6 7 7 6 10 11 12 13 14 11 16 17 18 6 7 6 10 5 7 7 8 3 2 6 2 2 6 8 4 2 4 7 10 1 2 2 4 5 6 7 6 9 7 10 6 8 6 7 3 2 9 7 19 1 2 3 4 5 6 7 7 9 4 11 11 13 14 13 16 17 18 5 5 6 2 10 2 9 3 3 6 5 1 5 8 7 9 1 6 1 18 1 2 3 4 5 6 1 8 9 10 11 12 13 14 15 9 17 1 1 9 10 9 5 4 5 8 3 8 6 6 9 6 1 5 1 ...
output:
198 168 143 123 105 88 73 59 51 43 36 30 24 18 13 9 6 4 2 0 123 89 63 45 33 26 19 13 7 2 0 183 156 132 110 94 79 63 50 38 30 23 18 13 10 7 5 3 2 1 0 193 164 140 116 94 72 60 50 40 31 24 18 12 8 5 3 2 1 0 96 66 44 31 21 15 9 5 3 1 0 102 67 46 34 24 14 9 5 2 1 0 119 93 71 49 33 22 14 6 4 2 1 0 ...
result:
ok 2127 tokens
Test #15:
score: 0
Accepted
time: 1ms
memory: 5872kb
input:
135 19 1 2 1 4 5 5 7 4 9 9 11 12 11 14 15 14 17 18 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 16 1 1 3 3 5 6 7 6 9 10 11 12 13 14 15 1...
output:
37000000000 33000000000 29000000000 25000000000 22000000000 19000000000 16000000000 14000000000 12000000000 10000000000 9000000000 8000000000 7000000000 6000000000 5000000000 4000000000 3000000000 2000000000 1000000000 0 31000000000 27000000000 23000000000 20000000000 17000000000 14000000000 120000...
result:
ok 2134 tokens
Test #16:
score: 0
Accepted
time: 1ms
memory: 5856kb
input:
130 20 1 1 3 4 4 3 7 7 9 10 10 12 13 14 15 16 17 18 19 890245275 826949747 930998021 227159075 875688785 904342722 518026431 347758838 596601609 889934230 867009820 275688682 589383029 811978241 367558119 549938260 840229119 458907694 122945257 608340046 14 1 2 2 4 5 6 7 5 1 10 11 12 13 354616511 16...
output:
24109120725 21186553763 18538360720 16304011063 14164645131 12173130530 10192717221 8867003864 7582719668 6366039576 5207882480 4213524098 3346514278 2519564531 1922962922 1341109971 973551852 625793014 350104332 122945257 0 12079383037 9912001048 7999694706 6143099517 4542912749 3505539149 2626532...
result:
ok 2129 tokens
Test #17:
score: 0
Accepted
time: 342ms
memory: 67572kb
input:
2 1000 1 2 3 2 5 6 7 8 9 10 7 12 12 14 15 16 17 15 19 19 21 14 23 23 25 26 27 28 29 28 26 32 33 34 35 36 37 38 39 40 39 42 43 44 45 44 47 47 49 50 51 49 53 54 55 56 57 58 59 59 55 62 62 64 65 65 67 67 69 70 71 71 73 74 75 76 77 78 77 80 81 82 80 84 84 86 86 88 89 90 91 92 93 94 95 96 92 98 98 100 10...
output:
1087012935694 1083312606896 1079710047856 1076167857600 1072688415661 1069235844035 1065792043214 1062374798390 1058993816387 1055632934263 1052322356363 1049033592858 1045781646900 1042563691152 1039353881011 1036159077609 1032965722192 1029776323322 1026611904852 1023472572635 1020360346684 101730...
result:
ok 2002 tokens
Test #18:
score: -100
Time Limit Exceeded
input:
1 2000 1 2 3 4 2 6 6 8 9 10 10 12 13 9 15 16 16 18 19 20 21 21 23 19 25 26 27 28 28 30 31 32 33 34 35 34 31 38 38 40 41 42 40 44 44 46 46 48 49 50 50 52 53 54 55 56 53 58 59 60 61 62 59 64 65 66 64 68 69 70 71 72 73 74 75 76 76 78 79 80 81 82 83 83 85 86 87 88 89 90 88 92 92 94 81 96 97 96 99 100 10...