QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#74151#4815. Flower's LandReanapWA 3ms4528kbC++142.6kb2023-01-30 22:00:572023-01-30 22:01:00

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-30 22:01:00]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:4528kb
  • [2023-01-30 22:00:57]
  • 提交

answer

#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <iostream>
#include <algorithm>
#define pii pair <int , int>
#define pll pair <LL , LL>
#define mp make_pair
#define fs first
#define sc second
using namespace std;
typedef long long LL;

//const int Mxdt=100000;
//static char buf[Mxdt],*p1=buf,*p2=buf;
//#define getchar() p1==p2&&(p2=(p1=buf)+fread(buf,1,Mxdt,stdin),p1==p2)?EOF:*p1++;


template <typename T>
void read(T &x) {
	T f=1;x=0;char s=getchar();
	while(s<'0'||s>'9') {if(s=='-') f=-1;s=getchar();}
	while(s>='0'&&s<='9') {x=(x<<3)+(x<<1)+(s^'0');s=getchar();}
	x *= f;
}

template <typename T>
void write(T x , char s='\n') {
	if(!x) {putchar('0');putchar(s);return;}
	if(x<0) {putchar('-');x=-x;}
	T t = 0 , tmp[25] = {};
	while(x) tmp[t ++] = x % 10 , x /= 10;
	while(t -- > 0) putchar(tmp[t] + '0');
	putchar(s);
}

const int MAXN = 4e4 + 5;
const int MAXK = 3e3 + 5;

int f[MAXN][MAXK] , g[MAXN][MAXK] , a[MAXN] , siz[MAXN] , n , k , tmp[MAXK];
vector <int> G[MAXN];
int Ans[MAXN];

void dfs1(int x , int fa) {
	siz[x] = 1;
	f[x][1] = a[x];
	for (auto v:G[x]) {
		if(v == fa) continue;
		dfs1(v , x);
		for (int i = 1; i <= min(k , siz[x] + siz[v]); ++i) tmp[i] = 0;
		for (int i = 1; i <= min(k , siz[x]); ++i) g[v][i] = f[x][i];
		for (int i = 1; i <= min(k , siz[x]); ++i) for (int j = 0; j <= min(k , siz[v]); ++j) tmp[i + j] = max(tmp[i + j] , f[x][i] + f[v][j]);
		siz[x] += siz[v];
		for (int i = 1; i <= min(k , siz[x]); ++i) f[x][i] = tmp[i];  
	}
}

void dfs2(int x , int fa) {
	for (int i = 1; i <= min(siz[x] , k); ++i) Ans[x] = max(g[x][i] + f[x][i] , Ans[x]);
	reverse(G[x].begin() , G[x].end());
	int s = siz[x];
	for (auto v:G[x]) {
		if(v == fa) continue;
		s -= siz[v];
		for (int i = 1; i <= min(k , siz[v]); ++i) tmp[i] = 0;
		for (int i = 1; i <= min(k , siz[v]); ++i) for (int j = 1; j <= min(k , s) && i + j <= k; ++j) tmp[i] = max(tmp[i] , g[v][j] + g[x][i + j]);
		for (int i = 1; i <= min(k , siz[v]); ++i) g[v][i] = tmp[i];
		dfs2(v , x);
		
		for (int j = 1; j <= min(k , s); ++j) tmp[j] = 0;
		for (int j = 1; j <= min(k , s); ++j) for (int i = 1; i <= min(k , siz[v]) && i + j <= k; ++i) tmp[j] = max(tmp[j] , f[v][i] + g[x][j + i]);
		for (int j = 1; j <= min(k , s); ++j) g[x][j] = tmp[j];
	}
}

int main() {
	read(n),read(k);
	for (int i = 1; i <= n; ++i) read(a[i]);
	for (int i = 1; i < n; ++i) {
		int u , v;
		read(u),read(v);
		G[u].push_back(v) , G[v].push_back(u);
	}
	dfs1(1 , 0);
	dfs2(1 , 0);
	for (int i = 1; i <= n; ++i) write(Ans[i] , ' ');
	puts("");
	return 0;
}

詳細信息

Test #1:

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

input:

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

output:

20 20 17 17 20 

result:

ok 5 number(s): "20 20 17 17 20"

Test #2:

score: 0
Accepted
time: 3ms
memory: 4496kb

input:

7 4
1 3 2 1 7 12 17
4 6
1 4
5 1
2 5
7 6
3 2

output:

31 13 13 31 21 31 31 

result:

ok 7 numbers

Test #3:

score: 0
Accepted
time: 3ms
memory: 4444kb

input:

1 1
20

output:

20 

result:

ok 1 number(s): "20"

Test #4:

score: 0
Accepted
time: 3ms
memory: 4528kb

input:

10 3
19 7 25 18 93 97 21 51 60 80
6 7
7 1
1 9
9 10
10 2
2 5
5 3
3 8
8 4

output:

159 180 169 94 180 137 137 169 159 180 

result:

ok 10 numbers

Test #5:

score: -100
Wrong Answer
time: 3ms
memory: 4456kb

input:

20 3
932 609 248 720 831 253 418 482 1000 542 436 304 217 163 872 380 704 845 497 610
17 12
1 17
15 17
13 17
2 15
16 2
18 16
8 16
4 16
19 4
6 4
20 19
10 19
9 10
5 10
7 9
3 9
14 5
11 7

output:

2508 2185 1790 1945 2373 1470 1854 1707 2373 2373 1854 1880 1853 1536 2185 1945 2508 1707 2039 1649 

result:

wrong answer 7th numbers differ - expected: '1960', found: '1854'