ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#196852 | #2663. 旅游 | tkswls | 100 | 1341ms | 2712kb | C++ | 1.1kb | 2023-11-02 09:30:24 | 2023-11-02 12:03:47 |
answer
#include <bits/stdc++.h>
using namespace std;
int n, m, f[305][605], g[305][605];
vector<int> son[305];
void dfs(int p, int q) {
for (int i = 0; i <= m; i++) {
f[p][i] = g[p][i] = 1;
}
for (int i = 0; i < son[p].size(); i++) {
if (son[p][i] == q) continue;
dfs(son[p][i], p);
for (int j = m; j >= 0; j--) {
for (int k = 0; k <= j - 2; k++) {
g[p][j] = max(g[p][j - k - 2] + f[son[p][i]][k], g[p][j]);
}
}
for (int j = m; j >= 0; j--) {
for (int k = 0; k <= j - 1; k++) {
g[p][j] = max(f[p][j - k - 1] + g[son[p][i]][k], g[p][j]);
}
}
for (int j = m; j >= 0; j--) {
for (int k = 0; k <= j - 2; k++) {
f[p][j] = max(f[p][j - k - 2] + f[son[p][i]][k], f[p][j]);
}
}
}
// cout << p << "=======" << endl;
// for (int i = 0; i <= m; i++) {
/// cout << f[p][i] << ' ' << g[p][i] << "|";
// }
// cout << endl;
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> n >> m;
int q, p;
for (int i = 1; i < n; i++) {
cin >> p >> q;
son[p].push_back(q), son[q].push_back(p);
}
dfs(1, 0);
cout << g[1][m];
return 0;
}
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 5
Accepted
time: 21ms
memory: 2584kb
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: 40ms
memory: 2612kb
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: 67ms
memory: 2652kb
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: 70ms
memory: 2684kb
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: 6ms
memory: 2576kb
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: 8ms
memory: 2608kb
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: 81ms
memory: 2636kb
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: 199ms
memory: 2676kb
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: 133ms
memory: 2712kb
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: 179ms
memory: 2600kb
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: 2628kb
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: 89ms
memory: 2664kb
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: 13ms
memory: 2696kb
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: 2588kb
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: 107ms
memory: 2620kb
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: 1ms
memory: 2652kb
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: 85ms
memory: 2684kb
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: 34ms
memory: 2576kb
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: 191ms
memory: 2612kb
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: 17ms
memory: 2648kb
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'