QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#396969 | #6742. Leaves | skulker | WA | 3ms | 34512kb | C++14 | 3.3kb | 2024-04-23 15:02:42 | 2024-04-23 15:02:42 |
Judging History
answer
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
int n,m;
string dp[1100][510];
vector<int> len[1100][510];
int lf[110];
int siz[1100];
vector<int> nex[1100];
string val[1100];
void dfs(int now){
siz[now]=1;
lf[now]=0;
if(nex[now].size()==0){
lf[now]=1;
dp[now][0]=val[now];
len[now][0].push_back(val[now].size());
return ;
}
else {
for(int i=0;i<nex[now].size();i++){
dfs(nex[now][i]);
siz[now]=siz[now]+siz[nex[now][i]];
lf[now]=lf[now]+lf[nex[now][i]];
}
}
int l=nex[now][0],r=nex[now][1];
for(int i=0;i<=(siz[l]-1)/2;i++){
for(int j=0;j<=(siz[r]-1)/2&&i+j<=m;j++){
string s2=dp[l][i]+dp[r][j];
if(dp[now][i+j].size()==0){
dp[now][i+j]=s2;
for(int k=0;k<len[l][i].size();k++){
len[now][i+j].push_back(len[l][i][k]);
}
for(int k=0;k<len[r][j].size();k++){
len[now][i+j].push_back(len[r][j][k]);
}
}
else {
if(dp[now][i+j]>s2) {
dp[now][i+j]=s2;
for(int k=0;k<len[l][i].size();k++){
len[now][i+j].push_back(len[l][i][k]);
}
for(int k=0;k<len[r][j].size();k++){
len[now][i+j].push_back(len[r][j][k]);
}
}
}
}
}
l=nex[now][1],r=nex[now][0];
for(int i=0;i<=(siz[l]-1)/2;i++){
for(int j=0;j<=(siz[r]-1)/2&&i+j+1<=m;j++){
string s2=dp[l][i]+dp[r][j];
if(dp[now][i+j+1].size()==0){
dp[now][i+j+1]=s2;
for(int k=0;k<len[l][i].size();k++){
len[now][i+j+1].push_back(len[l][i][k]);
}
for(int k=0;k<len[r][j].size();k++){
len[now][i+j+1].push_back(len[r][j][k]);
}
}
else {
if(dp[now][i+j+1]>s2) {
dp[now][i+j+1]=s2;
for(int k=0;k<len[l][i].size();k++){
len[now][i+j+1].push_back(len[l][i][k]);
}
for(int k=0;k<len[r][j].size();k++){
len[now][i+j+1].push_back(len[r][j][k]);
}
}
}
// cout<<"swp l r i="<<i<<" j="<<j<<" dp[now][i+j+1]="<<dp[now][i+j+1]<<'\n';
}
}
}
int main(){
scanf("%d%d",&n,&m);
int u,v,w;
int opt;
for(int i=1;i<=n;i++){
scanf("%d",&opt);
if(opt==1){
scanf("%d%d",&u,&v);
nex[i].push_back(u);
nex[i].push_back(v);
}
else {
cin>>val[i];
}
}
dfs(1);
string ans=dp[1][0];
int pos=0;
for(int i=1;i<=m;i++) {
if(dp[1][i]<ans) {
ans=dp[1][i];
pos=i;
}
}
int k=len[1][pos][0];
int cnt=0;
for(int i=0;i<ans.size();i++){
cout<<ans[i];
k--;
if(k==0){
cout<<" ";
cnt++;
k=len[1][pos][cnt];
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 34436kb
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: 34432kb
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: 34456kb
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: 0ms
memory: 34512kb
input:
1 0 2 1000000000
output:
1000000000
result:
ok 1 number(s): "1000000000"
Test #5:
score: -100
Wrong Answer
time: 3ms
memory: 34508kb
input:
3 1 1 2 3 2 1 2 2
output:
1 2
result:
wrong answer 1st numbers differ - expected: '2', found: '1'