QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#418624#6742. LeavesericmegalovaniaWA 0ms3972kbC++201.7kb2024-05-23 14:51:442024-05-23 14:51:44

Judging History

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

  • [2024-05-23 14:51:44]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3972kb
  • [2024-05-23 14:51:44]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

//#define ONLINE
#ifndef ONLINE
#define debug(...) fprintf(stderr,##__VA_ARGS__)
#else
#define debug(...) ;
#endif

using LL=long long;
using PII=pair<int,int>;

template<typename T>
inline T READ(){
	T x=0; bool f=0; char c=getchar();
	while(!isdigit(c)) f|=(c=='-'),c=getchar();
	while(isdigit(c)) x=x*10+c-'0',c=getchar();
	return f?-x:x;
}
inline int read(){return READ<int>();}
inline LL readLL(){return READ<LL>();}
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

int main(){
	int n=read(),m=read();
	vector<array<int,2>>son(n+1,{0,0});
	vector<int>a(n+1,0);
	for(int i=1;i<=n;i++){
		if(read()==1){
			son[i][0]=read();
			son[i][1]=read();
		}
		else{
			a[i]=read();
		}
	}
	auto dfs=[&](auto self,int u)->vector<vector<int>>{
		if(a[u]){
			return {{a[u]}};
		}
		auto L=self(self,son[u][0]),R=self(self,son[u][1]);
		vector<vector<int>>dp(min( int(L.size()+R.size()+1), m+1 ));
		for(int i=0;i<L.size();i++){
			for(int j=0;i+j<=m && j<R.size();j++){
				auto tmp=L[i];
				tmp.insert(tmp.end(),R[j].begin(),R[j].end());
				if(dp[i+j].empty() || tmp<dp[i+j]){
					dp[i+j]=tmp;
				}
				if(i+j+1>m) continue;
				tmp=R[j];
				tmp.insert(tmp.end(),L[i].begin(),L[i].end());
				if(dp[i+j+1].empty() || tmp<dp[i+j+1]){
					dp[i+j+1]=tmp;
				}
			}
		}
		return dp;
	};
	auto dp=dfs(dfs,1);
	auto ans=dp[m];
	for(int i=m-2;i>=0;i-=2){
		ans=min(ans,dp[i]);
	}
	for(int x:ans) printf("%d ",x);
	return 0;
}

/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 3972kb

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: 3756kb

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: 3736kb

input:

1 0
2 1000000000

output:

1000000000 

result:

ok 1 number(s): "1000000000"

Test #5:

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

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

input:

7 2
1 2 3
1 4 5
1 6 7
2 1
2 2
2 3
2 4

output:

1 2 

result:

wrong answer Answer contains longer sequence [length = 4], but output contains 2 elements