QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#166967 | #6742. Leaves | PhantomThreshold# | AC ✓ | 306ms | 7700kb | C++20 | 2.9kb | 2023-09-06 21:51:31 | 2023-09-06 21:51:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxn=1000;
int n,m;
int a[maxn+5];
int g[maxn+5][2];
int sz[maxn+5];
int nonleaf[maxn+5];
pair<int,int> dp[maxn+5][maxn/2+5];
int top=0;
int arr[maxn+5];
void getarray(int u,int t){
// cerr << u << " " << t << " " << top << endl;
if (sz[u]==0){
arr[++top]=a[u];
return;
}
int flip=dp[u][t].second;
int v=g[u][0];
int w=g[u][1];
int vt=dp[u][t].first;
int wt=t-vt-flip;
if (!flip){
getarray(v,vt);
getarray(w,wt);
}
else{
getarray(w,wt);
getarray(v,vt);
}
}
int nowtop=0;
int nowarr[maxn+5];
void dfs(int u){
nonleaf[u]=1;
if (sz[u]==0){
nonleaf[u]=0;
dp[u][0]=make_pair(0,0);
return;
}
int v=g[u][0];
int w=g[u][1];
dfs(v);
dfs(w);
nonleaf[u]+=nonleaf[v];
nonleaf[u]+=nonleaf[w];
for (int t=0;t<=nonleaf[u] && t<=m;t++){
for (int flip=0;flip<=1;flip++){
for (int vt=0;vt<=nonleaf[v] && vt<=t-flip;vt++){
int wt=t-flip-vt;
if (wt>nonleaf[w]) continue;
if (dp[v][vt].first==-1) continue;
if (dp[w][wt].first==-1) continue;
if (!flip){
top=0;
getarray(v,vt);
getarray(w,wt);
}
else{
top=0;
getarray(w,wt);
getarray(v,vt);
}
if (dp[u][t].first==-1){
dp[u][t]=make_pair(vt,flip);
nowtop=top;
for (int i=1;i<=top;i++) nowarr[i]=arr[i];
}
else{
bool flag=0;
for (int i=1;i<=top;i++){
if (arr[i]==nowarr[i]) continue;
if (arr[i]<nowarr[i]) flag=1;
break;
}
if (flag){
dp[u][t]=make_pair(vt,flip);
nowtop=top;
for (int i=1;i<=top;i++) nowarr[i]=arr[i];
}
}
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i=1;i<=n;i++){
int opt=0;
cin >> opt;
if (opt==1){
int x,y;
cin >> x >> y;
sz[i]=2;
g[i][0]=x;
g[i][1]=y;
}
else{
cin >> a[i];
sz[i]=0;
}
}
/*
cerr << "---" << endl;
for (int i=1;i<=n;i++) cerr << sz[i] << " ";
cerr << endl;
for (int i=1;i<=n;i++) cerr << g[i][0] << " " << g[i][1] << endl;
for (int i=1;i<=n;i++) cerr << a[i] << " ";
cerr << endl;
cerr << "---" << endl;
*/
for (int i=1;i<=n;i++)
for (int j=0;j<=m;j++) dp[i][j]=make_pair(-1,-1);
dfs(1);
/*
for (int i=1;i<=n;i++){
for (int j=0;j<=2;j++){
cerr << "(" << dp[i][j].first << "," << dp[i][j].second << ") ";
}
cerr << endl;
}
*/
nowtop=n;
for (int i=1;i<=nowtop;i++) nowarr[i]=2e9;
for (int t=m;t>=0;t-=2){
if (dp[1][t].first==-1) continue;
// cerr << "-------------" << endl;
top=0;
getarray(1,t);
bool flag=0;
for (int i=1;i<=top;i++){
if (arr[i]==nowarr[i]) continue;
if (arr[i]<nowarr[i]) flag=1;
break;
}
if (flag){
nowtop=top;
for (int i=1;i<=top;i++) nowarr[i]=arr[i];
}
}
for (int i=1;i<=nowtop;i++) cout << nowarr[i] << " \n"[i==nowtop];
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3692kb
input:
3 0 1 2 3 2 1 2 2
output:
1 2
result:
ok 2 number(s): "1 2"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3692kb
input:
7 1 1 2 3 1 4 5 1 6 7 2 4 2 2 2 3 2 1
output:
2 4 3 1
result:
ok 4 number(s): "2 4 3 1"
Test #3:
score: 0
Accepted
time: 1ms
memory: 3692kb
input:
7 2 1 2 3 1 4 5 1 6 7 2 4 2 2 2 3 2 1
output:
1 3 4 2
result:
ok 4 number(s): "1 3 4 2"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3684kb
input:
1 0 2 1000000000
output:
1000000000
result:
ok 1 number(s): "1000000000"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3680kb
input:
3 1 1 2 3 2 1 2 2
output:
2 1
result:
ok 2 number(s): "2 1"
Test #6:
score: 0
Accepted
time: 1ms
memory: 3720kb
input:
7 2 1 2 3 1 4 5 1 6 7 2 1 2 2 2 3 2 4
output:
1 2 3 4
result:
ok 4 number(s): "1 2 3 4"
Test #7:
score: 0
Accepted
time: 303ms
memory: 7700kb
input:
999 480 1 3 2 1 4 5 1 6 7 1 9 8 1 10 11 1 13 12 1 14 15 1 16 17 1 19 18 1 21 20 1 23 22 1 25 24 1 27 26 1 28 29 1 30 31 1 33 32 1 35 34 1 37 36 1 38 39 1 41 40 1 42 43 1 45 44 1 46 47 1 48 49 1 51 50 1 52 53 1 55 54 1 56 57 1 58 59 1 61 60 1 62 63 1 64 65 1 67 66 1 69 68 1 71 70 1 73 72 1 74 75 1 76...
output:
34826804 763875883 763875883 763875883 763875883 763875883 763875883 763875883 248820103 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 763875883 7...
result:
ok 500 numbers
Test #8:
score: 0
Accepted
time: 306ms
memory: 7640kb
input:
999 480 1 2 3 1 4 5 1 7 6 1 8 9 1 10 11 1 13 12 1 15 14 1 17 16 1 19 18 1 21 20 1 23 22 1 24 25 1 26 27 1 28 29 1 31 30 1 32 33 1 34 35 1 37 36 1 38 39 1 41 40 1 42 43 1 44 45 1 46 47 1 48 49 1 51 50 1 52 53 1 54 55 1 57 56 1 58 59 1 60 61 1 62 63 1 65 64 1 66 67 1 69 68 1 71 70 1 72 73 1 75 74 1 77...
output:
530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 530024839 ...
result:
ok 500 numbers
Test #9:
score: 0
Accepted
time: 283ms
memory: 7644kb
input:
999 480 1 3 2 1 5 4 1 7 6 1 8 9 1 10 11 1 13 12 1 15 14 1 17 16 1 18 19 1 21 20 1 23 22 1 25 24 1 26 27 1 29 28 1 31 30 1 33 32 1 34 35 1 37 36 1 38 39 1 41 40 1 43 42 1 45 44 1 47 46 1 49 48 1 50 51 1 52 53 1 54 55 1 56 57 1 59 58 1 60 61 1 62 63 1 65 64 1 67 66 1 69 68 1 71 70 1 73 72 1 74 75 1 76...
output:
507922 928353918 51892154 809031414 296508656 801553730 385852006 930053625 95913518 431154803 117002192 187199343 405980569 664093190 478863675 930428963 3440725 490128679 92302056 563992614 27927487 246777697 168388113 922508131 52386784 780998040 438167217 876407248 158186938 633166175 788275543 ...
result:
ok 500 numbers
Test #10:
score: 0
Accepted
time: 289ms
memory: 7652kb
input:
999 480 1 3 2 1 4 5 1 7 6 1 8 9 1 10 11 1 13 12 1 15 14 1 16 17 1 19 18 1 21 20 1 23 22 1 24 25 1 26 27 1 29 28 1 30 31 1 32 33 1 35 34 1 37 36 1 38 39 1 41 40 1 43 42 1 45 44 1 46 47 1 48 49 1 50 51 1 52 53 1 54 55 1 56 57 1 58 59 1 60 61 1 63 62 1 65 64 1 66 67 1 68 69 1 70 71 1 72 73 1 74 75 1 76...
output:
666292 26210483 65023030 95745223 11431606 30237589 40996997 59830702 14512182 24640163 39361368 85684143 17020177 58437715 74457645 91998540 7557034 46923480 16066480 33921131 14512182 83949838 22501176 83949838 15127734 18414338 52979072 81930757 24640163 70913581 72929459 78973167 3687875 4610266...
result:
ok 500 numbers
Test #11:
score: 0
Accepted
time: 295ms
memory: 7696kb
input:
999 480 1 3 2 1 4 5 1 7 6 1 9 8 1 10 11 1 13 12 1 15 14 1 16 17 1 18 19 1 20 21 1 23 22 1 24 25 1 27 26 1 29 28 1 30 31 1 33 32 1 34 35 1 37 36 1 39 38 1 40 41 1 42 43 1 44 45 1 47 46 1 48 49 1 50 51 1 52 53 1 55 54 1 56 57 1 59 58 1 60 61 1 62 63 1 64 65 1 66 67 1 68 69 1 70 71 1 72 73 1 75 74 1 77...
output:
874376 19021636 10912872 49114704 21454471 94310772 60086613 88323254 20181141 62658098 54568372 64872279 21454471 30659919 73483837 98385133 12300384 74080822 54568372 100102768 29770805 62658098 33159023 62908008 33159023 98943188 52580720 64872279 58787422 98644936 73687255 98385133 10912872 9838...
result:
ok 500 numbers
Test #12:
score: 0
Accepted
time: 64ms
memory: 7340kb
input:
905 52 1 3 2 2 25780328 1 4 5 2 84996992 1 6 7 2 43060692 1 8 9 2 58356233 1 11 10 2 48172306 1 12 13 2 94654986 1 14 15 2 66085196 1 17 16 2 43141873 1 18 19 2 58356233 1 21 20 2 58356233 1 22 23 2 64124892 1 24 25 2 55464563 1 27 26 2 46324683 1 29 28 2 78919648 1 30 31 2 13379324 1 32 33 2 960564...
output:
612429 612429 11385745 15524596 27354220 30826255 44403070 95193352 30760083 84996992 87443977 43060692 49958425 13964121 91042474 36461317 15704020 17411083 64017133 36129702 78978485 64017133 17411083 36057170 100691869 58356233 19600883 97205832 56442997 15704020 11168414 91042474 36057170 789196...
result:
ok 453 numbers
Test #13:
score: 0
Accepted
time: 11ms
memory: 7464kb
input:
973 29 1 15 2 1 12 3 1 9 4 1 5 8 1 7 6 2 962417250 2 863548670 2 800966093 1 10 11 2 37461993 2 868171711 1 13 14 2 964174105 2 677204824 1 16 423 1 310 17 1 18 191 1 19 62 1 29 20 1 21 28 1 27 22 1 23 26 1 25 24 2 365120660 2 231292756 2 592806005 2 874105957 2 781723044 1 45 30 1 36 31 1 32 35 1 3...
output:
2675037 261767176 248501239 345976036 622219661 737137787 956443156 230208508 491670229 754288296 340857675 508137398 362818049 409851160 870728354 794405031 484178348 431303115 22375372 34846342 687160982 196499503 730262600 485680065 739755993 161591308 468293073 459956110 140991714 726201872 1492...
result:
ok 487 numbers
Test #14:
score: 0
Accepted
time: 99ms
memory: 7432kb
input:
951 148 1 677 2 1 62 3 1 47 4 1 32 5 1 31 6 1 7 12 1 11 8 1 10 9 2 151747858 2 8073627 2 884973114 1 13 22 1 14 21 1 15 20 1 17 16 2 139411516 1 18 19 2 449838238 2 5170971 2 627498389 2 868252445 1 24 23 2 317075494 1 28 25 1 26 27 2 490832736 2 714844981 1 29 30 2 771243846 2 315749038 2 957329344...
output:
659497 184554417 587672201 274768649 699968317 178766364 543809986 530978880 536461261 557219868 652279850 674934319 661534083 907282244 620212281 541213699 822252327 959188634 6937845 289752424 737160308 266639809 130898365 408815070 773575808 875783850 62361039 515918403 104430174 430469282 463808...
result:
ok 476 numbers