UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#196863#2663. 旅游yyh_yyds100954ms2516kbC++11930b2023-11-02 10:31:292023-11-02 12:04:51

answer

#include<bits/stdc++.h>
using namespace std;
const int N=610;
int n,v;
int h[N],e[N*2],ne[N*2],idx;
int dp[N][N][2];
void build(int xx,int yy)
{
	e[idx]=yy;
	ne[idx]=h[xx];
	h[xx]=idx++;
}
void dfs1(int u,int fa)
{
	dp[u][0][0]=dp[u][0][1]=1;
	for(int i=h[u];~i;i=ne[i])
	{
		int j=e[i];
		if(j==fa) continue ;
		dfs1(j,u);
		for(int p=v;p>=1;p--)
			for(int t=0;t<=p;t++)
			{
				if(p-t>=2){
					dp[u][p][0]=max(dp[u][p][0],dp[u][p-t-2][0]+dp[j][t][1]);
					dp[u][p][1]=max(dp[u][p][1],dp[u][p-t-2][1]+dp[j][t][1]);
				}
				dp[u][p][0]=max(dp[u][p][0],dp[u][p-t-1][1]+dp[j][t][0]);
			}
	}
}
int main()
{
	scanf("%d %d",&n,&v);
	memset(h,-1,sizeof(h));
	for(int i=1;i<n;i++)
	{
		int xx,yy;
		scanf("%d %d",&xx,&yy);
		build(xx,yy);
		build(yy,xx);
	}
	dfs1(1,0);
	int ans=0;
	for(int i=0;i<=v;i++) ans=max(ans,max(dp[1][i][0],dp[1][i][1]));
	printf("%d\n",ans);
	return 0;
}

详细

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

Test #1:

score: 5
Accepted
time: 14ms
memory: 2404kb

input:

273 176
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
...

output:

158

result:

ok single line: '158'

Test #2:

score: 5
Accepted
time: 26ms
memory: 2436kb

input:

280 245
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
...

output:

194

result:

ok single line: '194'

Test #3:

score: 5
Accepted
time: 36ms
memory: 2460kb

input:

287 307
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
...

output:

227

result:

ok single line: '227'

Test #4:

score: 5
Accepted
time: 42ms
memory: 2496kb

input:

294 322
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
...

output:

236

result:

ok single line: '236'

Test #5:

score: 5
Accepted
time: 4ms
memory: 2384kb

input:

271 83
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1...

output:

84

result:

ok single line: '84'

Test #6:

score: 5
Accepted
time: 5ms
memory: 2428kb

input:

278 124
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
...

output:

125

result:

ok single line: '125'

Test #7:

score: 5
Accepted
time: 51ms
memory: 2452kb

input:

285 350
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
...

output:

248

result:

ok single line: '248'

Test #8:

score: 5
Accepted
time: 196ms
memory: 2488kb

input:

292 544
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
...

output:

292

result:

ok single line: '292'

Test #9:

score: 5
Accepted
time: 144ms
memory: 2516kb

input:

299 437
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
...

output:

295

result:

ok single line: '295'

Test #10:

score: 5
Accepted
time: 112ms
memory: 2416kb

input:

276 531
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
...

output:

276

result:

ok single line: '276'

Test #11:

score: 5
Accepted
time: 1ms
memory: 2360kb

input:

283 18
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1...

output:

19

result:

ok single line: '19'

Test #12:

score: 5
Accepted
time: 56ms
memory: 2476kb

input:

290 363
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
...

output:

256

result:

ok single line: '256'

Test #13:

score: 5
Accepted
time: 8ms
memory: 2508kb

input:

297 127
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
...

output:

128

result:

ok single line: '128'

Test #14:

score: 5
Accepted
time: 0ms
memory: 2356kb

input:

274 49
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1...

output:

50

result:

ok single line: '50'

Test #15:

score: 5
Accepted
time: 61ms
memory: 2436kb

input:

281 397
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
...

output:

271

result:

ok single line: '271'

Test #16:

score: 5
Accepted
time: 0ms
memory: 2376kb

input:

288 15
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1...

output:

16

result:

ok single line: '16'

Test #17:

score: 5
Accepted
time: 49ms
memory: 2500kb

input:

295 350
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
...

output:

251

result:

ok single line: '251'

Test #18:

score: 5
Accepted
time: 20ms
memory: 2396kb

input:

272 218
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
...

output:

179

result:

ok single line: '179'

Test #19:

score: 5
Accepted
time: 118ms
memory: 2428kb

input:

279 541
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
...

output:

279

result:

ok single line: '279'

Test #20:

score: 5
Accepted
time: 11ms
memory: 2460kb

input:

286 149
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
...

output:

148

result:

ok single line: '148'