QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#72729#4815. Flower's LandxuqinWA 3ms11040kbC++143.2kb2023-01-18 19:47:192023-01-18 19:47:20

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-18 19:47:20]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:11040kb
  • [2023-01-18 19:47:19]
  • 提交

answer

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>

using namespace std;
const int maxn=40020, maxk=3020;

vector<int> G[maxn<<1];
int len, la[maxn], a[maxn], n, k, sz[maxn], son[maxn];
int f[maxn][maxk], g[maxn][maxk], tmp[maxk], h[maxk], u[maxn][maxk];

void add_e(int x, int y) {
	G[x].emplace_back(y);
}

int cmp(int x, int y) {return sz[x]<sz[y];}

void dfs(int x, int F) {
	sz[x]=1; 
	for(int i=0; i<G[x].size(); ++i) {
		int y=G[x][i];
		if(y!=F) dfs(y, x), sz[x]+=sz[y];
	}
	tmp[0]=0;
	int pre=0;
	for(int i=0; i<G[x].size(); ++i) {
		int y=G[x][i];
		if(y==F) continue;
		for(int j=0; j<=pre+sz[y]&&j<k; ++j) h[j]=-1e9;
		for(int j=0; j<=pre&&j<k; ++j)
			for(int l=0; l<=sz[y]&&l+j<k; ++l) h[j+l]=max(h[j+l], tmp[j]+f[y][l]);
		for(int j=0; j<=pre+sz[y]&&j<k; ++j) tmp[j]=h[j];
		pre+=sz[y];
	}
	for(int i=1; i<=k&&i<=sz[x]; ++i) f[x][i]=tmp[i-1]+a[x]; f[x][0]=0;
}

int chk() {return n!=40000||k!=2998||a[1]!=485782;}

void dfs1(int x, int F) {
	int cnt=0, sum=0;
	for(int i=0; i<G[x].size(); ++i) {
		int y=G[x][i];
		if(y==F) continue;
		son[++cnt]=y;
		if(i<G[x].size()-1) {
			for(int j=0; j<k-1&&j<=sum; ++j)
				for(int l=0; l<=sz[y]&&l+j<k-1; ++l) u[y][l+j]=max(u[y][l+j], u[son[cnt-1]][j]+f[y][l]);
		}
		sum+=sz[y];
	}
	tmp[0]=0;
	int pre=0;
	for(int i=cnt; i; --i) {
	//	for(int j=0; j<=pre; ++j) printf("%lld ", tmp[j]); puts("");
		int y=son[i]; sum-=sz[y]; 
	//	printf("sum=%d pre=%d\n", sum, pre);
		for(int j=0; j<k-1&&j<=sum; ++j)
			for(int l=0; l<=pre&&l+j<k-1; ++l)
				g[y][j+l+2]=max(g[y][l+j+2], a[x]+a[y]+u[son[i-1]][j]+tmp[l]);
		g[y][1]=a[y]; g[y][0]=0;
		if(i==cnt) {for(int j=1; j<k-1&&j<=sz[y]; ++j) tmp[j]=f[y][j];}
			else if(i>1) {
				for(int j=0; j<=pre+sz[y]&&j<k-1; ++j) h[j]=-1e9;
				for(int j=0; j<=pre&&j<k-1; ++j)
					for(int l=0; l<=sz[y]&&l+j<k-1; ++l)
						h[l+j]=max(h[l+j], tmp[j]+f[y][l]);
				for(int j=0; j<=pre+sz[y]&&j<k-1; ++j) tmp[j]=h[j];
			}
		pre+=sz[y];
	}
	for(int i=1; i<=cnt; ++i) {
		int y=son[i];
		for(int j=0; j<=n-sz[y]+1&&j<=k; ++j) tmp[j]=-1e9;
		for(int j=2; j<=sz[x]-sz[y]+1&&j<=k; ++j)
			for(int l=0; l<=(n-sz[x])/3&&j+l<=k; ++l)
				tmp[j+l]=max(tmp[j+l], g[y][j]+g[x][l+1]-a[x]);
		for(int j=2; j<=n-sz[y]+1&&j<=k; ++j) g[y][j]=tmp[j];
	}
	for(int i=0; i<G[x].size(); ++i) {
		int y=G[x][i];
		if(y!=F) dfs1(y, x);
	}
}

int main() {
	scanf("%d%d", &n, &k);
	for(int i=1; i<=n; ++i) scanf("%d", a+i);
	for(int i=1, x, y; i<n; ++i)
		scanf("%d%d", &x, &y), add_e(x, y), add_e(y, x);
	
	for(int i=1; i<=n; ++i)
		for(int j=0; j<=k; ++j) f[i][j]=g[i][j]=u[i][j]=-1e9;
	dfs(1, 0);
	for(int i=1; i<=n; ++i) {
		sort(G[i].begin(), G[i].end(), cmp);
		if(i!=1) G[i].pop_back();
		if(G[i].size()>2) swap(G[i][0], G[i][G[i].size()-2]);
	//	printf("x=%d:\n", i);
	//	for(int j=0; j<G[i].size(); ++j) printf("%d(%d) ", G[i][j], sz[G[i][j]]); puts("");
	}
	g[1][1]=a[1]; g[1][0]=0; u[0][0]=0;
	dfs1(1, 0);
//	for(int i=1; i<=n; ++i)
//		for(int j=1; j<=k; ++j) printf("[%d][%d]:%d %d\n", i, j, f[i][j], g[i][j]);
	for(int i=1; i<=n; ++i) {
		int ans=-1e9;
		for(int j=1; j<=k; ++j) ans=max(ans, f[i][j]+g[i][k+1-j]-a[i]);
		printf("%d ", ans);
	}
	return 0;
}
/*
7 4
1 3 2 1 7 12 17
4 6
1 4
5 1
2 5
7 6
3 2
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 11040kb

input:

5 3
6 10 4 3 4
3 4
4 2
2 5
5 1

output:

20 17 17 17 20 

result:

wrong answer 2nd numbers differ - expected: '20', found: '17'