ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#196855 | #2663. 旅游 | hsfzbzjr | 100 | 385ms | 1988kb | C++11 | 1.0kb | 2023-11-02 09:53:06 | 2023-11-02 12:04:13 |
answer
/*
f[u][c] 表示从 u 出发在 u 的子树中走 c 步,最多能走到几个不同的节点
如果需要在访问完之后回到 u,那么最多访问 min{sz[u],c/2} 个节点
*/
#include<bits/stdc++.h>
using namespace std;
const int N=310;
int n,m;
int f[N][2*N];
vector<int> to[N];
int sz[N];
void dfs1(int u,int fa){
sz[u]=1;
for(auto v:to[u]){
if(v==fa)continue;
dfs1(v,u);
sz[u]+=sz[v];
}
}
void dfs2(int u,int fa){
f[u][0]=1;
for(int i=1;i<=m;i++)f[u][i]=min(sz[u]-1,i/2);
for(auto v:to[u])
if(v!=fa)dfs2(v,u);
for(int i=1;i<=m;i++){
for(auto v:to[u])if(v!=fa){
for(int j=1;j<=i;j++){
f[u][i]=max(f[u][i],f[v][j-1]+min(sz[u]-sz[v]-1,(i-j)/2));
}
}
f[u][i]+=1;
}
}
int main(){
scanf("%d %d",&n,&m);
for(int i=1;i<n;i++){
int u,v;
scanf("%d %d",&u,&v);
to[u].push_back(v);
to[v].push_back(u);
}
dfs1(1,0);
dfs2(1,0);
printf("%d\n",f[1][m]);
// for(int i=1;i<=n;i++,cout<<endl)
// for(int j=0;j<=m;j++)cout<<f[i][j]<<" ";
return 0;
}
Details
小提示:点击横条可展开更详细的信息
Test #1:
score: 5
Accepted
time: 7ms
memory: 1924kb
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: 12ms
memory: 1940kb
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: 19ms
memory: 1956kb
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: 18ms
memory: 1980kb
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: 3ms
memory: 1916kb
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: 0ms
memory: 1936kb
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: 21ms
memory: 1956kb
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: 60ms
memory: 1968kb
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: 35ms
memory: 1988kb
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: 48ms
memory: 1928kb
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: 0ms
memory: 1944kb
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: 29ms
memory: 1964kb
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: 5ms
memory: 1980kb
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: 1928kb
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: 30ms
memory: 1940kb
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: 1960kb
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: 27ms
memory: 1976kb
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: 10ms
memory: 1920kb
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: 55ms
memory: 1940kb
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: 6ms
memory: 1952kb
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'