QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#396986#6742. LeavesskulkerTL 7ms21468kbC++203.3kb2024-04-23 15:15:192024-04-23 15:15:20

Judging History

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

  • [2024-04-23 15:15:20]
  • 评测
  • 测评结果:TL
  • 用时:7ms
  • 内存:21468kb
  • [2024-04-23 15:15:19]
  • 提交

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]);
                    }
                }
            }
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    int u,v,w;
    int opt;
    for(int i=1;i<=n;i++){
        cin>>opt;
        if(opt==1){
            cin>>u>>v;
            nex[i].push_back(u);
            nex[i].push_back(v);
        }
        else {
            cin>>val[i];
        }
    }
    dfs(1);
    string ans=dp[1][m];
    int pos=m;
    for(int i=m-2;i>=0;i-=2){
        if(ans>dp[1][i]) {
            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];
        }
    }
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 21104kb

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: 0ms
memory: 21176kb

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: 4ms
memory: 21400kb

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: 7ms
memory: 21156kb

input:

1 0
2 1000000000

output:

1000000000 

result:

ok 1 number(s): "1000000000"

Test #5:

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

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: 0ms
memory: 21236kb

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: -100
Time Limit Exceeded

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:


result: