UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#204042#3575. 最优染色drdilyor1002120ms22952kbC++111.3kb2024-04-06 09:11:542024-04-06 12:00:48

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,k;
int w[200005];
vector<int> e[200005];
int dp[200005][2][2];
//父亲有没有选。 自己有没有选.
void dfs(int x,int f){
	dp[x][0][0]=0;
	if(x!=1){dp[x][1][0]=0;}
	else dp[x][1][0]=-(1ll<<60);
	dp[x][0][1]=w[x];
	if(x!=1){dp[x][1][1]=w[x];}
	else dp[x][1][1]=-(1ll<<60);
	//<=k.
	vector<int> p1,p2;
	for(auto v:e[x]){
		if(v==f)continue;
		dfs(v,x);
		dp[x][0][0]+=dp[v][0][0];dp[x][1][0]+=dp[v][0][0];
		dp[x][0][1]+=dp[v][1][0];dp[x][1][1]+=dp[v][1][0];
		p1.push_back(dp[v][0][1]-dp[v][0][0]);p2.push_back(dp[v][1][1]-dp[v][1][0]);
	}
	sort(p1.begin(),p1.end(),greater<int>());
	sort(p2.begin(),p2.end(),greater<int>());
	for(int i=0;i<(int)p1.size();i++){
		if(i==k)break;
		if(p1[i]<0)break;
		dp[x][0][0]+=p1[i];
	}
	for(int i=0;i<(int)p2.size();i++){
		if(i==k)break;
		if(p2[i]<0)break;
		dp[x][0][1]+=p2[i];
	}
	for(int i=0;i<(int)p1.size();i++){
		if(i==k-1)break;
		if(p1[i]<0)break;
		dp[x][1][0]+=p1[i];
	}
	for(int i=0;i<(int)p2.size();i++){
		if(i==k-1)break;
		if(p2[i]<0)break;
		dp[x][1][1]+=p2[i];
	}
}
signed main(){
	cin>>n>>k;
	for(int i=1;i<=n;i++)cin>>w[i];
	for(int i=1;i<n;i++){
		int u,v;
		cin>>u>>v;
		e[u].push_back(v),e[v].push_back(u);
	}
	dfs(1,0);
	cout<<max(dp[1][0][0],dp[1][0][1]);
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 5948kb

input:

20 1
347192840 123415708 104186945 297664991 326084336 697294773 848544612 626247365 363640284 72290...

output:

3759463586

result:

ok single line: '3759463586'

Test #2:

score: 10
Accepted
time: 0ms
memory: 5948kb

input:

20 2
667724589 650804565 563033074 104095033 271899196 319234003 944010406 821350764 594891664 29403...

output:

4805765200

result:

ok single line: '4805765200'

Test #3:

score: 10
Accepted
time: 3ms
memory: 5952kb

input:

20 4
404199446 814571520 417885681 688955458 727208789 945802335 336341722 696509607 211912547 17990...

output:

8445978760

result:

ok single line: '8445978760'

Test #4:

score: 10
Accepted
time: 323ms
memory: 21148kb

input:

200000 1
65131236 428388251 181072273 297507181 684782962 422650616 617467776 878979280 296988619 89...

output:

55251531103968

result:

ok single line: '55251531103968'

Test #5:

score: 10
Accepted
time: 266ms
memory: 22320kb

input:

200000 1
218817207 269055645 308606199 210621051 870398313 587193804 249481820 335706321 802885845 9...

output:

5715753362335

result:

ok single line: '5715753362335'

Test #6:

score: 10
Accepted
time: 300ms
memory: 22788kb

input:

200000 1
755099222 702525613 386076692 40080018 272081660 92143755 266901783 991290625 739690258 459...

output:

19997919119

result:

ok single line: '19997919119'

Test #7:

score: 10
Accepted
time: 270ms
memory: 22952kb

input:

200000 20000
628296669 90702120 493190089 740028325 560292098 840050204 545466526 345062080 67949339...

output:

97039113665030

result:

ok single line: '97039113665030'

Test #8:

score: 10
Accepted
time: 285ms
memory: 21952kb

input:

200000 20
99356922 248658451 234566445 2297720 811608165 197926494 190435104 168271555 149385661 969...

output:

94756737110725

result:

ok single line: '94756737110725'

Test #9:

score: 10
Accepted
time: 404ms
memory: 22868kb

input:

200000 6969
140418096 120749784 620210201 786196327 578435058 541889222 684886743 236586908 85592758...

output:

90693280518745

result:

ok single line: '90693280518745'

Test #10:

score: 10
Accepted
time: 269ms
memory: 22108kb

input:

200000 579
860366334 920293405 203327092 891962204 765687678 452227988 341933733 153845045 249140737...

output:

100183252808433

result:

ok single line: '100183252808433'

Extra Test:

score: 0
Extra Test Passed