QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#402799 | #6742. Leaves | altaires1 | WA | 4ms | 32216kb | C++14 | 2.4kb | 2024-05-01 15:13:36 | 2024-05-01 15:13:37 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int maxn=1100;
int n,m;
int into[maxn];
int arrayA[maxn];
vector<int> dp[maxn][maxn];
vector<int> to[maxn];
int siz[maxn];
void dfs1(int a){
if(to[a].empty()){
siz[a]=0;
return;
}
siz[a]=1;
for(int it : to[a]){
dfs1(it);
siz[a]+=siz[it];
}
return;
}
void dfs(int a){
if(to[a].empty()){
dp[a][0].push_back(arrayA[a]);
return;
}
int l=to[a][0];
int r=to[a][1];
dfs(l);
dfs(r);
for(int i=0;i<=siz[l];i++){
for(int j=0;j<=siz[r];j++){
//先左后右,表示没有交换
vector<int> temp;
int len=0;
for(int it : dp[l][i]){
temp.push_back(it);
len++;
}
for(int it : dp[r][j]){
temp.push_back(it);
len++;
}
if(dp[a][i+j].empty()){
for(int it : temp){
dp[a][i+j].push_back(it);
}
}
else{
int judge=0;
for(int k=0;k<len;k++){
if(dp[a][i+j][k]<temp[k]){
judge=-1;
break;
}
else if(dp[a][i+j][k]>temp[k]){
judge=1;
break;
}
}
if(judge==1){
dp[a][i+j].clear();
for(int it : temp){
dp[a][i+j].push_back(it);
}
}
}
//先右后左,表示交换
if(i+j+1>siz[a]){
continue;
}
temp.clear();
len=0;
for(int it : dp[r][j]){
temp.push_back(it);
len++;
}
for(int it : dp[l][i]){
temp.push_back(it);
len++;
}
if(dp[a][i+j+1].empty()){
for(int it : temp){
dp[a][i+j+1].push_back(it);
}
}
else{
int judge=0;
for(int k=0;k<len;k++){
if(dp[a][i+j+1][k]<temp[k]){
judge=-1;
break;
}
else if(dp[a][i+j+1][k]>temp[k]){
judge=1;
break;
}
}
if(judge==1){
dp[a][i+j+1].clear();
for(int it : temp){
dp[a][i+j+1].push_back(it);
}
}
}
}
}
return;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
int type;
cin>>type;
if(type==1){
int l,r;
cin>>l>>r;
into[l]++;
into[r]++;
to[i].push_back(l);
to[i].push_back(r);
}
else{
cin>>arrayA[i];
}
}
int root=0;
for(int i=1;i<=n;i++){
if(into[i]==0){
root=i;
break;
}
}
dfs1(root);
dfs(root);
for(int it : dp[root][m]){
cout<<it<<' ';
}
cout<<'\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 31884kb
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: 3ms
memory: 32040kb
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: 0ms
memory: 31956kb
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: 3ms
memory: 32216kb
input:
1 0 2 1000000000
output:
1000000000
result:
ok 1 number(s): "1000000000"
Test #5:
score: 0
Accepted
time: 3ms
memory: 31940kb
input:
3 1 1 2 3 2 1 2 2
output:
2 1
result:
ok 2 number(s): "2 1"
Test #6:
score: -100
Wrong Answer
time: 4ms
memory: 32008kb
input:
7 2 1 2 3 1 4 5 1 6 7 2 1 2 2 2 3 2 4
output:
2 1 4 3
result:
wrong answer 1st numbers differ - expected: '1', found: '2'